Skip to content

Commit 80b6053

Browse files
committed
[green] fence editor transfers by revision and connection session
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent a75d478 commit 80b6053

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

‎app/lib/editor/program_actions.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,16 @@ class ProgramActions {
133133
required String source,
134134
ProgressCb? onProgress,
135135
}) async {
136+
final Connection connection = _ref.read(connectionProvider);
137+
final Object stamp = connectionSessionStampOf(connection);
136138
await saveSource(path: path, source: source, onProgress: onProgress);
137-
await _ref.read(connectionProvider).runFile(path);
139+
if (!identical(_ref.read(connectionProvider), connection) ||
140+
!identical(connectionSessionStampOf(connection), stamp)) {
141+
// The upload may have completed on the previous attachment. Do not send
142+
// the second verb to a replacement board.
143+
throw const NotConnectedException();
144+
}
145+
await connection.runFile(path);
138146
}
139147

140148
/// Writes a Python file and then its raw JSON companion commit record.

‎app/lib/editor/run_controller.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,15 @@ class RunController {
117117
}
118118
final EditorDocument uploadedDocument = _ref.read(editorDocumentProvider);
119119
final String path = boardPathForDocument(uploadedDocument);
120+
final int epoch = _ref.read(editorDocumentEpochProvider);
121+
final Connection connection = _connection;
122+
final Object stamp = connectionSessionStampOf(connection);
120123
await _ref
121124
.read(programActionsProvider)
122125
.runSourceFile(path: path, source: cleaned);
123-
if (_ref.read(editorDocumentProvider) == uploadedDocument) {
126+
if (_ref.read(editorDocumentEpochProvider) == epoch &&
127+
identical(_connection, connection) &&
128+
identical(connectionSessionStampOf(connection), stamp)) {
124129
_ref.read(editorDocumentProvider.notifier).markSaved(boardPath: path);
125130
}
126131
}

‎app/lib/editor/save_controller.dart‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ library;
2525
import 'package:flutter_riverpod/flutter_riverpod.dart';
2626

2727
import 'package:pyble/pble/pble.dart';
28+
import 'package:pyble/app/providers.dart';
2829

2930
import 'editor_document.dart';
3031
import 'program_actions.dart';
@@ -73,14 +74,19 @@ class SaveController {
7374
doc = _ref.read(editorDocumentProvider);
7475
}
7576

77+
final int epoch = _ref.read(editorDocumentEpochProvider);
78+
final Connection connection = _ref.read(connectionProvider);
79+
final Object stamp = connectionSessionStampOf(connection);
7680
await _ref
7781
.read(programActionsProvider)
7882
.saveSource(path: path, source: cleaned, onProgress: onProgress);
7983

8084
// Only after CRC-verified success, and only if this is still the document
8185
// whose bytes were uploaded: clear dirty and adopt the path. A user may edit
8286
// or open another document while a BLE transfer is in flight.
83-
if (_ref.read(editorDocumentProvider) == doc) {
87+
if (_ref.read(editorDocumentEpochProvider) == epoch &&
88+
identical(_ref.read(connectionProvider), connection) &&
89+
identical(connectionSessionStampOf(connection), stamp)) {
8490
_ref.read(editorDocumentProvider.notifier).markSaved(boardPath: path);
8591
}
8692
return path;

‎app/lib/files/file_explorer_controller.dart‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,20 @@ class FileExplorerController extends Notifier<FileExplorerState> {
493493
Future<void> openInEditor(String fileName) async {
494494
final String path = _join(state.cwd, fileName);
495495
final int epoch = ++_openEpoch;
496-
final EditorDocument before = ref.read(editorDocumentProvider);
496+
final int documentEpoch = ref.read(editorDocumentEpochProvider);
497+
final Connection connection = _conn;
498+
final Object stamp = connectionSessionStampOf(connection);
497499
try {
498-
final Uint8List bytes = await _conn.getFile(
500+
final Uint8List bytes = await connection.getFile(
499501
path,
500502
onProgress: (TransferProgress progress) {
501503
if (epoch == _openEpoch) _onProgress(progress);
502504
},
503505
);
504-
if (epoch != _openEpoch || ref.read(editorDocumentProvider) != before) {
506+
if (_disposed ||
507+
epoch != _openEpoch ||
508+
ref.read(editorDocumentEpochProvider) != documentEpoch ||
509+
!_isCapturedSessionCurrent(connection: connection, stamp: stamp)) {
505510
return;
506511
}
507512
state = state.copyWith(clearProgress: true);
@@ -562,14 +567,18 @@ class FileExplorerController extends Notifier<FileExplorerState> {
562567
Future<void> uploadCurrentBuffer() async {
563568
final EditorDocument doc = ref.read(editorDocumentProvider);
564569
final String path = _join(state.cwd, doc.name);
570+
final int epoch = ref.read(editorDocumentEpochProvider);
571+
final Connection connection = _conn;
572+
final Object stamp = connectionSessionStampOf(connection);
565573
try {
566-
await _conn.putFile(
574+
await connection.putFile(
567575
path,
568576
Uint8List.fromList(utf8.encode(doc.content)),
569577
onProgress: _onProgress,
570578
);
571579
state = state.copyWith(clearProgress: true);
572-
if (ref.read(editorDocumentProvider) == doc) {
580+
if (ref.read(editorDocumentEpochProvider) == epoch &&
581+
_isCapturedSessionCurrent(connection: connection, stamp: stamp)) {
573582
ref.read(editorDocumentProvider.notifier).markSaved(boardPath: path);
574583
}
575584
await refresh();

0 commit comments

Comments
 (0)