boot<I, O, CR> static method
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,
);
}