Skip to content

Commit 5c54025

Browse files
committed
[green] make filesystem listings latest-wins
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent 4b060c0 commit 5c54025

1 file changed

Lines changed: 80 additions & 16 deletions

File tree

‎app/lib/files/file_explorer_controller.dart‎

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ class FileExplorerController extends Notifier<FileExplorerState> {
280280
/// replaced, the controller is disposed, or a newer initialization starts.
281281
int _initEpoch = 0;
282282

283+
/// Makes directory listings latest-request-wins within one board session.
284+
int _listingEpoch = 0;
285+
286+
/// Prevents queued or in-flight asynchronous work from reading a dead ref.
287+
bool _disposed = false;
288+
283289
/// Serializes explicit Open-as-Blocks source downloads. The native row stays
284290
/// responsive, but a rapid second activation cannot start a duplicate GET
285291
/// or a competing preview flow.
@@ -300,14 +306,24 @@ class FileExplorerController extends Notifier<FileExplorerState> {
300306
_lastConnState = connState.value;
301307
connState.addListener(_onConnState);
302308
ref.onDispose(() {
309+
_disposed = true;
303310
_initEpoch += 1;
311+
_listingEpoch += 1;
304312
connState.removeListener(_onConnState);
305313
});
306314

307315
if (_isAttached(connState.value)) {
308316
// Already connected at first build (the gated shell's normal path):
309317
// root-init (fsRoot -> initial listing) asynchronously.
310-
scheduleMicrotask(_init);
318+
final int scheduledEpoch = _initEpoch;
319+
scheduleMicrotask(() {
320+
if (_disposed ||
321+
scheduledEpoch != _initEpoch ||
322+
!_isAttached(connState.value)) {
323+
return;
324+
}
325+
_init();
326+
});
311327
}
312328
return FileExplorerState(
313329
fsRoot: '/',
@@ -332,6 +348,7 @@ class FileExplorerController extends Notifier<FileExplorerState> {
332348
// DEVICE_INFO and the initial listing belong to the attachment that
333349
// started them. Prevent either completion from publishing after detach.
334350
_initEpoch += 1;
351+
_listingEpoch += 1;
335352
// The board went away: drop the stale listing (the view shows the
336353
// disconnected guidance); the next connection re-roots via _init.
337354
state = state.copyWith(
@@ -348,10 +365,15 @@ class FileExplorerController extends Notifier<FileExplorerState> {
348365

349366
/// Reads `fsRoot` from the board and lists the root directory.
350367
Future<void> _init() async {
351-
final int epoch = ++_initEpoch;
368+
if (_disposed) return;
352369
final Connection connection = _conn;
370+
if (!_isAttached(connection.state.value)) return;
371+
372+
final int epoch = ++_initEpoch;
373+
_listingEpoch += 1;
353374
final Object sessionStamp = connectionSessionStampOf(connection);
354375
String? reportedRoot;
376+
int? initialListingEpoch;
355377
state = state.copyWith(
356378
loading: true,
357379
hasReportedFsRoot: false,
@@ -369,30 +391,38 @@ class FileExplorerController extends Notifier<FileExplorerState> {
369391

370392
reportedRoot = info.fsRoot;
371393
_fsRoot = reportedRoot;
394+
initialListingEpoch = ++_listingEpoch;
372395
state = state.copyWith(
373396
fsRoot: _fsRoot,
374397
hasReportedFsRoot: true,
375398
cwd: _fsRoot,
376399
);
377400

378401
final List<RemoteEntry> entries = await connection.listDir(reportedRoot);
379-
if (!_isInitContextCurrent(
380-
epoch: epoch,
381-
connection: connection,
382-
stamp: sessionStamp,
383-
) ||
384-
state.cwd != reportedRoot) {
402+
if (!_isListingContextCurrent(
403+
epoch: initialListingEpoch,
404+
connection: connection,
405+
stamp: sessionStamp,
406+
path: reportedRoot,
407+
)) {
385408
return;
386409
}
387410
entries.sort(_entryOrder);
388411
state = state.copyWith(entries: entries, loading: false);
389412
} on PbleException catch (e) {
390-
if (!_isInitContextCurrent(
391-
epoch: epoch,
392-
connection: connection,
393-
stamp: sessionStamp,
394-
) ||
395-
(reportedRoot != null && state.cwd != reportedRoot)) {
413+
final bool isCurrent = reportedRoot == null
414+
? _isInitContextCurrent(
415+
epoch: epoch,
416+
connection: connection,
417+
stamp: sessionStamp,
418+
)
419+
: _isListingContextCurrent(
420+
epoch: initialListingEpoch!,
421+
connection: connection,
422+
stamp: sessionStamp,
423+
path: reportedRoot,
424+
);
425+
if (!isCurrent) {
396426
return;
397427
}
398428
state = state.copyWith(
@@ -405,16 +435,37 @@ class FileExplorerController extends Notifier<FileExplorerState> {
405435

406436
/// Re-lists [FileExplorerState.cwd] (FR-FILES-1).
407437
Future<void> refresh() async {
438+
if (_disposed) return;
439+
final Connection connection = _conn;
440+
final Object sessionStamp = connectionSessionStampOf(connection);
441+
final String path = state.cwd;
442+
final int epoch = ++_listingEpoch;
408443
state = state.copyWith(loading: true, clearError: true);
409444
try {
410-
final List<RemoteEntry> entries = await _conn.listDir(state.cwd);
445+
final List<RemoteEntry> entries = await connection.listDir(path);
446+
if (!_isListingContextCurrent(
447+
epoch: epoch,
448+
connection: connection,
449+
stamp: sessionStamp,
450+
path: path,
451+
)) {
452+
return;
453+
}
411454
entries.sort(_entryOrder);
412455
state = state.copyWith(entries: entries, loading: false);
413456
} on PbleException catch (e) {
457+
if (!_isListingContextCurrent(
458+
epoch: epoch,
459+
connection: connection,
460+
stamp: sessionStamp,
461+
path: path,
462+
)) {
463+
return;
464+
}
414465
state = state.copyWith(
415466
loading: false,
416467
error: _kindOf(e),
417-
errorPath: state.cwd,
468+
errorPath: path,
418469
);
419470
}
420471
}
@@ -791,10 +842,23 @@ class FileExplorerController extends Notifier<FileExplorerState> {
791842
required Connection connection,
792843
required Object stamp,
793844
}) =>
845+
!_disposed &&
794846
epoch == _initEpoch &&
795847
_isCapturedSessionCurrent(connection: connection, stamp: stamp) &&
796848
_isAttached(connection.state.value);
797849

850+
bool _isListingContextCurrent({
851+
required int epoch,
852+
required Connection connection,
853+
required Object stamp,
854+
required String path,
855+
}) =>
856+
!_disposed &&
857+
epoch == _listingEpoch &&
858+
state.cwd == path &&
859+
_isCapturedSessionCurrent(connection: connection, stamp: stamp) &&
860+
_isAttached(connection.state.value);
861+
798862
/// Wall-clock for the transfer in flight; restarted per transfer.
799863
Stopwatch? _txClock;
800864
int _lastSent = 0;

0 commit comments

Comments
 (0)