stream_isolate 0.1.1 stream_isolate: ^0.1.1 copied to clipboard
A utility wrapper class for working with isolates using stream-powered intercommunication.
A wrapper class for Isolate
that exposes a communication channel using a Stream
.
API Reference #
Usage #
To use, call StreamIsolate.spawn
with the same types of arguments passed to Isolate.spawn
. You can then use the returned instance to subscribe to events published by the isolate:
final streamIsolate = await StreamIsolate.spawn<int>(doWork);
await for (final i in streamIsolate.stream) {
print(i);
}
Stream<int> doWork(_) async* {
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
}
You can also call StreamIsolate.spawnBidirectional
to create an isolate that exposes an additional communication stream for sending messages to the instance:
final streamIsolate = await StreamIsolate.spawnBidirectional<String, int>(doWork);
await for (final i in streamIsolate.stream) {
print(i);
streamIsolate.send('received');
}
Stream<int> doWorkWithListener(Stream<String> inc, _) async* {
inc.listen((msg) => print('from main: $msg'));
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
}