OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 abstract class _TimerTask implements Timer { |
| 8 final Zone _zone; |
| 9 final Timer _nativeTimer; |
| 10 |
| 11 _TimerTask(this._nativeTimer, this._zone); |
| 12 |
| 13 void cancel() { |
| 14 _nativeTimer.cancel(); |
| 15 } |
| 16 |
| 17 bool get isActive => _nativeTimer.isActive; |
| 18 } |
| 19 |
| 20 class _SingleShotTimerTask extends _TimerTask { |
| 21 // TODO(floitsch): the generic argument should be 'void'. |
| 22 final ZoneCallback<dynamic> _callback; |
| 23 |
| 24 _SingleShotTimerTask(Timer timer, this._callback, Zone zone) |
| 25 : super(timer, zone); |
| 26 } |
| 27 |
| 28 class _PeriodicTimerTask extends _TimerTask { |
| 29 // TODO(floitsch): the first generic argument should be 'void'. |
| 30 final ZoneUnaryCallback<dynamic, Timer> _callback; |
| 31 |
| 32 _PeriodicTimerTask(Timer timer, this._callback, Zone zone) |
| 33 : super(timer, zone); |
| 34 } |
| 35 |
| 36 /** |
| 37 * A task specification for a single-shot timer. |
| 38 * |
| 39 * *Experimental*. Might disappear without notice. |
| 40 */ |
| 41 class SingleShotTimerTaskSpecification implements TaskSpecification { |
| 42 static const String specificationName = "dart.async.timer"; |
| 43 |
| 44 /** The duration after which the timer should invoke the [callback]. */ |
| 45 final Duration duration; |
| 46 |
| 47 /** The callback that should be run when the timer triggers. */ |
| 48 // TODO(floitsch): the generic argument should be void. |
| 49 final ZoneCallback<dynamic> callback; |
| 50 |
| 51 SingleShotTimerTaskSpecification(this.duration, void this.callback()); |
| 52 |
| 53 @override |
| 54 String get name => specificationName; |
| 55 |
| 56 @override |
| 57 bool get isOneShot => true; |
| 58 } |
| 59 |
| 60 /** |
| 61 * A task specification for a periodic timer. |
| 62 * |
| 63 * *Experimental*. Might disappear without notice. |
| 64 */ |
| 65 class PeriodicTimerTaskSpecification implements TaskSpecification { |
| 66 static const String specificationName = "dart.async.periodic-timer"; |
| 67 |
| 68 /** The interval at which the periodic timer should invoke the [callback]. */ |
| 69 final Duration duration; |
| 70 |
| 71 /** The callback that should be run when the timer triggers. */ |
| 72 // TODO(floitsch): the first generic argument should be void. |
| 73 final ZoneUnaryCallback<dynamic, Timer> callback; |
| 74 |
| 75 PeriodicTimerTaskSpecification( |
| 76 this.duration, void this.callback(Timer timer)); |
| 77 |
| 78 @override |
| 79 String get name => specificationName; |
| 80 |
| 81 @override |
| 82 bool get isOneShot => false; |
| 83 } |
| 84 |
7 /** | 85 /** |
8 * A count-down timer that can be configured to fire once or repeatedly. | 86 * A count-down timer that can be configured to fire once or repeatedly. |
9 * | 87 * |
10 * The timer counts down from the specified duration to 0. | 88 * The timer counts down from the specified duration to 0. |
11 * When the timer reaches 0, the timer invokes the specified callback function. | 89 * When the timer reaches 0, the timer invokes the specified callback function. |
12 * Use a periodic timer to repeatedly count down the same interval. | 90 * Use a periodic timer to repeatedly count down the same interval. |
13 * | 91 * |
14 * A negative duration is treated the same as a duration of 0. | 92 * A negative duration is treated the same as a duration of 0. |
15 * If the duration is statically known to be 0, consider using [run]. | 93 * If the duration is statically known to be 0, consider using [run]. |
16 * | 94 * |
(...skipping 23 matching lines...) Expand all Loading... |
40 /** | 118 /** |
41 * Creates a new timer. | 119 * Creates a new timer. |
42 * | 120 * |
43 * The [callback] function is invoked after the given [duration]. | 121 * The [callback] function is invoked after the given [duration]. |
44 * | 122 * |
45 */ | 123 */ |
46 factory Timer(Duration duration, void callback()) { | 124 factory Timer(Duration duration, void callback()) { |
47 if (Zone.current == Zone.ROOT) { | 125 if (Zone.current == Zone.ROOT) { |
48 // No need to bind the callback. We know that the root's timer will | 126 // No need to bind the callback. We know that the root's timer will |
49 // be invoked in the root zone. | 127 // be invoked in the root zone. |
50 return Zone.current.createTimer(duration, callback); | 128 return Timer._createTimer(duration, callback); |
51 } | 129 } |
52 return Zone.current.createTimer( | 130 return Zone.current.createTimer(duration, callback); |
53 duration, Zone.current.bindCallback(callback, runGuarded: true)); | 131 } |
| 132 |
| 133 factory Timer._task(Zone zone, Duration duration, void callback()) { |
| 134 SingleShotTimerTaskSpecification specification = |
| 135 new SingleShotTimerTaskSpecification(duration, callback); |
| 136 return zone.createTask(_createSingleShotTimerTask, specification); |
54 } | 137 } |
55 | 138 |
56 /** | 139 /** |
57 * Creates a new repeating timer. | 140 * Creates a new repeating timer. |
58 * | 141 * |
59 * The [callback] is invoked repeatedly with [duration] intervals until | 142 * The [callback] is invoked repeatedly with [duration] intervals until |
60 * canceled with the [cancel] function. | 143 * canceled with the [cancel] function. |
61 * | 144 * |
62 * The exact timing depends on the underlying timer implementation. | 145 * The exact timing depends on the underlying timer implementation. |
63 * No more than `n` callbacks will be made in `duration * n` time, | 146 * No more than `n` callbacks will be made in `duration * n` time, |
64 * but the time between two consecutive callbacks | 147 * but the time between two consecutive callbacks |
65 * can be shorter and longer than `duration`. | 148 * can be shorter and longer than `duration`. |
66 * | 149 * |
67 * In particular, an implementation may schedule the next callback, e.g., | 150 * In particular, an implementation may schedule the next callback, e.g., |
68 * a `duration` after either when the previous callback ended, | 151 * a `duration` after either when the previous callback ended, |
69 * when the previous callback started, or when the previous callback was | 152 * when the previous callback started, or when the previous callback was |
70 * scheduled for - even if the actual callback was delayed. | 153 * scheduled for - even if the actual callback was delayed. |
71 */ | 154 */ |
72 factory Timer.periodic(Duration duration, | 155 factory Timer.periodic(Duration duration, |
73 void callback(Timer timer)) { | 156 void callback(Timer timer)) { |
74 if (Zone.current == Zone.ROOT) { | 157 if (Zone.current == Zone.ROOT) { |
75 // No need to bind the callback. We know that the root's timer will | 158 // No need to bind the callback. We know that the root's timer will |
76 // be invoked in the root zone. | 159 // be invoked in the root zone. |
77 return Zone.current.createPeriodicTimer(duration, callback); | 160 return Timer._createPeriodicTimer(duration, callback); |
78 } | 161 } |
| 162 return Zone.current.createPeriodicTimer(duration, callback); |
| 163 } |
| 164 |
| 165 factory Timer._periodicTask(Zone zone, Duration duration, |
| 166 void callback(Timer timer)) { |
| 167 PeriodicTimerTaskSpecification specification = |
| 168 new PeriodicTimerTaskSpecification(duration, callback); |
| 169 return zone.createTask(_createPeriodicTimerTask, specification); |
| 170 } |
| 171 |
| 172 static Timer _createSingleShotTimerTask( |
| 173 SingleShotTimerTaskSpecification specification, Zone zone) { |
| 174 ZoneCallback registeredCallback = identical(_ROOT_ZONE, zone) |
| 175 ? specification.callback |
| 176 : zone.registerCallback(specification.callback); |
| 177 |
| 178 _TimerTask timerTask; |
| 179 |
| 180 Timer nativeTimer = Timer._createTimer(specification.duration, () { |
| 181 timerTask._zone.runTask(_runSingleShotCallback, timerTask, null); |
| 182 }); |
| 183 |
| 184 timerTask = new _SingleShotTimerTask(nativeTimer, registeredCallback, zone); |
| 185 return timerTask; |
| 186 } |
| 187 |
| 188 static void _runSingleShotCallback(_SingleShotTimerTask timerTask, Object _) { |
| 189 timerTask._callback(); |
| 190 } |
| 191 |
| 192 static Timer _createPeriodicTimerTask( |
| 193 PeriodicTimerTaskSpecification specification, Zone zone) { |
79 // TODO(floitsch): the return type should be 'void', and the type | 194 // TODO(floitsch): the return type should be 'void', and the type |
80 // should be inferred. | 195 // should be inferred. |
81 var boundCallback = Zone.current.bindUnaryCallback/*<dynamic, Timer>*/( | 196 ZoneUnaryCallback<dynamic, Timer> registeredCallback = |
82 callback, runGuarded: true); | 197 identical(_ROOT_ZONE, zone) |
83 return Zone.current.createPeriodicTimer(duration, boundCallback); | 198 ? specification.callback |
| 199 : zone.registerUnaryCallback/*<dynamic, Timer>*/( |
| 200 specification.callback); |
| 201 |
| 202 _TimerTask timerTask; |
| 203 |
| 204 Timer nativeTimer = |
| 205 Timer._createPeriodicTimer(specification.duration, (Timer _) { |
| 206 timerTask._zone.runTask(_runPeriodicCallback, timerTask, null); |
| 207 }); |
| 208 |
| 209 timerTask = new _PeriodicTimerTask(nativeTimer, registeredCallback, zone); |
| 210 return timerTask; |
| 211 } |
| 212 |
| 213 static void _runPeriodicCallback(_PeriodicTimerTask timerTask, Object _) { |
| 214 timerTask._callback(timerTask); |
84 } | 215 } |
85 | 216 |
86 /** | 217 /** |
87 * Runs the given [callback] asynchronously as soon as possible. | 218 * Runs the given [callback] asynchronously as soon as possible. |
88 * | 219 * |
89 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. | 220 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. |
90 */ | 221 */ |
91 static void run(void callback()) { | 222 static void run(void callback()) { |
92 new Timer(Duration.ZERO, callback); | 223 new Timer(Duration.ZERO, callback); |
93 } | 224 } |
(...skipping 11 matching lines...) Expand all Loading... |
105 * | 236 * |
106 * A periodic timer is active if it has not been canceled. | 237 * A periodic timer is active if it has not been canceled. |
107 */ | 238 */ |
108 bool get isActive; | 239 bool get isActive; |
109 | 240 |
110 external static Timer _createTimer(Duration duration, void callback()); | 241 external static Timer _createTimer(Duration duration, void callback()); |
111 external static Timer _createPeriodicTimer(Duration duration, | 242 external static Timer _createPeriodicTimer(Duration duration, |
112 void callback(Timer timer)); | 243 void callback(Timer timer)); |
113 } | 244 } |
114 | 245 |
OLD | NEW |