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

Side by Side Diff: packages/quiver/lib/src/async/metronome.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
OLDNEW
1 // Copyright 2014 Google Inc. All Rights Reserved. 1 // Copyright 2014 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 stream of [DateTime] events at [interval]s centered on [anchor].
18 * A stream of [DateTime] events at [interval]s centered on [anchor]. 18 ///
19 * 19 /// This stream accounts for drift but only guarantees that events are
20 * This stream accounts for drift but only guarantees that events are 20 /// delivered on or after the interval. If the system is busy for longer than
21 * delivered on or after the interval. If the system is busy for longer than 21 /// two [interval]s, only one will be delivered.
22 * two [interval]s, only one will be delivered. 22 ///
23 * 23 /// [anchor] defaults to [clock.now], which means the stream represents a
24 * [anchor] defaults to [clock.now], which means the stream represents a 24 /// self-correcting periodic timer. If anchor is the epoch, then the stream is
25 * self-correcting periodic timer. If anchor is the epoch, then the stream is 25 /// synchronized to wall-clock time. It can be anchored anywhere in time, but
26 * synchronized to wall-clock time. It can be anchored anywhere in time, but 26 /// this does not delay the first delivery.
27 * this does not delay the first delivery. 27 ///
28 * 28 /// Examples:
29 * Examples: 29 ///
30 * 30 /// new Metronome.epoch(aMinute).listen((d) => print(d));
31 * new Metronome.epoch(aMinute).listen((d) => print(d)); 31 ///
32 * 32 /// Could print the following stream of events, anchored by epoch, till the
33 * Could print the following stream of events, anchored by epoch, 33 /// stream is canceled:
34 * till the stream is canceled: 34 /// 2014-05-04 14:06:00.001
35 * 2014-05-04 14:06:00.001 35 /// 2014-05-04 14:07:00.000
36 * 2014-05-04 14:07:00.000 36 /// 2014-05-04 14:08:00.003
37 * 2014-05-04 14:08:00.003 37 /// ...
38 * ... 38 ///
39 * 39 /// Example anchored in the future (now = 2014-05-05 20:06:00.123)
40 * Example anchored in the future (now = 2014-05-05 20:06:00.123) 40 /// new IsochronousStream.periodic(aMillisecond * 100,
41 * new IsochronousStream.periodic(aMillisecond * 100, 41 /// anchorMs: DateTime.parse("2014-05-05 21:07:00"))
42 * anchorMs: DateTime.parse("2014-05-05 21:07:00")) 42 /// .listen(print);
43 * .listen((d) => print(d)); 43 ///
44 * 44 /// 2014-05-04 20:06:00.223
45 * 2014-05-04 20:06:00.223 45 /// 2014-05-04 20:06:00.324
46 * 2014-05-04 20:06:00.324 46 /// 2014-05-04 20:06:00.423
47 * 2014-05-04 20:06:00.423 47 /// ...
48 * ...
49 */
50 class Metronome extends Stream<DateTime> { 48 class Metronome extends Stream<DateTime> {
51 static final DateTime _EPOCH = new DateTime.fromMillisecondsSinceEpoch(0); 49 static final DateTime _EPOCH = new DateTime.fromMillisecondsSinceEpoch(0);
52 50
53 final Clock clock; 51 final Clock clock;
54 final Duration interval; 52 final Duration interval;
55 final DateTime anchor; 53 final DateTime anchor;
56 54
57 Timer _timer; 55 Timer _timer;
58 StreamController _controller; 56 StreamController<DateTime> _controller;
59 final int _intervalMs; 57 final int _intervalMs;
60 final int _anchorMs; 58 final int _anchorMs;
61 59
62 bool get isBroadcast => true; 60 bool get isBroadcast => true;
63 61
64 Metronome.epoch(Duration interval, {Clock clock: const Clock()}) 62 Metronome.epoch(Duration interval, {Clock clock: const Clock()})
65 : this._(interval, clock: clock, anchor: _EPOCH); 63 : this._(interval, clock: clock, anchor: _EPOCH);
66 64
67 Metronome.periodic(Duration interval, 65 Metronome.periodic(Duration interval,
68 {Clock clock: const Clock(), DateTime anchor}) 66 {Clock clock: const Clock(), DateTime anchor})
69 : this._(interval, clock: clock, anchor: anchor); 67 : this._(interval, clock: clock, anchor: anchor);
70 68
71 Metronome._(Duration interval, {Clock clock: const Clock(), DateTime anchor}) 69 Metronome._(Duration interval, {Clock clock: const Clock(), DateTime anchor})
72 : this.clock = clock, 70 : this.clock = clock,
73 this.anchor = anchor, 71 this.anchor = anchor,
74 this.interval = interval, 72 this.interval = interval,
75 this._intervalMs = interval.inMilliseconds, 73 this._intervalMs = interval.inMilliseconds,
76 this._anchorMs = (anchor == null 74 this._anchorMs =
77 ? clock.now() 75 (anchor == null ? clock.now() : anchor).millisecondsSinceEpoch {
78 : anchor).millisecondsSinceEpoch {
79 _controller = new StreamController<DateTime>.broadcast( 76 _controller = new StreamController<DateTime>.broadcast(
80 sync: true, onCancel: () { 77 sync: true,
81 _timer.cancel(); 78 onCancel: () {
82 }, onListen: () { 79 _timer.cancel();
83 _startTimer(clock.now()); 80 },
84 }); 81 onListen: () {
82 _startTimer(clock.now());
83 });
85 } 84 }
86 85
87 StreamSubscription<DateTime> listen(void onData(DateTime event), 86 StreamSubscription<DateTime> listen(void onData(DateTime event),
88 {Function onError, void onDone(), bool cancelOnError}) => 87 {Function onError, void onDone(), bool cancelOnError}) =>
89 _controller.stream.listen(onData, 88 _controller.stream.listen(onData,
90 onError: onError, onDone: onDone, cancelOnError: cancelOnError); 89 onError: onError, onDone: onDone, cancelOnError: cancelOnError);
91 90
92 _startTimer(DateTime now) { 91 _startTimer(DateTime now) {
93 var delay = 92 var delay =
94 _intervalMs - ((now.millisecondsSinceEpoch - _anchorMs) % _intervalMs); 93 _intervalMs - ((now.millisecondsSinceEpoch - _anchorMs) % _intervalMs);
95 _timer = new Timer(new Duration(milliseconds: delay), _tickDate); 94 _timer = new Timer(new Duration(milliseconds: delay), _tickDate);
96 } 95 }
97 96
98 _tickDate() { 97 _tickDate() {
99 // Hey now, what's all this hinky clock.now() calls? Simple, if the workers 98 // Hey now, what's all this hinky clock.now() calls? Simple, if the workers
100 // on the receiving end of _controller.add() take a non-zero amount of time 99 // on the receiving end of _controller.add() take a non-zero amount of time
101 // to do their thing (e.g. rendering a large scene with canvas), the next 100 // to do their thing (e.g. rendering a large scene with canvas), the next
102 // timer must be adjusted to account for the lapsed time. 101 // timer must be adjusted to account for the lapsed time.
103 _controller.add(clock.now()); 102 _controller.add(clock.now());
104 _startTimer(clock.now()); 103 _startTimer(clock.now());
105 } 104 }
106 } 105 }
OLDNEW
« no previous file with comments | « packages/quiver/lib/src/async/iteration.dart ('k') | packages/quiver/lib/src/async/stream_buffer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698