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

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

Issue 12381064: Add Stream.periodic. (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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // Core Stream types 8 // Core Stream types
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 10
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 70
71 /** 71 /**
72 * Creates a single-subscription stream that gets its data from [data]. 72 * Creates a single-subscription stream that gets its data from [data].
73 */ 73 */
74 factory Stream.fromIterable(Iterable<T> data) { 74 factory Stream.fromIterable(Iterable<T> data) {
75 _PendingEvents iterableEvents = new _IterablePendingEvents<T>(data); 75 _PendingEvents iterableEvents = new _IterablePendingEvents<T>(data);
76 return new _GeneratedSingleStreamImpl<T>(iterableEvents); 76 return new _GeneratedSingleStreamImpl<T>(iterableEvents);
77 } 77 }
78 78
79 /** 79 /**
80 * Creates a stream that repeatedly emits events at [period] intervals.
81 *
82 * The sent event is a [Duration] object that represents the time between
Sean Eagan 2013/03/04 17:02:14 Why give the actual Duration for Stream.periodic b
floitsch 2013/03/14 13:27:34 Changed to be consistent. Both emit "null" now unl
83 * this and the last event. The duration can be both longer than
Lasse Reichstein Nielsen 2013/03/04 08:43:04 last -> previous. Add "and shorter" to match the "
floitsch 2013/03/13 19:26:55 I don't think it can be shorter anymore. Removed t
84 * the initial [period] if the machine was not able to
85 * execute the event early enough (for example, due to load), or if the
86 * Stream had been paused.
Lasse Reichstein Nielsen 2013/03/04 08:43:04 had been -> was
floitsch 2013/03/13 19:26:55 Done.
87 *
88 * The first event is emitted after waiting for the initial period.
Lasse Reichstein Nielsen 2013/03/04 08:43:04 After what? The stream being created or the first
floitsch 2013/03/13 19:26:55 reworded.
89 */
90 factory Stream.periodic(Duration period) {
91 Timer timer;
92 StreamController controller;
Lasse Reichstein Nielsen 2013/03/04 08:43:04 Consider making a specialized subclass of _SingleS
floitsch 2013/03/13 19:26:55 As discussed: keeping it as is. Do you want me to
Lasse Reichstein Nielsen 2013/03/14 06:54:46 No, it's fine. We would only be saving some extra
93 // userWatch counts the time that the Stream was running (and not paused).
94 Stopwatch userWatch = new Stopwatch();
95 // realWatch counts the actual time it took for the event.
96 Stopwatch realWatch = new Stopwatch();
97
98 void sendEvent() {
99 Duration elapsed = realWatch.elapsed;
100 realWatch.reset();
101 userWatch.reset();
102 controller.add(elapsed);
103 }
104
105 void startRepeatingTimer() {
106 assert(timer == null);
107 timer = new Timer.repeating(period, (Timer timer) {
108 sendEvent();
109 });
110 }
111
112 controller = new StreamController(
113 onPauseStateChange: () {
114 if (controller.isPaused) {
115 timer.cancel();
116 timer = null;
117 userWatch.stop();
118 } else {
119 assert(timer == null);
Lasse Reichstein Nielsen 2013/03/04 08:43:04 Why not fire immediately if realWatch.elapsedMilli
floitsch 2013/03/13 19:26:55 As discussed: keeping the current semantics.
120 Duration elapsed = userWatch.elapsed;
121 userWatch.start();
122 timer = new Timer(period - elapsed, () {
123 timer = null;
124 startRepeatingTimer();
125 sendEvent();
126 });
127 }
128 },
129 onSubscriptionStateChange: () {
130 if (controller.hasSubscribers) {
131 realWatch.start();
132 userWatch.start();
133 startRepeatingTimer();
134 } else {
135 if (timer != null) timer.cancel();
136 }
137 });
138 return controller.stream;
139 }
140
141 /**
80 * Reports whether this stream is a broadcast stream. 142 * Reports whether this stream is a broadcast stream.
81 */ 143 */
82 bool get isBroadcast => false; 144 bool get isBroadcast => false;
83 145
84 /** 146 /**
85 * Returns a multi-subscription stream that produces the same events as this. 147 * Returns a multi-subscription stream that produces the same events as this.
86 * 148 *
87 * If this stream is single-subscription, return a new stream that allows 149 * If this stream is single-subscription, return a new stream that allows
88 * multiple subscribers. It will subscribe to this stream when its first 150 * multiple subscribers. It will subscribe to this stream when its first
89 * subscriber is added, and unsubscribe again when the last subscription is 151 * subscriber is added, and unsubscribe again when the last subscription is
(...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 } 1168 }
1107 1169
1108 class _StreamOutputSinkWrapper<T> implements StreamSink<T> { 1170 class _StreamOutputSinkWrapper<T> implements StreamSink<T> {
1109 _StreamOutputSink _sink; 1171 _StreamOutputSink _sink;
1110 _StreamOutputSinkWrapper(this._sink); 1172 _StreamOutputSinkWrapper(this._sink);
1111 1173
1112 void add(T data) => _sink._sendData(data); 1174 void add(T data) => _sink._sendData(data);
1113 void signalError(AsyncError error) => _sink._sendError(error); 1175 void signalError(AsyncError error) => _sink._sendError(error);
1114 void close() => _sink._sendDone(); 1176 void close() => _sink._sendDone();
1115 } 1177 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698