Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: sdk/lib/async/stream_controller.dart

Issue 12393009: Change and structure how Stream implementations do callbacks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
11 /** 11 /**
12 * A controller with the stream it controls. 12 * A controller with the stream it controls.
13 * 13 *
14 * This controller allows sending data, error and done events on 14 * This controller allows sending data, error and done events on
15 * its [stream]. 15 * its [stream].
16 * This class can be used to create a simple stream that others 16 * This class can be used to create a simple stream that others
17 * can listen on, and to push events to that stream. 17 * can listen on, and to push events to that stream.
18 * 18 *
19 * It's possible to check whether the stream is paused or not, and whether 19 * It's possible to check whether the stream is paused or not, and whether
20 * it has subscribers or not, as well as getting a callback when either of 20 * it has subscribers or not, as well as getting a callback when either of
21 * these change. 21 * these change.
22 *
23 * If the stream starts or stops having listeners (first listener subscribing,
floitsch 2013/03/01 21:52:30 We could make our lives easier by making the Strea
Lasse Reichstein Nielsen 2013/03/04 11:53:02 An asBroadCast single-subscription stream is not t
24 * last listener unsubscribing), the `onSubscriptionStateChange` callback
25 * is notified as soon as possible. That will be as soon as a currently firing
26 * event is done firing, or after another callback has finished.
27 * If the pause state has also changed, only the subscription state callback
floitsch 2013/03/01 21:52:30 Not clear: are you saying that a subscription chan
Lasse Reichstein Nielsen 2013/03/04 11:53:02 Correct, if during the same callback/event, both s
28 * is called.
29 *
30 * If the subscriber state has not changed, but the pause state has, the
31 * `onPauseStateChange` callback is notified as soon as possible, after firing
32 * a current event or completing a another callback. This happens if the stream
floitsch 2013/03/01 21:52:30 -a-
Lasse Reichstein Nielsen 2013/03/04 11:53:02 Done.
33 * is not paused, and a listener pauses it, or if the stream has been resumed
34 * from pause and has no pending events. If the listeners resume a paused stream
35 * while it still has queued events, the controller will still consider the
36 * stream paused until all queued events have been dispatched.
37 *
38 * Whether to invoke a callback depends only on the state before and after
39 * a stream action, for example firing an event. If the state changes multiple
40 * times during the action, and then ends up in the same state as before, no
41 * callback is performed.
42 *
43 * If listeners are added after the stream has completed (sent a "done" event),
44 * the listeners will be sent a "done" event eventually, but they won't affect
45 * the stream at all, and won't trigger callbacks. From the controller's point
46 * of view, the stream is completely inert when has completed.
22 */ 47 */
23 class StreamController<T> implements StreamSink<T> { 48 class StreamController<T> implements StreamSink<T> {
24 final _StreamImpl<T> stream; 49 final _StreamImpl<T> stream;
25 50
26 /** 51 /**
27 * A controller with a broadcast [stream].. 52 * A controller with a broadcast [stream]..
28 * 53 *
29 * The [onPauseStateChange] function is called when the stream becomes 54 * The [onPauseStateChange] function is called when the stream becomes
30 * paused or resumes after being paused. The current pause state can 55 * paused or resumes after being paused. The current pause state can
31 * be read from [isPaused]. Ignored if [:null:]. 56 * be read from [isPaused]. Ignored if [:null:].
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 90
66 /** 91 /**
67 * Whether the stream is closed for adding more events. 92 * Whether the stream is closed for adding more events.
68 * 93 *
69 * If true, the "done" event might not have fired yet, but it has been 94 * If true, the "done" event might not have fired yet, but it has been
70 * scheduled, and it is too late to add more events. 95 * scheduled, and it is too late to add more events.
71 */ 96 */
72 bool get isClosed => stream._isClosed; 97 bool get isClosed => stream._isClosed;
73 98
74 /** Whether one or more active subscribers have requested a pause. */ 99 /** Whether one or more active subscribers have requested a pause. */
75 bool get isPaused => stream._isPaused; 100 bool get isPaused => stream._isControllerPaused;
76 101
77 /** Whether there are currently any subscribers on this [Stream]. */ 102 /** Whether there are currently any subscribers on this [Stream]. */
78 bool get hasSubscribers => stream._hasSubscribers; 103 bool get hasSubscribers => stream._hasSubscribers;
79 104
80 /** 105 /**
81 * Send or queue a data event. 106 * Send or queue a data event.
82 */ 107 */
83 void add(T value) => stream._add(value); 108 void add(T value) => stream._add(value);
84 109
85 /** 110 /**
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 _SingleControllerStream(this._subscriptionHandler, this._pauseHandler); 162 _SingleControllerStream(this._subscriptionHandler, this._pauseHandler);
138 163
139 void _onSubscriptionStateChange() { 164 void _onSubscriptionStateChange() {
140 if (_subscriptionHandler != null) _subscriptionHandler(); 165 if (_subscriptionHandler != null) _subscriptionHandler();
141 } 166 }
142 167
143 void _onPauseStateChange() { 168 void _onPauseStateChange() {
144 if (_pauseHandler != null) _pauseHandler(); 169 if (_pauseHandler != null) _pauseHandler();
145 } 170 }
146 } 171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698