EasyWorker<R, I> constructor

EasyWorker<R, I>(
  1. Entrypoint<I> entrypoint, {
  2. required String workerName,
  3. I? initialMessage,
  4. bool paused = false,
  5. bool errorsAreFatal = true,
  6. SendPort? onExit,
  7. SendPort? onError,
})

Creates and spawns an isolate that shares the same code as the current isolate.

The argument entryPoint specifies the initial function to call in the spawned isolate. The entry-point function is invoked in the new isolate with message as the only argument.

Implementation

EasyWorker(
  Entrypoint<I> entrypoint, {
  required String workerName,
  I? initialMessage,
  bool paused = false,
  bool errorsAreFatal = true,
  SendPort? onExit,
  SendPort? onError,
}) : _fromIsolate = ReceivePort(workerName) {
  _fromIsolate.listen((message) {
    if (message is SendPort) {
      _toIsolate = message;
      _ready.complete(true);
      if (!_firstMessageDelivered && initialMessage != null) {
        send(initialMessage);
        _firstMessageDelivered = true;
      }
      return;
    }

    _controller.sink.add(message);
  });

  Isolate.spawn(
    entrypoint,
    _fromIsolate.sendPort,
    debugName: "Worker: $workerName",
    paused: paused,
    onExit: onExit,
    onError: onError,
    errorsAreFatal: errorsAreFatal,
  ).then((value) {
    isolate = value;
  });
}