pub package github stars license MIT

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;
}

Libraries

stream_isolate