-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
From the Stream documentation: "Stream transformations, such as where and skip, always return non-broadcast streams."
The following code snippet demonstrates that this isn't consistently true among transformation methods:
void main() {
var stream = new StreamController.broadcast().stream;
testBroadcast("asyncExpand",
stream.asyncExpand((e) => new Stream.fromIterable([e])));
testBroadcast("asyncMap", stream.asyncMap((e) => e));
testBroadcast("distinct", stream.distinct());
testBroadcast("expand", stream.expand((e) => [e]));
testBroadcast("handleError", stream.handleError((e) => throw e));
testBroadcast("map", stream.map((e) => e));
testBroadcast("skip", stream.skip(1));
testBroadcast("skipWhile", stream.skipWhile(() => false));
testBroadcast("takeWhile", stream.skipWhile(() => true));
testBroadcast("timeout", stream.timeout(new Duration(seconds: 5)));
testBroadcast("transform",
stream.transform(new StreamTransformer.fromHandlers()));
testBroadcast("where", stream.where((_) => true));
}
void testBroadcast(String name, Stream stream) {
print("$name ${stream.isBroadcast ? 'is' : 'is not'} broadcast");
}
This prints:
asyncExpand is not broadcast
asyncMap is not broadcast
distinct is broadcast
expand is broadcast
handleError is broadcast
map is broadcast
skip is broadcast
skipWhile is broadcast
takeWhile is broadcast
timeout is not broadcast
transform is not broadcast
where is broadcast
Most transformations, including "where" and "skip" which are explicitly called out as not preserving the broadcast state, do in fact preserve it.