EasyWorker constructor

EasyWorker(
  1. void entrypoint(
    1. SendPort from
    ), {
  2. required String workerName,
  3. dynamic initialMessage,
  4. bool paused = false,
  5. bool errorsAreFatal = true,
  6. SendPort? onExit,
  7. SendPort? onError,
})

Implementation

EasyWorker(
  void Function(SendPort from) entrypoint, {
  required String workerName,
  dynamic initialMessage,
  bool paused = false,
  bool errorsAreFatal = true,
  SendPort? onExit,
  SendPort? onError,
}) : _fromIsolate = ReceivePort(workerName) {
  _fromIsolate.listen((message) {
    if (message is SendPort) {
      _toIsolate = message;
      if (!_firstMessageDelivered) {
        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;
    Future.delayed(
        const Duration(milliseconds: 50), () => _ready.complete(true));
  });
}