init method

Future<Object> init({
  1. required UiSnapshot? captureSnapshot(),
  2. bool isInScope(
    1. String screen
    )?,
  3. bool scheduleSharedCapture()?,
  4. Future<void>? captureSharedNow()?,
})

Load persisted state.

Implementation

Future<Object> init({
  required UiSnapshot? Function() captureSnapshot,
  bool Function(String screen)? isInScope,
  bool Function()? scheduleSharedCapture,
  Future<void>? Function()? captureSharedNow,
}) async {
  final source = _CaptureSource(
      captureSnapshot, isInScope, scheduleSharedCapture, captureSharedNow);
  _sources.add(source);
  _disposed = false;
  _gateHandle ??= NavigationGate.instance.addConsumer();
  if (_initialized) return source;

  final rawGraph = await _store.read(kRoutesLogicalPath);
  if (rawGraph != null) {
    final graph = RouteGraphFile.fromJson(rawGraph) ?? emptyGraph();
    if (graph.schemaVersion == kCacheSchemaVersion) {
      _graph = graph;
    } else {
      // The graph holds screen NAMES only — no stable IDs — so a format
      // bump does not invalidate it. Restamp instead of discarding, so
      // visit counts and edges survive the upgrade.
      _graph = RouteGraphFile(
        schemaVersion: kCacheSchemaVersion,
        type: graph.type,
        nodes: graph.nodes,
        edges: graph.edges,
        sync: graph.sync,
      );
      await serialize(kRoutesLogicalPath, _persistGraph);
    }
  }

  final rawManifest = await _store.read(kManifestLogicalPath);
  final manifest =
      rawManifest == null ? null : CacheManifest.fromJson(rawManifest);
  if (manifest != null && manifest.schemaVersion == kCacheSchemaVersion) {
    _manifest.addAll(manifest.screens);
  } else {
    // Missing manifest, or one written by an older format: re-index from
    // the files, discarding screen files of another format version.
    await _rebuildManifest();
  }

  _initialized = true;

  if (_pendingNavigations.isNotEmpty) {
    final replay = List<_PendingNavigation>.of(_pendingNavigations);
    _pendingNavigations.clear();
    for (final nav in replay) {
      await onNavigate(nav.to, from: nav.from);
    }
  }

  return source;
}