run method

Future<R> run(
  1. dynamic callback(
    1. A arg
    )?
)

Implementation

Future<R> run(Function(A arg)? callback) async {
  var completer = Completer<R>();
  var receivePort = ReceivePort();
  var errorPort = ReceivePort();

  job._sendPort = receivePort.sendPort;
  job._errorPort = errorPort.sendPort;

  var isolate = await Isolate.spawn<CallbackIsolateJob<R, A>>(
      _isolateBody, job,
      errorsAreFatal: true);

  receivePort.listen((data) {
    if (data is _IsolateCallbackArg) {
      if (callback != null) callback(data.value);
    } else if (data is R) {
      completer.complete(data);
      isolate.kill();
    }
  });

  errorPort.listen((e) {
    completer.completeError(e);
  });

  return completer.future;
}