boot<I, O, CR> static method

Future<Process<I, O, CR>> boot<I, O, CR>(
  1. Future<CR> setupProcess(),
  2. Future<O> processInput(
    1. I input,
    2. CR setupRecord
    ),
  3. Future<void> shutdownProcess(
    1. CR commonResourceRecord
    ), [
  4. String shutdownCode = '5hu†d0wn',
])

Create a new Operating System process. Runs on main Process.

Implementation

static Future<Process<I, O, CR>> boot<I, O, CR>(
  Future<CR> Function() setupProcess,
  Future<O> Function(I input, CR setupRecord) processInput,
  Future<void> Function(CR commonResourceRecord) shutdownProcess, [
  String shutdownCode = '5hu†d0wn',
]) async {
  // Used to block boot completion until 2-way communication is established.
  // Note: Completer returns a record — a structured bundle of ReceivePort and SendPort.
  final connectionLock = Completer<(ReceivePort, SendPort)>.sync();

  // Create a `RawReceivePort` (as we can set a different handler when we convert it to a
  // `ReceivePort` later).
  final protoFromProcessPort = RawReceivePort();

  // Add in the connectionLock logic. The callback is only executed when `Isolate.spawn` is
  // called later in this method.
  protoFromProcessPort.handler = (toProcessPort) {
    connectionLock.complete(
      // Note: Record, (ReceivePort, SendPort).
      (
        ReceivePort.fromRawReceivePort(protoFromProcessPort),
        toProcessPort as SendPort,
      ),
    );
  };

  // Spawn worker Isolate.
  late final Isolate isolate;

  try {
    isolate = await Isolate.spawn(
      _setupIsolate<I, O, CR>,
      (protoFromProcessPort.sendPort, shutdownCode, setupProcess, processInput, shutdownProcess),
    );
  } catch (err) {
    // Close Port and rethrow error on literally any error.
    protoFromProcessPort.close();
    rethrow;
  }

  // Hold execution until the connectionLock is released. Update Ports.
  final (fromProcessPort, toProcessPort) = await connectionLock.future;

  return Process._(
    setupProcess,
    processInput,
    shutdownProcess,
    shutdownCode,
    isolate,
    toProcessPort,
    fromProcessPort,
  );
}