OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 /// A collection of streams whose events are unified and sent through a central | 7 /// A collection of streams whose events are unified and sent through a central |
8 /// stream. | 8 /// stream. |
9 /// | 9 /// |
10 /// Both errors and data events are forwarded through [stream]. The streams in | 10 /// Both errors and data events are forwarded through [stream]. The streams in |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 /// If it's a broadcast group and it goes dormant again, broadcast stream | 46 /// If it's a broadcast group and it goes dormant again, broadcast stream |
47 /// subscriptions will be canceled and set to null again. Single-subscriber | 47 /// subscriptions will be canceled and set to null again. Single-subscriber |
48 /// stream subscriptions will be left intact, since they can't be | 48 /// stream subscriptions will be left intact, since they can't be |
49 /// re-subscribed. | 49 /// re-subscribed. |
50 final _subscriptions = new Map<Stream<T>, StreamSubscription<T>>(); | 50 final _subscriptions = new Map<Stream<T>, StreamSubscription<T>>(); |
51 | 51 |
52 /// Merges the events from [streams] into a single (single-subscriber) stream. | 52 /// Merges the events from [streams] into a single (single-subscriber) stream. |
53 /// | 53 /// |
54 /// This is equivalent to adding [streams] to a group, closing that group, and | 54 /// This is equivalent to adding [streams] to a group, closing that group, and |
55 /// returning its stream. | 55 /// returning its stream. |
56 static Stream/*<T>*/ merge/*<T>*/(Iterable<Stream/*<T>*/> streams) { | 56 static Stream<T> merge<T>(Iterable<Stream<T>> streams) { |
57 var group = new StreamGroup/*<T>*/(); | 57 var group = new StreamGroup<T>(); |
58 streams.forEach(group.add); | 58 streams.forEach(group.add); |
59 group.close(); | 59 group.close(); |
60 return group.stream; | 60 return group.stream; |
61 } | 61 } |
62 | 62 |
63 /// Creates a new stream group where [stream] is single-subscriber. | 63 /// Creates a new stream group where [stream] is single-subscriber. |
64 StreamGroup() { | 64 StreamGroup() { |
65 _controller = new StreamController<T>( | 65 _controller = new StreamController<T>( |
66 onListen: _onListen, | 66 onListen: _onListen, |
67 onPause: _onPause, | 67 onPause: _onPause, |
68 onResume: _onResume, | 68 onResume: _onResume, |
69 onCancel: _onCancel, | 69 onCancel: _onCancel, |
70 sync: true); | 70 sync: true); |
71 } | 71 } |
72 | 72 |
73 /// Creates a new stream group where [stream] is a broadcast stream. | 73 /// Creates a new stream group where [stream] is a broadcast stream. |
74 StreamGroup.broadcast() { | 74 StreamGroup.broadcast() { |
75 _controller = new StreamController<T>.broadcast( | 75 _controller = new StreamController<T>.broadcast( |
76 onListen: _onListen, | 76 onListen: _onListen, onCancel: _onCancelBroadcast, sync: true); |
77 onCancel: _onCancelBroadcast, | |
78 sync: true); | |
79 } | 77 } |
80 | 78 |
81 /// Adds [stream] as a member of this group. | 79 /// Adds [stream] as a member of this group. |
82 /// | 80 /// |
83 /// Any events from [stream] will be emitted through [this.stream]. If this | 81 /// Any events from [stream] will be emitted through [this.stream]. If this |
84 /// group has a listener, [stream] will be listened to immediately; otherwise | 82 /// group has a listener, [stream] will be listened to immediately; otherwise |
85 /// it will only be listened to once this group gets a listener. | 83 /// it will only be listened to once this group gets a listener. |
86 /// | 84 /// |
87 /// If this is a single-subscription group and its subscription has been | 85 /// If this is a single-subscription group and its subscription has been |
88 /// canceled, [stream] will be canceled as soon as its added. If this returns | 86 /// canceled, [stream] will be canceled as soon as its added. If this returns |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 if (!stream.isBroadcast) return; | 184 if (!stream.isBroadcast) return; |
187 subscription.cancel(); | 185 subscription.cancel(); |
188 _subscriptions[stream] = null; | 186 _subscriptions[stream] = null; |
189 }); | 187 }); |
190 } | 188 } |
191 | 189 |
192 /// Starts actively forwarding events from [stream] to [_controller]. | 190 /// Starts actively forwarding events from [stream] to [_controller]. |
193 /// | 191 /// |
194 /// This will pause the resulting subscription if [this] is paused. | 192 /// This will pause the resulting subscription if [this] is paused. |
195 StreamSubscription<T> _listenToStream(Stream<T> stream) { | 193 StreamSubscription<T> _listenToStream(Stream<T> stream) { |
196 var subscription = stream.listen( | 194 var subscription = stream.listen(_controller.add, |
197 _controller.add, | 195 onError: _controller.addError, onDone: () => remove(stream)); |
198 onError: _controller.addError, | |
199 onDone: () => remove(stream)); | |
200 if (_state == _StreamGroupState.paused) subscription.pause(); | 196 if (_state == _StreamGroupState.paused) subscription.pause(); |
201 return subscription; | 197 return subscription; |
202 } | 198 } |
203 | 199 |
204 /// Closes the group, indicating that no more streams will be added. | 200 /// Closes the group, indicating that no more streams will be added. |
205 /// | 201 /// |
206 /// If there are no streams in the group, [stream] is closed immediately. | 202 /// If there are no streams in the group, [stream] is closed immediately. |
207 /// Otherwise, [stream] will close once all streams in the group close. | 203 /// Otherwise, [stream] will close once all streams in the group close. |
208 /// | 204 /// |
209 /// Returns a [Future] that completes once [stream] has actually been closed. | 205 /// Returns a [Future] that completes once [stream] has actually been closed. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 | 244 |
249 /// The name of the state. | 245 /// The name of the state. |
250 /// | 246 /// |
251 /// Used for debugging. | 247 /// Used for debugging. |
252 final String name; | 248 final String name; |
253 | 249 |
254 const _StreamGroupState(this.name); | 250 const _StreamGroupState(this.name); |
255 | 251 |
256 String toString() => name; | 252 String toString() => name; |
257 } | 253 } |
OLD | NEW |