Persist exact tab locations including Settings under Pipelines
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../shell/destination.dart';
|
||||
import 'app_route.dart';
|
||||
import 'tab_locations_store.dart';
|
||||
import 'tab_location_routes.dart';
|
||||
|
||||
export 'tab_location_routes.dart';
|
||||
|
||||
class TabLocations {
|
||||
TabLocations(this.store);
|
||||
final TabLocationsStore store;
|
||||
final routes = <AppDestination, AppRoute>{};
|
||||
final _touched = <AppDestination>{};
|
||||
AppDestination active = AppDestination.pipelines;
|
||||
int revision = 0;
|
||||
bool _loaded = false;
|
||||
bool _disposed = false;
|
||||
bool persistenceFailed = false;
|
||||
Future<void> _writes = Future<void>.value();
|
||||
|
||||
void record(AppRoute route, {bool visited = true}) {
|
||||
if (_disposed) return;
|
||||
active = tabForRoute(route);
|
||||
routes[active] = route;
|
||||
if (visited) {
|
||||
_touched.add(active);
|
||||
revision++;
|
||||
}
|
||||
if (_loaded) _save();
|
||||
}
|
||||
|
||||
/// Slow storage never overwrites navigation made while it was loading.
|
||||
Future<AppRoute?> restore({required bool restoreActive}) async {
|
||||
TabLocationsSnapshot saved;
|
||||
try {
|
||||
saved = await store.load();
|
||||
} catch (_) {
|
||||
// Keep in-memory navigation, but never overwrite unread stored history.
|
||||
persistenceFailed = true;
|
||||
return null;
|
||||
}
|
||||
if (_disposed) return null;
|
||||
for (final entry in saved.routes.entries) {
|
||||
if (!_touched.contains(entry.key)) routes[entry.key] = entry.value;
|
||||
}
|
||||
AppRoute? restored;
|
||||
if (restoreActive && _touched.isEmpty) {
|
||||
active = saved.active;
|
||||
restored = routes[active];
|
||||
}
|
||||
_loaded = true;
|
||||
_save();
|
||||
return restored;
|
||||
}
|
||||
|
||||
void _save() {
|
||||
final snapshot = TabLocationsSnapshot(
|
||||
active: active,
|
||||
routes: {
|
||||
for (final entry in routes.entries)
|
||||
entry.key: durableTabRoute(entry.value),
|
||||
},
|
||||
);
|
||||
_writes = _writes.then((_) async {
|
||||
try {
|
||||
persistenceFailed = !await store.save(snapshot);
|
||||
} catch (_) {
|
||||
persistenceFailed = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void dispose() => _disposed = true;
|
||||
|
||||
Future<void> get settled => _writes;
|
||||
}
|
||||
Reference in New Issue
Block a user