Keep replaced tab navigation owners from overwriting newer state
This commit is contained in:
@@ -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