IsolateManager
instance can be used for only one Function, so that the stream
can be used easily when using something like a StreamBuilder
(Use the@isolateManagerWorker
and @isolateManagerCustomWorker
annotations).IsolateManagerShared
instance can be used for multiple Functions, it's good for computing multiple Functions and doesn't care much about the stream
(Use the @isolateManagerSharedWorker
annotation).Worker
on the Web. If the Worker
is unavailable in the working browser or is not configured, the Future
(and Stream
) will be used.WASM
compilation on the Web.try-catch
blocks.compute
operations are allowed because the plugin will queue the input data and send it to a free isolate later.Execute a recursive Fibonacci function 70 times, computing the sequence for the numbers 30, 33, and 36. The results are in microseconds (On Macbook M1 Pro 14-inch 16Gb RAM).
Fibonacci | Main App | One Isolate | Three Isolates | Isolate.run |
---|---|---|---|---|
30 | 751,364 | 771,142 | 274,854 | 769,588 |
33 | 3,189,873 | 3,185,798 | 1,152,083 | 3,214,685 |
36 | 13,510,136 | 13,540,763 | 4,873,100 | 13,766,930 |
Worker
supported)Fibonacci | Main App | One Worker | Three Workers | Isolate.run (Unsupported) |
---|---|---|---|---|
30 | 2,125,101 | 547,800 | 195,101 | 0 |
33 | 9,083,800 | 2,286,899 | 803,599 | 0 |
36 | 38,083,500 | 9,575,899 | 3,383,299 | 0 |
See here for the test details.
A function used for the Isolate MUST BE a static
or top-level
function. The @pragma('vm:entry-point')
annotation also should be added to this function to ensure that tree-shaking doesn't remove the code since it would be invoked on the native side.
@pragma('vm:entry-point')
int add(List<int> values) {
return values[0] + values[1];
}
dart:ui
, material
,... The best way is to move these functions into a separate file so we can control the imports easily.JSON
(or Dart primitive types) to make the Worker
work properly.void main() async {
// Create 3 isolateShared to solve the problems
final isolateShared = IsolateManager.createShared(
concurrent: 3,
// Remove this line (or set it to `false`) if you don't want to use the Worker
useWorker: true,
// Add this mappings so we can ignore the `workerName` parameter
// when using the `compute` method.
workerMappings: {
addFuture : 'addFuture',
add : 'add',
}
);
// Compute the values. The return type and parameter type will respect the type
// of the function.
final added = await isolateShared.compute(
addFuture,
[1.1, 2.2],
// workerFunction: 'addFuture', // Ignored because the `workerMappings` is specified
);
print('addFuture: 1.1 + 2.2 = $added');
// Compute the values. The return type and parameter type will respect the type
// of the function.
final added = await isolateShared.compute(
add,
[1, 2],
// workerFunction: 'add', // Ignored because the `workerMappings` is specified
);
print('add: 1 + 2 = $added');
}
@isolateManagerSharedWorker
Future<double> addFuture(List<double> values) async {
return values[0] + values[1];
}
@isolateManagerSharedWorker
int add(List<int> values) {
return values[0] + values[1];
}
Run this command to generate a Javascript Worker (named $shared_worker.js
inside the web
folder):
dart run isolate_manager:generate
Add flag --shared
if you want to generate only for the IsolateManagerShared
.
There are multiple ways to use this package. The only thing to notice is that the function
has to be a static
or top-level
function.
main() async {
final isolate = IsolateManager.create(
fibonacci,
// And the name of the function if you want to use the Worker.
// Otherwise, you can ignore this parameter.
workerName: 'fibonacci',
concurrent: 2,
);
isolate.stream.listen((value) {
print(value);
});
final fibo = await isolate(20);
}
@isolateManagerWorker // Remove this annotation if you don't want to use the Worker
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
Run this command to generate a Javascript Worker:
dart run isolate_manager:generate
Add flag --single
if you want to generate only for the IsolateManager
.
You can restart or stop the isolate using this method:
await isolateManager.restart();
await isolateManager.stop();
You can control everything with this method when you want to create multiple isolates for a function.
Let it automatically handles the result and the Exception:
@isolateManagerCustomWorker // Remove this line if you don't want to use the Worker
void customIsolateFunction(dynamic params) {
IsolateManagerFunction.customFunction<int, int>(
params,
onEvent: (controller, message) {
/* This event will be executed every time the `message` is received from the main isolate */
return fibonacci(message);
},
onInitial: (controller, initialParams) {
/* This event will be executed before all the other events and only one time. */
},
onDispose: (controller) {
/* This event will be executed after all the other events and should NOT be a `Future` event */
},
);
}
Handle the result and the Exception by your self:
@isolateManagerCustomWorker // Remove this line if you don't want to use the Worker
void customIsolateFunction(dynamic params) {
IsolateManagerFunction.customFunction<Map<String, dynamic>, String>(
params,
onEvent: (controller, message) {
// This event will be executed every time the `message` is received from the main isolate.
try {
final result = fibonacci(message);
controller.sendResult(result);
} catch (err, stack) {
controller.sendResultError(IsolateException(err, stack));
}
// Just returns something that unused to complete this method.
return 0;
},
onInitial: (controller, initialParams) {
/* This event will be executed before all the other events. */
},
onDispose: (controller) {
/* This event will be executed after all the other events. */
},
autoHandleException: false,
autoHandleResult: false,
);
}
final isolateManager = IsolateManager.createCustom(
customIsolateFunction,
initialParams: 'This is the initialParams',
// And the name of the function if you want to use the Worker.
// Otherwise, you can ignore this parameter.
workerName: 'customIsolateFunction',
debugMode: true,
);
Now you can use everything as the Basic Usage.
When you have a computation that you want to compute as soon as possible, you can change the priority
parameter to true
to promote it to the top of the Queue.
You can set the maximum number of the queued computations for an IsolateManager
or IsolateManagerShared
by changing the maxCount
value.
If the maxCount
is <= 0, the max number of the queued computations is unlimited.
When creating a new IsolateManager
or IsolateManagerShared
, you can define the queueStrategy
to control how new computations are added to or retrieved from the queue. There are four fundamental strategies:
/// Unlimited queued computations (default).
QueueStrategyUnlimited()
/// Remove the newest computation if the [maxCount] is exceeded.
QueueStrategyRemoveNewest();
/// Remove the oldest computation if the [maxCount] is exceeded.
QueueStrategyRemoveOldest()
/// Discard the new incoming computation if the [maxCount] is exceeded.
QueueStrategyDiscardIncoming()
You can extend the QueueStrategy
and use the queues
, maxCount
and queuesCount
to create your own strategy. These are how the basic strategies are created:
class QueueStrategyUnlimited<R, P> extends QueueStrategy<R, P> {
/// Unlimited queued computations.
QueueStrategyUnlimited();
@override
bool continueIfMaxCountExceeded() {
// It means the current computation should be added to the Queue
// without doing anything with the `queues`.
return true;
}
}
class QueueStrategyRemoveNewest<R, P> extends QueueStrategy<R, P> {
/// Remove the newest computation if the [maxCount] is exceeded.
QueueStrategyRemoveNewest({super.maxCount = 0});
@override
bool continueIfMaxCountExceeded() {
// Remove the last computation if the Queue (mean the newest one).
queues.removeLast();
// It means the current computation should be added to the Queue.
return true;
}
}
class QueueStrategyRemoveOldest<R, P> extends QueueStrategy<R, P> {
/// Remove the oldest computation if the [maxCount] is exceeded.
QueueStrategyRemoveOldest({super.maxCount = 0});
@override
bool continueIfMaxCountExceeded() {
// Remove the first computation if the Queue (mean the oldest one).
queues.removeFirst();
// It means the current computation should be added to the Queue.
return true;
}
}
class QueueStrategyDiscardIncoming<R, P> extends QueueStrategy<R, P> {
/// Discard the new incoming computation if the [maxCount] is exceeded.
QueueStrategyDiscardIncoming({super.maxCount = 0});
@override
bool continueIfMaxCountExceeded() {
// It means the current computation should NOT be added to the Queue.
return false;
}
}
You can use try-catch
to catch exceptions:
try {
final result = await isolate(-10);
} on SomeException catch (e1) {
print(e1);
} catch (e2) {
print(e2);
}
You can even manage the final result by using this callback, useful when you create your own function that needs to send the progress value before returning the final result:
main() {
// Create an IsolateManager instance.
final isolateManager = IsolateManager.createCustom(progressFunction);
// Get the result.
final result = await isolateManager.compute(100, callback: (value) {
// Condition to recognize the progress value. Ex:
final data = jsonDecode(value);
if (data.containsKey('progress')) {
print('This is a progress value: ${data['progress']}');
// Return `false` to mark this value is not the final.
return false;
}
print('This is a final value: ${data['result']}');
// Return `true` to mark this value is the final.
return true;
});
print(result); // 100
}
// This is a progress function
@isolateManagerCustomWorker // Add this anotation for a custom function
void progressFunction(dynamic params) {
IsolateManagerFunction.customFunction<String, int>(
params,
onEvent: (controller, message) {
// This value is sent as the progress values.
for (int i = 0; i < message; i++) {
final progress = jsonEncode({'progress' : messsage});
controller.sendResult(progress);
}
// This is a final value.
return jsonEncode({'result' : messsage});
},
);
}
The result that you get from the isolate (or Worker) is sometimes different from the result that you want to get from the return type in the main app, you can use converter
and workerConverter
parameters to convert the result received from the Isolate
(converter) and Worker
(workerConverter). Example:
List
main() async {
final isolate = IsolateManager.create(
aList,
workerName: 'aList',
isDebug: true,
);
final list = ['a', 'b', 'c'];
final result = await isolate.compute(list);
expect(result, equals(list));
}
@isolateManagerSharedWorker
@isolateManagerWorker
List aList(List params) {
return params;
}
Map
main() async {
final isolate = IsolateManager.create(
aMap,
workerName: 'aMap',
isDebug: true,
);
await isolate.start();
final map = {'a': '1', 'b': 2, 'c': 3};
final result = await isolate.compute(map);
expect(result, equals(map));
}
@isolateManagerSharedWorker
@isolateManagerWorker
Map aMap(Map params) {
return params;
}
Data flow: Main -> Isolate or Worker -> Converter -> Result
--single
: Generates single Functions only.
--shared
: Generates shared Functions only.
--in <path>
(or -i <path>
): Inputted folder.
--out <path>
(or -o <path>
): Outputted folder.
--obfuscate <level>
: The obfuscated level of JS (0 to 4). The default is set to 4
.
--debug
: Keeps the temp files for debugging.
If you want to add options or flags to the Dart to Js Compiler, you can add a --
flag before adding those options and flags. Please note that all the arguments after the --
flag will be passed directly into the Dart to Js Compiler. For instance:
dart run isolate_manager:generate --single -i test -out test -- -Dkey1=value1 -Dkey2=value2
[Experiment]: Automatically generate the workerMappings
for both IsolateManager
and IsolateManagerShared
by adding a --worker-mappings-experiment=lib/main.dart
flag to the generator. Here are the steps of the generator:
_addWorkerMappings
method at the end of the lib/main.dart
.IsolateManager.addWorkerMapping
method._addWorkerMappings
method to the beginning of the main
method.queuesLength
to get the current number of queued computation.ensureStarted
to able to wait for the start
method to finish when you want to call the start
method manually without await
and wait for it later.isStarted
to check if the start
method is completed or not.All above examples use top-level
functions so the workerName
will be the same as the function name. If you use static
functions, you have to add the class name like ClassName.functionName
to the workerName
parameter. For instance:
class MyIsolateFuncs {
@isolateManagerWorker
static String isolateFunc(String params) {
return params;
}
}
Then the IsolateManager
should be:
final isolate = IsolateManager.create(
MyIsolateFuncs.isolateFunc,
workerName: 'MyIsolateFuncs.isolateFunc',
);
PR Notes:
test/bin/run.sh
to run all needed tests (the generator included)..js
files inside the test) to keep to the original files for backward compatibility testing.