iso

Last updated:

0 purchases

iso Image
iso Images
Add to Cart

Description:

iso

Iso #

An isolates runner that handles bidirectionnal communication. Run some code in an isolate and communicate with it.
Example #
import 'package:iso/iso.dart';

void run(IsoRunner iso) async {
int counter = 0;
// init the data in channel
iso.receive();
// listen to the data coming in
iso.dataIn.listen((dynamic data) {
counter = counter + int.parse("$data");
// send into the main thread
iso.send(counter);
});
}

void main() async {
final iso = Iso(run, onDataOut: (dynamic data) => print("Counter: $data"));
iso.run();
await iso.onCanReceive;
// now we can send messages to the isolate
while (true) {
await Future<dynamic>.delayed(Duration(seconds: 1));
iso.send(1);
}
}
copied to clipboard
Usage #
Initialize #
Define a function to be run in an isolate:
void run(IsoRunner iso) {
// do something here
}
copied to clipboard
Important: this must be a top level function or a static method. The
function can be async if needed
Initialize a runner:
final iso = Iso(run);
copied to clipboard
Launch the function in the isolate:
iso.run();
// to terminate it:
iso.dispose();
copied to clipboard
The function can be run with parameters:
final params = <dynamic>["arg1", "arg2", 3];
iso.run(params);
copied to clipboard
To grab the parameters in the isolate:
void run(IsoRunner iso) {
if (iso.hasArgs) {
final List<dynamic> args = iso.args;
}
}
copied to clipboard
Communication channels #
Data coming from the isolate
Handle data coming from the isolate using a handler function:
void onDataOut(dynamic data) => print("Data coming from isolate: $data");

final iso = Iso(run, onDataOut: onDataOut);
copied to clipboard
Another option to handle this data is to listen to a channel:
iso.dataOut.listen((dynamic payload) {
if (payload == <String, dynamic>{"status": "finished"}) {
print("Isolate declares it has finished");
iso.kill();
}
});
copied to clipboard
Data coming into the isolate
By default this data channel is not activated: you need to do it in the run function if needed:
void run(IsoRunner iso) {
iso.receive();
iso.dataIn.listen((dynamic data) {
// do something with the data
});
}
copied to clipboard
or:
void run(IsoRunner iso) {
iso.receive()
..listen((dynamic data) =>
print("Data received in isolate -> $data / ${data.runtimeType}"));
}
copied to clipboard
Send data to the isolate
This has to be initialized in the isolate before sending as explained above.
iso.run();
// wait for the isolate to be ready to receive data
await iso.onCanReceive;
// send data
iso.send("Some data");
copied to clipboard
Send data from the isolate to the main thread
void run(IsoRunner iso) {
// ...
iso.send("Some data");
}
copied to clipboard
Examples #
Check the example folder to see examples for Dart and Flutter

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.