forked from deakjahn/flutter_isolate_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.dart
36 lines (31 loc) · 1.04 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Copyright (C) 2020 DEÁK JAHN Gábor.
* All rights reserved.
*/
final worker = BackgroundWorker();
int counter = 0;
void main() {
// Start the worker/isolate at the `entryPoint` function.
worker.spawn<int>(entryPoint,
name: "counter",
// Executed every time data is received from the spawned worker/isolate.
onReceive: setCounter,
// Executed once when spawned worker/isolate is ready for communication.
onInitialized: () => worker.sendTo("counter", counter),
);
}
// Set new count and display current count.
void setCounter(int count) {
counter = count;
print("Counter is now $counter");
// We will no longer be needing the worker/isolate, let's dispose of it.
worker.kill("counter");
}
// This function happens in the worker/isolate.
void entryPoint(String name) {
// Triggered every time data is received from the main app.
worker.listen((count) {
// Add one to the count and send the new value back to the main app.
worker.sendFrom(name, ++count);
}, name: name);
}