integral_isolates 0.1.0+1 integral_isolates: ^0.1.0+1 copied to clipboard
Create a long lived isolate easily
Easy to use isolates for Dart and Flutter.
Usage #
Almost as easy to use as compute, but using a long lived isolate. For example:
void main() async {
final isolated = Isolated();
final isolate = isolated.isolate;
print(await isolate(_isPrime, 7));
print(await isolate(_isPrime, 42));
isolated.dispose();
}
bool _isPrime(int value) {
if (value == 1) {
return false;
}
for (int i = 2; i < value; ++i) {
if (value % i == 0) {
return false;
}
}
return true;
}
Remember to always dispose once you are done using the isolate to clean up and close the isolate.
isolated.dispose();
Different backpressure strategies are also supported by just sending in the desired strategy:
Isolated(backpressureStrategy: DiscardNewBackPressureStrategy());
Currently supported strategies can be found in the documentation.
Additional information #
The API of this package is not final, and is subject to change.
Are you using hooks? #
Try the use_isolate package that controls the lifecycle of the isolate, so you don't have to.