| 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 /** | 7 /** |
| 8 * 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. |
| 9 * | 9 * |
| 10 * The timer counts down from the specified duration to 0. | 10 * The timer counts down from the specified duration to 0. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 /** | 40 /** |
| 41 * Creates a new timer. | 41 * Creates a new timer. |
| 42 * | 42 * |
| 43 * The [callback] function is invoked after the given [duration]. | 43 * The [callback] function is invoked after the given [duration]. |
| 44 * | 44 * |
| 45 */ | 45 */ |
| 46 factory Timer(Duration duration, void callback()) { | 46 factory Timer(Duration duration, void callback()) { |
| 47 if (Zone.current == Zone.ROOT) { | 47 if (Zone.current == Zone.ROOT) { |
| 48 // 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 |
| 49 // be invoked in the root zone. | 49 // be invoked in the root zone. |
| 50 return Zone.current.createTimer(duration, callback); | 50 return Timer._createTimer(duration, callback); |
| 51 } | 51 } |
| 52 return Zone.current.createTimer( | 52 return Zone.current.createTimer(duration, callback); |
| 53 duration, Zone.current.bindCallback(callback, runGuarded: true)); | 53 } |
| 54 |
| 55 factory Timer._task(Zone zone, Duration duration, void callback()) { |
| 56 SingleShotTimerTaskSpecification specification = |
| 57 new SingleShotTimerTaskSpecification(duration, callback); |
| 58 Object task = |
| 59 Zone.current.createTask(specification, _createSingleShotTimerTask); |
| 60 return new _TimerTaskWrapper(task); |
| 54 } | 61 } |
| 55 | 62 |
| 56 /** | 63 /** |
| 57 * Creates a new repeating timer. | 64 * Creates a new repeating timer. |
| 58 * | 65 * |
| 59 * The [callback] is invoked repeatedly with [duration] intervals until | 66 * The [callback] is invoked repeatedly with [duration] intervals until |
| 60 * canceled with the [cancel] function. | 67 * canceled with the [cancel] function. |
| 61 * | 68 * |
| 62 * The exact timing depends on the underlying timer implementation. | 69 * The exact timing depends on the underlying timer implementation. |
| 63 * No more than `n` callbacks will be made in `duration * n` time, | 70 * No more than `n` callbacks will be made in `duration * n` time, |
| 64 * but the time between two consecutive callbacks | 71 * but the time between two consecutive callbacks |
| 65 * can be shorter and longer than `duration`. | 72 * can be shorter and longer than `duration`. |
| 66 * | 73 * |
| 67 * In particular, an implementation may schedule the next callback, e.g., | 74 * In particular, an implementation may schedule the next callback, e.g., |
| 68 * a `duration` after either when the previous callback ended, | 75 * a `duration` after either when the previous callback ended, |
| 69 * when the previous callback started, or when the previous callback was | 76 * when the previous callback started, or when the previous callback was |
| 70 * scheduled for - even if the actual callback was delayed. | 77 * scheduled for - even if the actual callback was delayed. |
| 71 */ | 78 */ |
| 72 factory Timer.periodic(Duration duration, | 79 factory Timer.periodic(Duration duration, |
| 73 void callback(Timer timer)) { | 80 void callback(Timer timer)) { |
| 74 if (Zone.current == Zone.ROOT) { | 81 if (Zone.current == Zone.ROOT) { |
| 75 // No need to bind the callback. We know that the root's timer will | 82 // No need to bind the callback. We know that the root's timer will |
| 76 // be invoked in the root zone. | 83 // be invoked in the root zone. |
| 77 return Zone.current.createPeriodicTimer(duration, callback); | 84 return Timer._createPeriodicTimer(duration, callback); |
| 78 } | 85 } |
| 79 return Zone.current.createPeriodicTimer( | 86 PeriodicTimerTaskSpecification specification = |
| 80 duration, Zone.current.bindUnaryCallback(callback, runGuarded: true)); | 87 new PeriodicTimerTaskSpecification(duration, callback); |
| 88 Object task = |
| 89 Zone.current.createTask(specification, _createPeriodicTimerTask); |
| 90 return new _TimerTaskWrapper(task); |
| 91 } |
| 92 |
| 93 factory Timer._periodicTask(Zone zone, Duration duration, |
| 94 void callback(Timer timer)) { |
| 95 PeriodicTimerTaskSpecification specification = |
| 96 new PeriodicTimerTaskSpecification(duration, callback); |
| 97 Object task = |
| 98 Zone.current.createTask(specification, _createPeriodicTimerTask); |
| 99 return new _TimerTaskWrapper(task); |
| 100 } |
| 101 |
| 102 static TimerTask _createSingleShotTimerTask( |
| 103 SingleShotTimerTaskSpecification specification, Zone zone) { |
| 104 ZoneCallback registeredCallback = identical(_ROOT_ZONE, zone) |
| 105 ? specification.callback |
| 106 : zone.registerCallback(specification.callback); |
| 107 |
| 108 TimerTask timerTask; |
| 109 |
| 110 Timer timer = Timer._createTimer(specification.duration, () { |
| 111 timerTask.zone.runTask(timerTask, null, _runSingleShotCallback); |
| 112 }); |
| 113 |
| 114 timerTask = new SingleShotTimerTask(timer, registeredCallback, zone); |
| 115 return timerTask; |
| 116 } |
| 117 |
| 118 static void _runSingleShotCallback(SingleShotTimerTask timerTask, Object _) { |
| 119 timerTask.callback(); |
| 120 } |
| 121 |
| 122 static TimerTask _createPeriodicTimerTask( |
| 123 PeriodicTimerTaskSpecification specification, Zone zone) { |
| 124 ZoneUnaryCallback registeredCallback = identical(_ROOT_ZONE, zone) |
| 125 ? specification.callback |
| 126 : zone.registerUnaryCallback(specification.callback); |
| 127 |
| 128 TimerTask timerTask; |
| 129 |
| 130 Timer timer = Timer._createPeriodicTimer(specification.duration, (Timer _) { |
| 131 timerTask.zone.runTask(timerTask, null, _runPeriodicCallback); |
| 132 }); |
| 133 |
| 134 timerTask = new PeriodicTimerTask(timer, registeredCallback, zone); |
| 135 return timerTask; |
| 136 } |
| 137 |
| 138 static void _runPeriodicCallback(PeriodicTimerTask timerTask, Object _) { |
| 139 // TODO(floitsch): cache the TimerWrapper? |
| 140 timerTask.callback(new _TimerTaskWrapper(timerTask)); |
| 81 } | 141 } |
| 82 | 142 |
| 83 /** | 143 /** |
| 84 * Runs the given [callback] asynchronously as soon as possible. | 144 * Runs the given [callback] asynchronously as soon as possible. |
| 85 * | 145 * |
| 86 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. | 146 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. |
| 87 */ | 147 */ |
| 88 static void run(void callback()) { | 148 static void run(void callback()) { |
| 89 new Timer(Duration.ZERO, callback); | 149 new Timer(Duration.ZERO, callback); |
| 90 } | 150 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 102 * | 162 * |
| 103 * A periodic timer is active if it has not been canceled. | 163 * A periodic timer is active if it has not been canceled. |
| 104 */ | 164 */ |
| 105 bool get isActive; | 165 bool get isActive; |
| 106 | 166 |
| 107 external static Timer _createTimer(Duration duration, void callback()); | 167 external static Timer _createTimer(Duration duration, void callback()); |
| 108 external static Timer _createPeriodicTimer(Duration duration, | 168 external static Timer _createPeriodicTimer(Duration duration, |
| 109 void callback(Timer timer)); | 169 void callback(Timer timer)); |
| 110 } | 170 } |
| 111 | 171 |
| OLD | NEW |