run<R> static method
Future<R>
run<R>(
- FutureOr<
R> computation(), { - String? workerName,
- Object? workerParameter,
- IsolateConverter<
R> ? converter, - IsolateConverter<
R> ? workerConverter, - bool enableWasmConverter = true,
- bool isDebug = false,
Executes computation
in a one-off isolate and returns its result.
This function behaves similarly to Isolate.run
but also supports Web Workers.
When a workerName
is provided, the corresponding Web Worker is used,
and workerParameter
is passed to the worker.
Transforms results using converter
and workerConverter
before returning them.
When enableWasmConverter
is true
(default), numeric values are automatically
converted back to their intended types during message passing between isolates
or web workers. This ensures proper type handling in WebAssembly environments.
The conversion pipeline applies WASM conversions first, then passes results through any custom converter functions provided.
Example:
@isolateManagerWorker
int fibonacciRecursive(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
final fibo40 = await IsolateManager.run(
() => fibonacciRecursive(40),
workerName: 'fibonacciRecursive',
workerParameter: 40,
);
Set isDebug
to true
to enable debug logging.
Implementation
static Future<R> run<R>(
FutureOr<R> Function() computation, {
String? workerName,
Object? workerParameter,
IsolateConverter<R>? converter,
IsolateConverter<R>? workerConverter,
bool enableWasmConverter = true,
bool isDebug = false,
}) {
return runFunction<R, Object?>(
(_) => computation(),
workerParameter,
workerName: workerName,
converter: converter,
workerConverter: workerConverter,
isDebug: isDebug,
);
}