| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 part of quiver.async; | |
| 16 | |
| 17 /** | |
| 18 * A simple countdown timer that fires events in regular increments until a | |
| 19 * duration has passed. | |
| 20 * | |
| 21 * CountdownTimer implements [Stream] and sends itself as the event. From the | |
| 22 * timer you can get the [remaining] and [elapsed] time, or [cancel] the timer. | |
| 23 */ | |
| 24 class CountdownTimer extends Stream<CountdownTimer> { | |
| 25 static const _THRESHOLD_MS = 4; | |
| 26 | |
| 27 final Duration _duration; | |
| 28 final Duration _increment; | |
| 29 final Stopwatch _stopwatch; | |
| 30 final StreamController<CountdownTimer> _controller; | |
| 31 Timer _timer; | |
| 32 | |
| 33 /** | |
| 34 * Creates a new [CountdownTimer] that fires events in increments of | |
| 35 * [increment], until the [duration] has passed. | |
| 36 * | |
| 37 * [stopwatch] is for testing purposes. If you're using CountdownTimer and | |
| 38 * need to control time in a test, pass a mock or a fake. See [FakeAsync] and | |
| 39 * [FakeStopwatch]. | |
| 40 */ | |
| 41 CountdownTimer(Duration duration, Duration increment, {Stopwatch stopwatch}) | |
| 42 : _duration = duration, | |
| 43 _increment = increment, | |
| 44 _stopwatch = stopwatch == null ? new Stopwatch() : stopwatch, | |
| 45 _controller = new StreamController<CountdownTimer>.broadcast( | |
| 46 sync: true) { | |
| 47 _timer = new Timer.periodic(increment, _tick); | |
| 48 _stopwatch.start(); | |
| 49 } | |
| 50 | |
| 51 StreamSubscription<CountdownTimer> listen(void onData(CountdownTimer event), | |
| 52 {Function onError, void onDone(), bool cancelOnError}) => | |
| 53 _controller.stream.listen(onData, onError: onError, onDone: onDone); | |
| 54 | |
| 55 Duration get elapsed => _stopwatch.elapsed; | |
| 56 | |
| 57 Duration get remaining => _duration - _stopwatch.elapsed; | |
| 58 | |
| 59 bool get isRunning => _stopwatch.isRunning; | |
| 60 | |
| 61 cancel() { | |
| 62 _stopwatch.stop(); | |
| 63 _timer.cancel(); | |
| 64 _controller.close(); | |
| 65 } | |
| 66 | |
| 67 _tick(Timer timer) { | |
| 68 var t = remaining; | |
| 69 _controller.add(this); | |
| 70 // timers may have a 4ms resolution | |
| 71 if (t.inMilliseconds < _THRESHOLD_MS) { | |
| 72 cancel(); | |
| 73 } | |
| 74 } | |
| 75 } | |
| OLD | NEW |