ensureStarted method

  1. @override
Future<Satellite> ensureStarted({
  1. required DbName dbName,
  2. required DBSchema dbDescription,
  3. required DatabaseAdapter adapter,
  4. required Migrator migrator,
  5. required Notifier notifier,
  6. required SocketFactory socketFactory,
  7. required HydratedConfig config,
  8. SatelliteOverrides? opts,
})
inherited

Implementation

@override
Future<Satellite> ensureStarted({
  required DbName dbName,
  required DBSchema dbDescription,
  required DatabaseAdapter adapter,
  required Migrator migrator,
  required Notifier notifier,
  required SocketFactory socketFactory,
  required HydratedConfig config,
  SatelliteOverrides? opts,
}) async {
  // If we're in the process of stopping the satellite process for this
  // dbName, then we wait for the process to be stopped and then we
  // call this function again to retry starting it.
  final stoppingPromises = this.stoppingPromises;
  final stopping = stoppingPromises[dbName];
  if (stopping != null) {
    return stopping.then(
      (_) => ensureStarted(
        dbName: dbName,
        dbDescription: dbDescription,
        adapter: adapter,
        migrator: migrator,
        notifier: notifier,
        socketFactory: socketFactory,
        config: config,
        opts: opts,
      ),
    );
  }

  // If we're in the process of starting the satellite process for this
  // dbName, then we short circuit and return that process. Note that
  // this assumes that the previous call to start the process for this
  // dbName would have passed in functionally equivalent `dbAdapter`,
  // `fs` and `notifier` arguments. Which is *probably* a safe assumption
  // in the case where this might happen, which is multiple components
  // in the same app opening a connection to the same db at the same time.
  final starting = startingPromises[dbName];
  if (starting != null) {
    return starting;
  }

  // If we already have a satellite process running for this db, then
  // return it.
  final satellite = satellites[dbName];
  if (satellite != null) {
    return satellite;
  }

  // Otherwise we need to fire it up!
  final startingPromise = startProcess(
    dbName: dbName,
    dbDescription: dbDescription,
    adapter: adapter,
    migrator: migrator,
    notifier: notifier,
    socketFactory: socketFactory,
    config: config,
  ).then((satellite) {
    satellites[dbName] = satellite;

    return satellite;
  }).whenComplete(() {
    startingPromises.remove(dbName);
  });

  startingPromises[dbName] = startingPromise;
  return startingPromise;
}