spawn static method

Create a new Isolate, as by Isolate.spawn and wrap that.

The returned IsolateRunner forwards operations to the new isolate, and keeps a port open in the new isolate that receives commands from the IsolateRunner. Remember to close the IsolateRunner when it's no longer needed.

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

Implementation

static Future<IsolateRunner> spawn() async {
  var channel = SingleResponseChannel();
  var isolate =
      await Isolate.spawn(IsolateRunnerRemote._create, channel.port);
  // The runner can be used to run multiple independent functions.
  // An accidentally uncaught error shouldn't ruin it for everybody else.
  isolate.setErrorsFatal(false);
  var pingChannel = SingleResponseChannel();
  isolate.ping(pingChannel.port);
  var commandPort = await channel.result as SendPort;
  var result = IsolateRunner(isolate, commandPort);
  // Guarantees that setErrorsFatal has completed.
  await pingChannel.result;
  return result;
}