Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 | 76 |
| 77 /** | 77 /** |
| 78 * Creates a single-subscription stream that gets its data from [data]. | 78 * Creates a single-subscription stream that gets its data from [data]. |
| 79 */ | 79 */ |
| 80 factory Stream.fromIterable(Iterable<T> data) { | 80 factory Stream.fromIterable(Iterable<T> data) { |
| 81 _PendingEvents iterableEvents = new _IterablePendingEvents<T>(data); | 81 _PendingEvents iterableEvents = new _IterablePendingEvents<T>(data); |
| 82 return new _GeneratedSingleStreamImpl<T>(iterableEvents); | 82 return new _GeneratedSingleStreamImpl<T>(iterableEvents); |
| 83 } | 83 } |
| 84 | 84 |
| 85 /** | 85 /** |
| 86 * Creates a stream that repeatedly emits events at [period] intervals. | |
| 87 * | |
| 88 * The events are computed by invoking [computation]. The argument to | |
| 89 * this callback is an integer that starts with 0 and is incremented for | |
| 90 * every event. | |
| 91 * | |
| 92 * By default, the computation always returns `null`. | |
|
Lasse Reichstein Nielsen
2013/03/14 06:54:46
Above: The event values are computed ....
and here
floitsch
2013/03/14 13:27:34
Done.
| |
| 93 * | |
| 94 * When a listener subscribes the stream starts by waiting for the given | |
| 95 * [period]. After this duration has elapsed it emits the first event. | |
|
Lasse Reichstein Nielsen
2013/03/14 06:54:46
When will the next event be emitted? [period] afte
floitsch
2013/03/14 13:27:34
Removed the lines. Not adding anything.
| |
| 96 */ | |
| 97 factory Stream.periodic(Duration period, | |
| 98 [T computation(int computationCount)]) { | |
| 99 if (computation == null) computation = ((i) => null); | |
| 100 | |
| 101 Timer timer; | |
| 102 int computationCount = 0; | |
| 103 StreamController<T> controller; | |
| 104 // Counts the time that the Stream was running (and not paused). | |
| 105 Stopwatch watch = new Stopwatch(); | |
| 106 | |
| 107 void sendEvent() { | |
| 108 watch.reset(); | |
| 109 T data = computation(computationCount++); | |
| 110 controller.add(data); | |
| 111 } | |
| 112 | |
| 113 void startPeriodicTimer() { | |
| 114 assert(timer == null); | |
| 115 timer = new Timer.periodic(period, (Timer timer) { | |
| 116 sendEvent(); | |
| 117 }); | |
| 118 } | |
| 119 | |
| 120 controller = new StreamController<T>( | |
| 121 onPauseStateChange: () { | |
| 122 if (controller.isPaused) { | |
| 123 timer.cancel(); | |
| 124 timer = null; | |
| 125 watch.stop(); | |
| 126 } else { | |
| 127 assert(timer == null); | |
| 128 Duration elapsed = watch.elapsed; | |
| 129 watch.start(); | |
| 130 timer = new Timer(period - elapsed, () { | |
| 131 timer = null; | |
| 132 startPeriodicTimer(); | |
| 133 sendEvent(); | |
| 134 }); | |
| 135 } | |
| 136 }, | |
| 137 onSubscriptionStateChange: () { | |
| 138 if (controller.hasSubscribers) { | |
| 139 watch.start(); | |
| 140 startPeriodicTimer(); | |
| 141 } else { | |
| 142 if (timer != null) timer.cancel(); | |
|
Lasse Reichstein Nielsen
2013/03/14 06:54:46
set timer to null here?
Otherwise unsubscribing an
floitsch
2013/03/14 13:27:34
done.
| |
| 143 } | |
| 144 }); | |
| 145 return controller.stream; | |
| 146 } | |
| 147 | |
| 148 /** | |
| 86 * Reports whether this stream is a broadcast stream. | 149 * Reports whether this stream is a broadcast stream. |
| 87 */ | 150 */ |
| 88 bool get isBroadcast => false; | 151 bool get isBroadcast => false; |
| 89 | 152 |
| 90 /** | 153 /** |
| 91 * Returns a multi-subscription stream that produces the same events as this. | 154 * Returns a multi-subscription stream that produces the same events as this. |
| 92 * | 155 * |
| 93 * If this stream is single-subscription, return a new stream that allows | 156 * If this stream is single-subscription, return a new stream that allows |
| 94 * multiple subscribers. It will subscribe to this stream when its first | 157 * multiple subscribers. It will subscribe to this stream when its first |
| 95 * subscriber is added, and unsubscribe again when the last subscription is | 158 * subscriber is added, and unsubscribe again when the last subscription is |
| (...skipping 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1127 | 1190 |
| 1128 /* TODO(8997): Implement EventSink instead, */ | 1191 /* TODO(8997): Implement EventSink instead, */ |
| 1129 class _EventOutputSinkWrapper<T> extends StreamSink<T> { | 1192 class _EventOutputSinkWrapper<T> extends StreamSink<T> { |
| 1130 _EventOutputSink _sink; | 1193 _EventOutputSink _sink; |
| 1131 _EventOutputSinkWrapper(this._sink); | 1194 _EventOutputSinkWrapper(this._sink); |
| 1132 | 1195 |
| 1133 void add(T data) { _sink._sendData(data); } | 1196 void add(T data) { _sink._sendData(data); } |
| 1134 void addError(AsyncError error) { _sink._sendError(error); } | 1197 void addError(AsyncError error) { _sink._sendError(error); } |
| 1135 void close() { _sink._sendDone(); } | 1198 void close() { _sink._sendDone(); } |
| 1136 } | 1199 } |
| OLD | NEW |