0 purchases
easy worker
Easy Worker #
Working with Dart Isolate is not very easy for beginners and tedious to write
big boilerplate code for veteran and even though there are lot of packages to
solve this issue none were exactly as straight forward as they try to be.
Well then EasyWorker might be the simplest way to work with dart isolate so far.
Features #
Easy to learn and use
Simple to start a long running process or a short computation.
easiest bidirectional communication
Getting started #
Add easy_worker to pubspec.yaml #
dependencies:
flutter:
sdk: flutter
easy_worker:
...
copied to clipboard
Usage #
For long running task and bidirectional communication between your main isolate and the worker isolate, You will need two things: #
A static or top level function for example this factorial calculator
/// This takes number (message/payload or whatever you wanna say) and a sender
void calculateFactorial(int number, Sender send) {
int temp = number;
number--;
while (number > 0) {
temp *= number;
number--;
// just to simulate long running task with blocking operation
// (operation that can freeze the ui thread)
sleep(const Duration(seconds: 1));
}
/// once done send the calculated result to the parent process
send(temp);
}
copied to clipboard
A EasyWorker or EasyCompute instance
A. With EasyWorker #
Create an instance of EasyWorker
// EasyWorker<Result Type, Input Type>
final worker = EasyWorker<int, int>(
Entrypoint(calculateFactorial),
workerName: "Factorial Calculator",
initialMessage: 0, // Optional: Initial payload for this worker will be 0
);
await worker.waitUntilReady();
copied to clipboard
Now how to send and receive to and from this worker?
get the first result only
final result = await worker.stream.first;
copied to clipboard
Listen to all the results coming from isolate
worker.onMessage((message) {
print("Message From Worker: $message");
});
copied to clipboard
Send Message to the worker
/// send 6 as payload to the worker to get the factorial of
/// 6.
worker.send(6);
copied to clipboard
B. (NEW) With EasyCompute : Resuable isolates for high performance apps. #
Create an instance of EasyCompute
// EasyCompute<Result Type, Input Type>
final worker = EasyCompute<int, int>(
ComputeEntrypoint(calculateFactorial),
workerName: "Factorial Calculator",
);
await worker.waitUntilReady();
copied to clipboard
Get results
final result = await worker.compute(6);
print(result); //
copied to clipboard
Dispose when not needed anymore #
worker.dispose()
copied to clipboard
FAQs #
What about simple one time tasks?
/// just call compute and pass your entrypoint and payload
final result = await EasyWorker.compute<int, int>(calculateFactorial, 5);
print(result); // 120
copied to clipboard
Additional information #
FAQ:
Why this package if there are already other who are doing the same?
Basically all of them just have a learning curve and there technicalities, this package doesn't.
I want to implement something. how to do it?
You can create a feature request on github.
License #
MIT
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.