TF-616 Add adr for handle heavy task with isolate

This commit is contained in:
dab246
2022-06-30 11:16:37 +07:00
committed by Dat H. Pham
parent 589abfafb8
commit fe5de99f5d
2 changed files with 48 additions and 0 deletions
@@ -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)
Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB