OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.io; | 5 part of dart.io; |
6 | 6 |
7 class _Timer extends LinkedListEntry<_Timer> implements Timer { | 7 class _Timer extends LinkedListEntry<_Timer> implements Timer { |
8 // Disables the timer. | 8 // Disables the timer. |
9 static const int _NO_TIMER = -1; | 9 static const int _NO_TIMER = -1; |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 int milliSeconds, | 22 int milliSeconds, |
23 bool repeating) { | 23 bool repeating) { |
24 _Timer timer = new _Timer._internal(); | 24 _Timer timer = new _Timer._internal(); |
25 timer._callback = callback; | 25 timer._callback = callback; |
26 if (milliSeconds > 0) { | 26 if (milliSeconds > 0) { |
27 timer._wakeupTime = | 27 timer._wakeupTime = |
28 new DateTime.now().millisecondsSinceEpoch + milliSeconds; | 28 new DateTime.now().millisecondsSinceEpoch + milliSeconds; |
29 } | 29 } |
30 timer._milliSeconds = repeating ? milliSeconds : -1; | 30 timer._milliSeconds = repeating ? milliSeconds : -1; |
31 timer._addTimerToList(); | 31 timer._addTimerToList(); |
32 timer._notifyEventHandler(); | 32 if (identical(timer, _timers.first)) { |
| 33 // The new timer is the first in queue. Update event handler. |
| 34 timer._notifyEventHandler(); |
| 35 } |
33 return timer; | 36 return timer; |
34 } | 37 } |
35 | 38 |
36 factory _Timer(int milliSeconds, void callback(Timer timer)) { | 39 factory _Timer(int milliSeconds, void callback(Timer timer)) { |
37 return _createTimer(callback, milliSeconds, false); | 40 return _createTimer(callback, milliSeconds, false); |
38 } | 41 } |
39 | 42 |
40 factory _Timer.periodic(int milliSeconds, void callback(Timer timer)) { | 43 factory _Timer.periodic(int milliSeconds, void callback(Timer timer)) { |
41 return _createTimer(callback, milliSeconds, true); | 44 return _createTimer(callback, milliSeconds, true); |
42 } | 45 } |
(...skipping 22 matching lines...) Expand all Loading... |
65 if (identical(first, this)) { | 68 if (identical(first, this)) { |
66 _notifyEventHandler(); | 69 _notifyEventHandler(); |
67 } | 70 } |
68 } | 71 } |
69 | 72 |
70 void _advanceWakeupTime() { | 73 void _advanceWakeupTime() { |
71 assert(_milliSeconds >= 0); | 74 assert(_milliSeconds >= 0); |
72 _wakeupTime += _milliSeconds; | 75 _wakeupTime += _milliSeconds; |
73 } | 76 } |
74 | 77 |
75 // Adds a timer to the timer list and resets the native timer if it is the | 78 // Adds a timer to the timer list. Timers with the same wakeup time are |
76 // earliest timer in the list. Timers with the same wakeup time are enqueued | 79 // enqueued in order and notified in FIFO order. |
77 // in order and notified in FIFO order. | |
78 void _addTimerToList() { | 80 void _addTimerToList() { |
79 _Timer entry = _timers.isEmpty ? null : _timers.first; | 81 _Timer entry = _timers.isEmpty ? null : _timers.first; |
80 while (entry != null) { | 82 while (entry != null) { |
81 if (_wakeupTime < entry._wakeupTime) { | 83 if (_wakeupTime < entry._wakeupTime) { |
82 entry.insertBefore(this); | 84 entry.insertBefore(this); |
83 return; | 85 return; |
84 } | 86 } |
85 entry = entry.next; | 87 entry = entry.next; |
86 } | 88 } |
87 _timers.add(this); | 89 _timers.add(this); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 _getTimerFactoryClosure() { | 182 _getTimerFactoryClosure() { |
181 return (int milliSeconds, void callback(Timer timer), bool repeating) { | 183 return (int milliSeconds, void callback(Timer timer), bool repeating) { |
182 if (repeating) { | 184 if (repeating) { |
183 return new _Timer.periodic(milliSeconds, callback); | 185 return new _Timer.periodic(milliSeconds, callback); |
184 } | 186 } |
185 return new _Timer(milliSeconds, callback); | 187 return new _Timer(milliSeconds, callback); |
186 }; | 188 }; |
187 } | 189 } |
188 | 190 |
189 | 191 |
OLD | NEW |