build static method

Future<IsolateRunner> build([
  1. RunnerBuilder? builder
])

Create a new Isolate, as by Isolate.spawn and wrap that using a RunnerBuilder to configure the underlying Isolate

see IsolateRunner.spawn The created isolate is set to have errors not be fatal.

Implementation

static Future<IsolateRunner> build([RunnerBuilder? builder]) async {
  builder ??= RunnerBuilder.defaults();
  var initialPortGetter = SingleResponseChannel();
  var isolate = await Isolate.spawn(
      IsolateRunnerRemote._create, initialPortGetter.port,
      debugName: builder.debugName);

  // Whether an uncaught exception should kill the isolate
  isolate.setErrorsFatal(builder.failOnError);
  var pingChannel = SingleResponseChannel();
  isolate.ping(pingChannel.port);
  var commandPort = (await initialPortGetter.result as SendPort);
  var result = IsolateRunner(isolate, commandPort);

  try {
    await _initializeIsolateRunner(builder, result);
  } catch (e, stack) {
    print(stack);
    await result.close();
    rethrow;
  }

  // Guarantees that setErrorsFatal has completed.
  await pingChannel.result;

  if (builder.autoCloseChildren) {
    /// Use a separate channel.
    final shutdownResponse = SingleResponseChannel(callback: (_) {
      print(
          '############  SHUTDOWN ${builder!.debugNameBase}  ##################');
    });
    Isolate.current.addOnExitListener(commandPort,
        response: [_shutdown, shutdownResponse.port]);
  }

  return result;
}