Keep replaced tab navigation owners from overwriting newer state
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../shell/destination.dart';
|
||||
import '../core/storage/ordered_local_storage.dart';
|
||||
import 'app_route.dart';
|
||||
import 'tab_locations_store.dart';
|
||||
import 'tab_location_routes.dart';
|
||||
@@ -19,6 +20,10 @@ class TabLocations {
|
||||
bool persistenceFailed = false;
|
||||
Future<void> _writes = Future<void>.value();
|
||||
|
||||
Object get _storageKey => store is SharedPreferencesTabLocationsStore
|
||||
? (store as SharedPreferencesTabLocationsStore).key
|
||||
: store;
|
||||
|
||||
void record(AppRoute route, {bool visited = true}) {
|
||||
if (_disposed) return;
|
||||
active = tabForRoute(route);
|
||||
@@ -34,7 +39,7 @@ class TabLocations {
|
||||
Future<AppRoute?> restore({required bool restoreActive}) async {
|
||||
TabLocationsSnapshot saved;
|
||||
try {
|
||||
saved = await store.load();
|
||||
saved = await orderedLocalStorage(_storageKey, store.load);
|
||||
} catch (_) {
|
||||
// Keep in-memory navigation, but never overwrite unread stored history.
|
||||
persistenceFailed = true;
|
||||
@@ -63,8 +68,13 @@ class TabLocations {
|
||||
},
|
||||
);
|
||||
_writes = _writes.then((_) async {
|
||||
if (_disposed) return;
|
||||
try {
|
||||
persistenceFailed = !await store.save(snapshot);
|
||||
final saved = await orderedLocalStorage<bool?>(_storageKey, () async {
|
||||
if (_disposed) return null;
|
||||
return store.save(snapshot);
|
||||
});
|
||||
if (saved != null) persistenceFailed = !saved;
|
||||
} catch (_) {
|
||||
persistenceFailed = true;
|
||||
}
|
||||
|
||||
@@ -37,3 +37,17 @@ and scroll offsets are not serialized by this store. Their existing mounted
|
||||
state remains intact within a session. Settings inner-topic persistence is a
|
||||
separate companion change. Manager-owned integrated browser capture/build and
|
||||
publication are still required; this lane did not deploy or restart anything.
|
||||
|
||||
## Replacement-owner write ordering
|
||||
|
||||
A disposed owner skips writes that have not started, including operations queued
|
||||
behind another storage owner. Already-started operations share a small per-key
|
||||
read/write order with replacement owners; replacement reads wait for an old
|
||||
in-flight write before merging and saving newer navigation. Idle operation keys
|
||||
are removed, and a failure never poisons later operations.
|
||||
|
||||
18 focused tab/controller/full-app navigation cases passed after this follow-up
|
||||
(`/tmp/memwatch/out.hBbm4Y`), including a blocked old write plus a queued old write,
|
||||
disposal, replacement navigation and delayed release. The newer state wins for
|
||||
both an in-memory store and distinct SharedPreferences store instances sharing
|
||||
a logical key. Two independent ordering-helper tests passed (`out.FffOAC`).
|
||||
|
||||
@@ -45,6 +45,19 @@ class FailingTabLocationsStore extends MemoryTabLocationsStore {
|
||||
}
|
||||
}
|
||||
|
||||
class GatedPreferencesTabLocationsStore
|
||||
extends SharedPreferencesTabLocationsStore {
|
||||
GatedPreferencesTabLocationsStore({required super.scope});
|
||||
final gate = Completer<void>();
|
||||
final started = Completer<void>();
|
||||
@override
|
||||
Future<bool> save(TabLocationsSnapshot snapshot) async {
|
||||
if (!started.isCompleted) started.complete();
|
||||
await gate.future;
|
||||
return super.save(snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
test('Settings and nested pages retain Pipelines ownership', () async {
|
||||
@@ -247,4 +260,69 @@ void main() {
|
||||
expect(store.writes, isEmpty);
|
||||
},
|
||||
);
|
||||
test(
|
||||
'disposed queued writes cannot overwrite a replacement controller',
|
||||
() async {
|
||||
final gate = Completer<bool>();
|
||||
final store = MemoryTabLocationsStore()..write = gate;
|
||||
final old = TabLocations(store)..record(AppRoute.settings);
|
||||
await old.restore(restoreActive: false);
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(store.writes, hasLength(1));
|
||||
old.record(AppRoute.chat); // queued behind the blocked Settings write
|
||||
old.dispose();
|
||||
final replacement = TabLocations(store)..record(AppRoute.run(12));
|
||||
var readFinished = false;
|
||||
final restoring = replacement.restore(restoreActive: false).then((value) {
|
||||
readFinished = true;
|
||||
return value;
|
||||
});
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(readFinished, isFalse, reason: 'wait for the old in-flight write');
|
||||
gate.complete(true);
|
||||
await restoring;
|
||||
await old.settled;
|
||||
await replacement.settled;
|
||||
expect(store.value.active, AppDestination.runs);
|
||||
expect(store.value.routes[AppDestination.runs], AppRoute.run(12));
|
||||
expect(store.value.routes[AppDestination.pipelines], AppRoute.settings);
|
||||
expect(
|
||||
store.value.routes[AppDestination.chat],
|
||||
isNull,
|
||||
reason: 'the disposed owner never starts its queued Chat write',
|
||||
);
|
||||
expect(store.writes, hasLength(2));
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'different preference store instances share ordering for the same key',
|
||||
() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
final oldStore = GatedPreferencesTabLocationsStore(scope: 'same-server');
|
||||
final old = TabLocations(oldStore)..record(AppRoute.settings);
|
||||
await old.restore(restoreActive: false);
|
||||
await oldStore.started.future;
|
||||
old.record(AppRoute.chat);
|
||||
old.dispose();
|
||||
const newStore = SharedPreferencesTabLocationsStore(scope: 'same-server');
|
||||
final replacement = TabLocations(newStore)..record(AppRoute.run(18));
|
||||
var readFinished = false;
|
||||
final restoring = replacement.restore(restoreActive: false).then((value) {
|
||||
readFinished = true;
|
||||
return value;
|
||||
});
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(readFinished, isFalse);
|
||||
oldStore.gate.complete();
|
||||
await restoring;
|
||||
await old.settled;
|
||||
await replacement.settled;
|
||||
final saved = await newStore.load();
|
||||
expect(saved.active, AppDestination.runs);
|
||||
expect(saved.routes[AppDestination.runs], AppRoute.run(18));
|
||||
expect(saved.routes[AppDestination.pipelines], AppRoute.settings);
|
||||
expect(saved.routes[AppDestination.chat], isNull);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user