spawnIsolate method

Future<(Isolate?, PortListener?, SendPort?, Future<bool>?)> spawnIsolate()

Implementation

Future<(Isolate?, PortListener?, SendPort?, Future<bool>?)>
    spawnIsolate() async {
  if (_started) {
    return (null, null, null, null);
  }

  final workerDebugName = this.workerDebugName;

  var spawnPort = ReceivePort('$workerDebugName:spawnPort');

  var spawnPortListener = PortListener(spawnPort);

  var resumePortAsync = spawnPortListener.next();

  final isolate = await Isolate.spawn(
    _isolateInit,
    (worker: this, mainWorkerPort: spawnPort.sendPort),
    debugName: workerDebugName,
  );

  var exitPort = ReceivePort();
  var completer = Completer<bool>();
  exitPort.listen((_) => completer.complete(true));
  isolate.addOnExitListener(exitPort.sendPort);

  SendPort resumePort = await resumePortAsync;

  return (isolate, spawnPortListener, resumePort, completer.future);
}