| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 * Creates a stream that repeatedly emits events at [period] intervals. | 132 * Creates a stream that repeatedly emits events at [period] intervals. |
| 133 * | 133 * |
| 134 * The event values are computed by invoking [computation]. The argument to | 134 * The event values are computed by invoking [computation]. The argument to |
| 135 * this callback is an integer that starts with 0 and is incremented for | 135 * this callback is an integer that starts with 0 and is incremented for |
| 136 * every event. | 136 * every event. |
| 137 * | 137 * |
| 138 * If [computation] is omitted the event values will all be `null`. | 138 * If [computation] is omitted the event values will all be `null`. |
| 139 */ | 139 */ |
| 140 factory Stream.periodic(Duration period, | 140 factory Stream.periodic(Duration period, |
| 141 [T computation(int computationCount)]) { | 141 [T computation(int computationCount)]) { |
| 142 if (computation == null) computation = ((i) => null); | |
| 143 | |
| 144 Timer timer; | 142 Timer timer; |
| 145 int computationCount = 0; | 143 int computationCount = 0; |
| 146 StreamController<T> controller; | 144 StreamController<T> controller; |
| 147 // Counts the time that the Stream was running (and not paused). | 145 // Counts the time that the Stream was running (and not paused). |
| 148 Stopwatch watch = new Stopwatch(); | 146 Stopwatch watch = new Stopwatch(); |
| 149 | 147 |
| 150 void sendEvent() { | 148 void sendEvent() { |
| 151 watch.reset(); | 149 watch.reset(); |
| 152 T data = computation(computationCount++); | 150 T data; |
| 151 if (computation != null) { |
| 152 try { |
| 153 data = computation(computationCount++); |
| 154 } catch (e, s) { |
| 155 controller.addError(e, s); |
| 156 return; |
| 157 } |
| 158 } |
| 153 controller.add(data); | 159 controller.add(data); |
| 154 } | 160 } |
| 155 | 161 |
| 156 void startPeriodicTimer() { | 162 void startPeriodicTimer() { |
| 157 assert(timer == null); | 163 assert(timer == null); |
| 158 timer = new Timer.periodic(period, (Timer timer) { | 164 timer = new Timer.periodic(period, (Timer timer) { |
| 159 sendEvent(); | 165 sendEvent(); |
| 160 }); | 166 }); |
| 161 } | 167 } |
| 162 | 168 |
| (...skipping 1452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1615 * onListen: () { | 1621 * onListen: () { |
| 1616 * subscription = input.listen((data) { | 1622 * subscription = input.listen((data) { |
| 1617 * // Duplicate the data. | 1623 * // Duplicate the data. |
| 1618 * controller.add(data); | 1624 * controller.add(data); |
| 1619 * controller.add(data); | 1625 * controller.add(data); |
| 1620 * }, | 1626 * }, |
| 1621 * onError: controller.addError, | 1627 * onError: controller.addError, |
| 1622 * onDone: controller.close, | 1628 * onDone: controller.close, |
| 1623 * cancelOnError: cancelOnError); | 1629 * cancelOnError: cancelOnError); |
| 1624 * }, | 1630 * }, |
| 1625 * onPause: () => subscription.pause(), | 1631 * onPause: () { subscription.pause(); }, |
| 1626 * onResume: () => subscription.resume(), | 1632 * onResume: () { subscription.resume(); }, |
| 1627 * onCancel: () => subscription.cancel(), | 1633 * onCancel: () { subscription.cancel(); }, |
| 1628 * sync: true); | 1634 * sync: true); |
| 1629 * return controller.stream.listen(null); | 1635 * return controller.stream.listen(null); |
| 1630 * }); | 1636 * }); |
| 1631 */ | 1637 */ |
| 1632 const factory StreamTransformer( | 1638 const factory StreamTransformer( |
| 1633 StreamSubscription<T> transformer(Stream<S> stream, bool cancelOnError)) | 1639 StreamSubscription<T> transformer(Stream<S> stream, bool cancelOnError)) |
| 1634 = _StreamSubscriptionTransformer; | 1640 = _StreamSubscriptionTransformer; |
| 1635 | 1641 |
| 1636 /** | 1642 /** |
| 1637 * Creates a [StreamTransformer] that delegates events to the given functions. | 1643 * Creates a [StreamTransformer] that delegates events to the given functions. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1735 class _ControllerEventSinkWrapper<T> implements EventSink<T> { | 1741 class _ControllerEventSinkWrapper<T> implements EventSink<T> { |
| 1736 EventSink _sink; | 1742 EventSink _sink; |
| 1737 _ControllerEventSinkWrapper(this._sink); | 1743 _ControllerEventSinkWrapper(this._sink); |
| 1738 | 1744 |
| 1739 void add(T data) { _sink.add(data); } | 1745 void add(T data) { _sink.add(data); } |
| 1740 void addError(error, [StackTrace stackTrace]) { | 1746 void addError(error, [StackTrace stackTrace]) { |
| 1741 _sink.addError(error, stackTrace); | 1747 _sink.addError(error, stackTrace); |
| 1742 } | 1748 } |
| 1743 void close() { _sink.close(); } | 1749 void close() { _sink.close(); } |
| 1744 } | 1750 } |
| OLD | NEW |