OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 // ------------------------------------------------------------------- | 7 // ------------------------------------------------------------------- |
8 // Controller for creating and adding events to a stream. | 8 // Controller for creating and adding events to a stream. |
9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
10 | 10 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 abstract class StreamController<T> implements StreamSink<T> { | 61 abstract class StreamController<T> implements StreamSink<T> { |
62 /** The stream that this controller is controlling. */ | 62 /** The stream that this controller is controlling. */ |
63 Stream<T> get stream; | 63 Stream<T> get stream; |
64 | 64 |
65 /** | 65 /** |
66 * A controller with a [stream] that supports only one single subscriber. | 66 * A controller with a [stream] that supports only one single subscriber. |
67 * | 67 * |
68 * If [sync] is true, the returned stream controller is a | 68 * If [sync] is true, the returned stream controller is a |
69 * [SynchronousStreamController], and must be used with the care | 69 * [SynchronousStreamController], and must be used with the care |
70 * and attention necessary to not break the [Stream] contract. | 70 * and attention necessary to not break the [Stream] contract. |
| 71 * See [Completer.sync] for some explanations on when a synchronous |
| 72 * dispatching can be used. |
| 73 * If in doubt, keep the controller non-sync. |
71 * | 74 * |
72 * The controller will buffer all incoming events until the subscriber is | 75 * A Stream should be inert until a subscriber starts listening on it (using |
73 * registered. | 76 * the [onListen] callback to start producing events). Streams should not |
| 77 * leak resources (like websockets) when no user ever listens on the stream. |
| 78 * |
| 79 * The controller buffers all incoming events until a subscriber is |
| 80 * registered, but this feature should only be used in rare circumstances. |
74 * | 81 * |
75 * The [onPause] function is called when the stream becomes | 82 * The [onPause] function is called when the stream becomes |
76 * paused. [onResume] is called when the stream resumed. | 83 * paused. [onResume] is called when the stream resumed. |
77 * | 84 * |
78 * The [onListen] callback is called when the stream | 85 * The [onListen] callback is called when the stream |
79 * receives its listener and [onCancel] when the listener ends | 86 * receives its listener and [onCancel] when the listener ends |
80 * its subscription. If [onCancel] needs to perform an asynchronous operation, | 87 * its subscription. If [onCancel] needs to perform an asynchronous operation, |
81 * [onCancel] should return a future that completes when the cancel operation | 88 * [onCancel] should return a future that completes when the cancel operation |
82 * is done. | 89 * is done. |
83 * | 90 * |
84 * If the stream is canceled before the controller needs new data the | 91 * If the stream is canceled before the controller needs new data the |
85 * [onResume] call might not be executed. | 92 * [onResume] call might not be executed. |
86 */ | 93 */ |
87 factory StreamController({void onListen(), | 94 factory StreamController({void onListen(), |
88 void onPause(), | 95 void onPause(), |
89 void onResume(), | 96 void onResume(), |
90 onCancel(), | 97 onCancel(), |
91 bool sync: false}) { | 98 bool sync: false}) { |
92 return sync | 99 return sync |
93 ? new _SyncStreamController<T>(onListen, onPause, onResume, onCancel) | 100 ? new _SyncStreamController<T>(onListen, onPause, onResume, onCancel) |
94 : new _AsyncStreamController<T>(onListen, onPause, onResume, onCancel); | 101 : new _AsyncStreamController<T>(onListen, onPause, onResume, onCancel); |
95 } | 102 } |
96 | 103 |
97 /** | 104 /** |
98 * A controller where [stream] can be listened to more than once. | 105 * A controller where [stream] can be listened to more than once. |
99 * | 106 * |
100 * The [Stream] returned by [stream] is a broadcast stream. | 107 * The [Stream] returned by [stream] is a broadcast stream. |
101 * It can be listened to more than once. | 108 * It can be listened to more than once. |
102 * | 109 * |
| 110 * A Stream should be inert until a subscriber starts listening on it (using |
| 111 * the [onListen] callback to start producing events). Streams should not |
| 112 * leak resources (like websockets) when no user ever listens on the stream. |
| 113 * |
| 114 * Broadcast streams do not buffer events when there is no listener. |
| 115 * |
103 * The controller distributes any events to all currently subscribed | 116 * The controller distributes any events to all currently subscribed |
104 * listeners at the time when [add], [addError] or [close] is called. | 117 * listeners at the time when [add], [addError] or [close] is called. |
105 * It is not allowed to call `add`, `addError`, or `close` before a previous | 118 * It is not allowed to call `add`, `addError`, or `close` before a previous |
106 * call has returned. The controller does not have any internal queue of | 119 * call has returned. The controller does not have any internal queue of |
107 * events, and if there are no listeners at the time the event is added, | 120 * events, and if there are no listeners at the time the event is added, |
108 * it will just be dropped, or, if it is an error, be reported as uncaught. | 121 * it will just be dropped, or, if it is an error, be reported as uncaught. |
109 * | 122 * |
110 * Each listener subscription is handled independently, | 123 * Each listener subscription is handled independently, |
111 * and if one pauses, only the pausing listener is affected. | 124 * and if one pauses, only the pausing listener is affected. |
112 * A paused listener will buffer events internally until unpaused or canceled. | 125 * A paused listener will buffer events internally until unpaused or canceled. |
113 * | 126 * |
114 * If [sync] is true, events may be fired directly by the stream's | 127 * If [sync] is true, events may be fired directly by the stream's |
115 * subscriptions during an [add], [addError] or [close] call. | 128 * subscriptions during an [add], [addError] or [close] call. |
116 * The returned stream controller is a [SynchronousStreamController], | 129 * The returned stream controller is a [SynchronousStreamController], |
117 * and must be used with the care and attention necessary to not break | 130 * and must be used with the care and attention necessary to not break |
118 * the [Stream] contract. | 131 * the [Stream] contract. |
| 132 * See [Completer.sync] for some explanations on when a synchronous |
| 133 * dispatching can be used. |
| 134 * If in doubt, keep the controller non-sync. |
119 * | 135 * |
120 * If [sync] is false, the event will always be fired at a later time, | 136 * If [sync] is false, the event will always be fired at a later time, |
121 * after the code adding the event has completed. | 137 * after the code adding the event has completed. |
122 * In that case, no guarantees are given with regard to when | 138 * In that case, no guarantees are given with regard to when |
123 * multiple listeners get the events, except that each listener will get | 139 * multiple listeners get the events, except that each listener will get |
124 * all events in the correct order. Each subscription handles the events | 140 * all events in the correct order. Each subscription handles the events |
125 * individually. | 141 * individually. |
126 * If two events are sent on an async controller with two listeners, | 142 * If two events are sent on an async controller with two listeners, |
127 * one of the listeners may get both events | 143 * one of the listeners may get both events |
128 * before the other listener gets any. | 144 * before the other listener gets any. |
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
915 _StreamControllerAddStreamState(_StreamController controller, | 931 _StreamControllerAddStreamState(_StreamController controller, |
916 this.varData, | 932 this.varData, |
917 Stream source, | 933 Stream source, |
918 bool cancelOnError) | 934 bool cancelOnError) |
919 : super(controller, source, cancelOnError) { | 935 : super(controller, source, cancelOnError) { |
920 if (controller.isPaused) { | 936 if (controller.isPaused) { |
921 addSubscription.pause(); | 937 addSubscription.pause(); |
922 } | 938 } |
923 } | 939 } |
924 } | 940 } |
OLD | NEW |