| 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 | |
| 85 /** | 7 /** |
| 86 * A count-down timer that can be configured to fire once or repeatedly. | 8 * A count-down timer that can be configured to fire once or repeatedly. |
| 87 * | 9 * |
| 88 * The timer counts down from the specified duration to 0. | 10 * The timer counts down from the specified duration to 0. |
| 89 * When the timer reaches 0, the timer invokes the specified callback function. | 11 * When the timer reaches 0, the timer invokes the specified callback function. |
| 90 * Use a periodic timer to repeatedly count down the same interval. | 12 * Use a periodic timer to repeatedly count down the same interval. |
| 91 * | 13 * |
| 92 * A negative duration is treated the same as a duration of 0. | 14 * A negative duration is treated the same as a duration of 0. |
| 93 * If the duration is statically known to be 0, consider using [run]. | 15 * If the duration is statically known to be 0, consider using [run]. |
| 94 * | 16 * |
| (...skipping 23 matching lines...) Expand all Loading... |
| 118 /** | 40 /** |
| 119 * Creates a new timer. | 41 * Creates a new timer. |
| 120 * | 42 * |
| 121 * The [callback] function is invoked after the given [duration]. | 43 * The [callback] function is invoked after the given [duration]. |
| 122 * | 44 * |
| 123 */ | 45 */ |
| 124 factory Timer(Duration duration, void callback()) { | 46 factory Timer(Duration duration, void callback()) { |
| 125 if (Zone.current == Zone.ROOT) { | 47 if (Zone.current == Zone.ROOT) { |
| 126 // No need to bind the callback. We know that the root's timer will | 48 // No need to bind the callback. We know that the root's timer will |
| 127 // be invoked in the root zone. | 49 // be invoked in the root zone. |
| 128 return Timer._createTimer(duration, callback); | 50 return Zone.current.createTimer(duration, callback); |
| 129 } | 51 } |
| 130 return Zone.current.createTimer(duration, callback); | 52 return Zone.current.createTimer( |
| 131 } | 53 duration, Zone.current.bindCallback(callback, runGuarded: true)); |
| 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); | |
| 137 } | 54 } |
| 138 | 55 |
| 139 /** | 56 /** |
| 140 * Creates a new repeating timer. | 57 * Creates a new repeating timer. |
| 141 * | 58 * |
| 142 * The [callback] is invoked repeatedly with [duration] intervals until | 59 * The [callback] is invoked repeatedly with [duration] intervals until |
| 143 * canceled with the [cancel] function. | 60 * canceled with the [cancel] function. |
| 144 * | 61 * |
| 145 * The exact timing depends on the underlying timer implementation. | 62 * The exact timing depends on the underlying timer implementation. |
| 146 * No more than `n` callbacks will be made in `duration * n` time, | 63 * No more than `n` callbacks will be made in `duration * n` time, |
| 147 * but the time between two consecutive callbacks | 64 * but the time between two consecutive callbacks |
| 148 * can be shorter and longer than `duration`. | 65 * can be shorter and longer than `duration`. |
| 149 * | 66 * |
| 150 * In particular, an implementation may schedule the next callback, e.g., | 67 * In particular, an implementation may schedule the next callback, e.g., |
| 151 * a `duration` after either when the previous callback ended, | 68 * a `duration` after either when the previous callback ended, |
| 152 * when the previous callback started, or when the previous callback was | 69 * when the previous callback started, or when the previous callback was |
| 153 * scheduled for - even if the actual callback was delayed. | 70 * scheduled for - even if the actual callback was delayed. |
| 154 */ | 71 */ |
| 155 factory Timer.periodic(Duration duration, | 72 factory Timer.periodic(Duration duration, |
| 156 void callback(Timer timer)) { | 73 void callback(Timer timer)) { |
| 157 if (Zone.current == Zone.ROOT) { | 74 if (Zone.current == Zone.ROOT) { |
| 158 // No need to bind the callback. We know that the root's timer will | 75 // No need to bind the callback. We know that the root's timer will |
| 159 // be invoked in the root zone. | 76 // be invoked in the root zone. |
| 160 return Timer._createPeriodicTimer(duration, callback); | 77 return Zone.current.createPeriodicTimer(duration, callback); |
| 161 } | 78 } |
| 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) { | |
| 194 // TODO(floitsch): the return type should be 'void', and the type | 79 // TODO(floitsch): the return type should be 'void', and the type |
| 195 // should be inferred. | 80 // should be inferred. |
| 196 ZoneUnaryCallback<dynamic, Timer> registeredCallback = | 81 var boundCallback = Zone.current.bindUnaryCallback/*<dynamic, Timer>*/( |
| 197 identical(_ROOT_ZONE, zone) | 82 callback, runGuarded: true); |
| 198 ? specification.callback | 83 return Zone.current.createPeriodicTimer(duration, boundCallback); |
| 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); | |
| 215 } | 84 } |
| 216 | 85 |
| 217 /** | 86 /** |
| 218 * Runs the given [callback] asynchronously as soon as possible. | 87 * Runs the given [callback] asynchronously as soon as possible. |
| 219 * | 88 * |
| 220 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. | 89 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. |
| 221 */ | 90 */ |
| 222 static void run(void callback()) { | 91 static void run(void callback()) { |
| 223 new Timer(Duration.ZERO, callback); | 92 new Timer(Duration.ZERO, callback); |
| 224 } | 93 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 236 * | 105 * |
| 237 * A periodic timer is active if it has not been canceled. | 106 * A periodic timer is active if it has not been canceled. |
| 238 */ | 107 */ |
| 239 bool get isActive; | 108 bool get isActive; |
| 240 | 109 |
| 241 external static Timer _createTimer(Duration duration, void callback()); | 110 external static Timer _createTimer(Duration duration, void callback()); |
| 242 external static Timer _createPeriodicTimer(Duration duration, | 111 external static Timer _createPeriodicTimer(Duration duration, |
| 243 void callback(Timer timer)); | 112 void callback(Timer timer)); |
| 244 } | 113 } |
| 245 | 114 |
| OLD | NEW |