diff --git a/docs/adr/0008-handle-heavy-task-with-isolate.md b/docs/adr/0008-handle-heavy-task-with-isolate.md new file mode 100644 index 000000000..cf4b5c9b0 --- /dev/null +++ b/docs/adr/0008-handle-heavy-task-with-isolate.md @@ -0,0 +1,48 @@ +# 8. Handle heavy task with isolate + +Date: 2022-06-29 + +## Status + +Accepted + +## Context + +- Use isolate to handle heavy task + +## Decision + +- Initialize the IsolateManager to help manage the list of tasks and isolate +``` +final isolateManager = IsolateManager(); +await isolateManager.initialize(isolatesCount: 1); +``` + + `isolatesCount`: The number of isolates created and run concurrently. Depends on your device's processor. + +- Perform task processing by calling `execute` function +``` +isolateManager.execute( + arguments, + handleTaskAction, + onUpdateProgress +); +``` + + `arguments`: Parameters passed to the function handling heavy tasks. + + `handleTaskAction`: Function to handle heavy tasks + + `onUpdateProgress`: Callback function every time a value is sent from `new isolate` to `main isolate` via `sendPort` + +- The return value is a `Completer` + +- Communicate between two isolates +To see a visual representation of this process, take a look at the image below: + +[communicate_between_two_isolate](../images/handle_task_with_isolate.png); + +## Consequences + +- No UI block when handling long-term tasks. +- Increase application performance, smoother application. + +## Reference + +- [worker_manager](https://pub.dev/packages/worker_manager) diff --git a/docs/images/handle_task_with_isolate.png b/docs/images/handle_task_with_isolate.png new file mode 100644 index 000000000..15fc869d3 Binary files /dev/null and b/docs/images/handle_task_with_isolate.png differ