Last active
September 3, 2022 12:43
-
-
Save pezi/93547b590ffd84937c1c65a7dbff3581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dart_periphery/src/isolate_helper.dart'; | |
import 'dummy.dart'; | |
class SomeClass { | |
static int counter = 0; | |
@InitJob() | |
static InitJobResult initJob() { | |
var dev = DummyDev(); | |
return InitJobResult(false, dev.toJson()); | |
} | |
@MainJob() | |
static MainJobResult mainJob(String json) { | |
var dev = DummyDev.isolate(json); | |
var m = <String, dynamic>{}; | |
m['result'] = '${counter++}'; | |
return MainJobResult(false, false, m); | |
} | |
@ExitJob() | |
static ExitJobResult exitJob(String json) { | |
var dev = DummyDev.isolate(json); | |
var m = <String, dynamic>{}; | |
return ExitJobResult(false, m); | |
} | |
} | |
void main() async { | |
SomeClass c = SomeClass(); | |
IsolateHelper h = IsolateHelper(c, JobIteration(3)); | |
await for (var s in h.run()) { | |
if (s is MainJobResult) { | |
print(s.data!['result']); | |
} | |
} | |
h = IsolateHelper(c, JobIteration.endless()); | |
int index = 0; | |
await for (var s in h.run()) { | |
if (s is MainJobResult) { | |
print(s.data!['result']); | |
if (index == 3) { | |
break; | |
} | |
++index; | |
} | |
} | |
h.killIsolate(); | |
if (h.json != null) { | |
var dummy = DummyDev.isolate(h.json as String); | |
// close handle | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment