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

Side by Side Diff: packages/quiver/lib/src/async/countdown_timer.dart

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

Powered by Google App Engine
This is Rietveld 408576698