run<R, P> method

  1. @override
Future<R> run<R, P>(
  1. FutureOr<R> function(
    1. P argument
    ),
  2. P argument, {
  3. String? name,
  4. Duration? timeout,
  5. FutureOr<R> onTimeout()?,
  6. bool ignoreShutdown = false,
})
override

Request that function be called with the provided arguments.

The arguments will be applied to the function in the same way as by Function.apply, but it may happen in a different isolate or setting.

It's necessary that the function can be sent through a SendPort if the call is performed in another isolate. That means the other isolate should be created using Isolate.spawn so that it is running the same code as the sending isolate, and the function must be a static or top-level function.

Waits for the result of the call, and completes the returned future with the result, whether it's a value or an error.

If the returned future does not complete before timeLimit has passed, the onTimeout action is executed instead, and its result (whether it returns or throws) is used as the result of the returned future.

If onTimeout is omitted, a timeout will cause the returned future to complete with a TimeoutException.

The default implementation runs the function in the current isolate.

Implementation

@override
Future<R> run<R, P>(FutureOr<R> Function(P argument) function, P argument,
    {String? name,
    Duration? timeout,
    FutureOr<R> onTimeout()?,
    bool ignoreShutdown = false}) async {
  try {
    final runner = await _runner;
    if (isShuttingDown && ignoreShutdown != true) {
      throw '$debugName: This service is shutting down';
    }
    final res = await Future.sync(() => runner.run(function, argument,
        timeout: timeout ?? defaultTimeout,
        onTimeout: onTimeout)).catchError((err) {
      print("${name ?? 'unknown'}: isolate: $err");
    });

    return res;
  } catch (e, stack) {
    print(stack);
    rethrow;
  }
}