| 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 event values 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 * If [computation] is omitted the event values will all be `null`. |
| 93 */ |
| 94 factory Stream.periodic(Duration period, |
| 95 [T computation(int computationCount)]) { |
| 96 if (computation == null) computation = ((i) => null); |
| 97 |
| 98 Timer timer; |
| 99 int computationCount = 0; |
| 100 StreamController<T> controller; |
| 101 // Counts the time that the Stream was running (and not paused). |
| 102 Stopwatch watch = new Stopwatch(); |
| 103 |
| 104 void sendEvent() { |
| 105 watch.reset(); |
| 106 T data = computation(computationCount++); |
| 107 controller.add(data); |
| 108 } |
| 109 |
| 110 void startPeriodicTimer() { |
| 111 assert(timer == null); |
| 112 timer = new Timer.periodic(period, (Timer timer) { |
| 113 sendEvent(); |
| 114 }); |
| 115 } |
| 116 |
| 117 controller = new StreamController<T>( |
| 118 onPauseStateChange: () { |
| 119 if (controller.isPaused) { |
| 120 timer.cancel(); |
| 121 timer = null; |
| 122 watch.stop(); |
| 123 } else { |
| 124 assert(timer == null); |
| 125 Duration elapsed = watch.elapsed; |
| 126 watch.start(); |
| 127 timer = new Timer(period - elapsed, () { |
| 128 timer = null; |
| 129 startPeriodicTimer(); |
| 130 sendEvent(); |
| 131 }); |
| 132 } |
| 133 }, |
| 134 onSubscriptionStateChange: () { |
| 135 if (controller.hasSubscribers) { |
| 136 watch.start(); |
| 137 startPeriodicTimer(); |
| 138 } else { |
| 139 if (timer != null) timer.cancel(); |
| 140 timer = null; |
| 141 } |
| 142 }); |
| 143 return controller.stream; |
| 144 } |
| 145 |
| 146 /** |
| 86 * Reports whether this stream is a broadcast stream. | 147 * Reports whether this stream is a broadcast stream. |
| 87 */ | 148 */ |
| 88 bool get isBroadcast => false; | 149 bool get isBroadcast => false; |
| 89 | 150 |
| 90 /** | 151 /** |
| 91 * Returns a multi-subscription stream that produces the same events as this. | 152 * Returns a multi-subscription stream that produces the same events as this. |
| 92 * | 153 * |
| 93 * If this stream is single-subscription, return a new stream that allows | 154 * If this stream is single-subscription, return a new stream that allows |
| 94 * multiple subscribers. It will subscribe to this stream when its first | 155 * multiple subscribers. It will subscribe to this stream when its first |
| 95 * subscriber is added, and unsubscribe again when the last subscription is | 156 * 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 | 1188 |
| 1128 /* TODO(8997): Implement EventSink instead, */ | 1189 /* TODO(8997): Implement EventSink instead, */ |
| 1129 class _EventOutputSinkWrapper<T> extends StreamSink<T> { | 1190 class _EventOutputSinkWrapper<T> extends StreamSink<T> { |
| 1130 _EventOutputSink _sink; | 1191 _EventOutputSink _sink; |
| 1131 _EventOutputSinkWrapper(this._sink); | 1192 _EventOutputSinkWrapper(this._sink); |
| 1132 | 1193 |
| 1133 void add(T data) { _sink._sendData(data); } | 1194 void add(T data) { _sink._sendData(data); } |
| 1134 void addError(AsyncError error) { _sink._sendError(error); } | 1195 void addError(AsyncError error) { _sink._sendError(error); } |
| 1135 void close() { _sink._sendDone(); } | 1196 void close() { _sink._sendDone(); } |
| 1136 } | 1197 } |
| OLD | NEW |