openBackend method

  1. @override
Future<void> openBackend()
override

Opens the backend. Idempotent.

Implementation

@override
Future<void> openBackend() async {
  if (_bundle != null) return;
  // NOTE: read before the store opens, which re-points Hive's global
  // instance at [storagePath]. hive marks the field for tests, and nothing
  // public names the directory its global instance points at.
  // ignore: invalid_use_of_visible_for_testing_member
  final globalHome = (Hive as HiveImpl).homePath;
  final bundle = await openReportingOnce(() => _factory.initialize(atSign,
      HivePersistenceConfig.clientDefaults(storagePath: storagePath)));
  final AtSyncQueue queue;
  try {
    queue = await openHiveSyncQueue(atSign, storagePath: storagePath);
  } catch (_) {
    await bundle.close();
    rethrow;
  }
  _bundle = bundle;
  _queue = queue;

  final here = HiveInstances.canonicalPathFor(storagePath);
  final elsewhere = {
    ..._directoriesOpened,
    if (globalHome != null) HiveInstances.canonicalPathFor(globalHome),
  }..remove(here);
  for (final directory in elsewhere) {
    await _adoptStrayQueueIn(directory);
  }
  _directoriesOpened.add(here);
  _open.add(this);
  await _handOnStrayQueuesIn(here);
}