You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Lasse's words: This is a fairly primitive version of remote-running a stream function, it doesn't forward pause/resume/cancel calls on the subscription, and it stops on the first error. It does show how to send multiple events back from the other isolate.
import"dart:isolate";
Stream<T> runStream<T>(Stream<T> Function() remoteStream) =>Stream.multi((controller) async {
// New port for event messages.var port =RawReceivePort();
port.handler = (message) {
var list = message asList;
if (list.length ==1) {
controller.add(list[0] asT);
} else {
controller.addError(list[1] asObject, list[2] asStackTrace);
}
};
// Run in other isolate, receive stream events on `port`.try {
awaitIsolate.run(_remoteStream(remoteStream, port.sendPort));
// Returns when stream done.
} catch (e, s) {
controller.addError(e, s);
} finally {
port.close();
controller.close();
}
});
// Creates an argument to `Isolate.run` from a `Stream Function()` and a port.Future<void> Function() _remoteStream(
StreamFunction() createStream, SendPort port) {
Future<void> runStreamSendEvents() async {
try {
awaitfor (var event increateStream()) {
// Send events on port.
port.send([event]);
}
} catch (e, s) {
// Send events on port.
port.send([e, s]);
}
}
return runStreamSendEvents;
}
// Example use:voidmain() async {
awaitfor (var v inrunStream(() =>someInts(5))) {
print(v);
}
}
Stream<int> someInts(int n) async* {
for (var i =0; i < n; i++) {
yield i;
}
}
There's value in keeping the sample as-is, using primitives, rather than replacing it. Using primitives without run is still a use case, even if this particular solution can be modified with run, it's still good to maintain a large example of primitive usage. So maybe another sample can be created, that just shows the same solution written with run.
The text was updated successfully, but these errors were encountered:
I have this example from @lrhn that could be used to update
long_running_isolate.dart
In Lasse's words: This is a fairly primitive version of remote-running a stream function, it doesn't forward pause/resume/cancel calls on the subscription, and it stops on the first error. It does show how to send multiple events back from the other isolate.
There's value in keeping the sample as-is, using primitives, rather than replacing it. Using primitives without
run
is still a use case, even if this particular solution can be modified withrun
, it's still good to maintain a large example of primitive usage. So maybe another sample can be created, that just shows the same solution written withrun
.The text was updated successfully, but these errors were encountered: