isolated_executor 0.2.2
isolated_executor: ^0.2.2 copied to clipboard
Create persistent stateless or stateful Isolate and execute there something.
What is this? #
Provides simple way to use long-lived [Isolate] using [Command]s.
Example (no isolate executor):
final isolate = Isolate.spawn(myMain);
final outPort = await isolate.first;
outPort.send('command-1');
@pragma('vm:entry-point')
void myMain(SendPort outPort) async {
final inPort = ReceivePort();
outPort.send(inPort.sendPort);
MyResult command1() {
return MyResult();
}
await for (final event in inPort) {
switch(event) {
case 'command-1':
return outPort.send(command1());
}
}
}
Example (IsolateExecutor)
final executor = await IsolatedExecutor.stateless();
/// Run for callbacks.
executor.run(command1);
/// Execute for commands.
executor.execute(Command1(AData()));
MyResult command1() {
return MyResult();
}
/// Extend StatelessCommand to automatically generate unique [id] for every command.
class Command1 extends StatelessCommand<int> {
Command1(this.a);
final AData a;
int execute() => a.nothingIsTrueEverythingIsPermitted ? 42 : -1;
}
As you can see u can easily focus on actual actions.