compute<R, I> static method
Future<R>
compute<R, I>(
- WorkerEntrypoint<
I> entrypoint, - I payload, {
- String name = "",
- PayloadCallback? onInit,
- Object? initData,
Spin up a short lived worker, execute the entrypoint
and get the result
Implementation
static Future<R> compute<R, I>(
WorkerEntrypoint<I> entrypoint,
I payload, {
String name = "",
PayloadCallback? onInit,
Object? initData,
}) async {
final onError = ReceivePort();
final worker = EasyWorker<R, I>(
Entrypoint<I>(
entrypoint,
onInit: onInit,
initData: initData,
),
workerName: "compute${name.trim().isEmpty ? "" : ":$name"}",
initialMessage: payload,
onError: onError.sendPort,
);
final Completer<R> completer = Completer<R>();
try {
await worker.waitUntilReady();
worker.stream.take(1).listen((event) => completer.complete(event));
onError.take(1).listen((event) => completer.completeError(event));
final result = await completer.future;
return result;
} catch (e) {
completer.completeError(e);
rethrow;
} finally {
onError.close();
worker.dispose();
}
}