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