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 |
11 // Timers are ordered by wakeup time. | 11 // Timers are ordered by wakeup time. |
12 static LinkedList<_Timer> _timers = new LinkedList<_Timer>(); | 12 static LinkedList<_Timer> _timers = new LinkedList<_Timer>(); |
13 | 13 |
14 static RawReceivePort _receivePort; | 14 static RawReceivePort _receivePort; |
15 static bool _handling_callbacks = false; | 15 static bool _handling_callbacks = false; |
16 | 16 |
17 Function _callback; | 17 Function _callback; |
18 int _milliSeconds; | 18 int _milliSeconds; |
19 int _wakeupTime = 0; | 19 int _wakeupTime = 0; |
20 | 20 |
21 static Timer _createTimer(void callback(Timer timer), | 21 static Timer _createTimer(void callback(Timer timer), |
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 // Add one because DateTime.now() is assumed to round down | |
28 // to nearest millisecond, not up, so that time + duration is before | |
29 // duration milliseconds from now. Using micosecond timers like | |
30 // Stopwatch allows detecting that the timer fires early. | |
31 timer._wakeupTime = | 27 timer._wakeupTime = |
32 new DateTime.now().millisecondsSinceEpoch + 1 + milliSeconds; | 28 new DateTime.now().millisecondsSinceEpoch + milliSeconds; |
33 } | 29 } |
34 timer._milliSeconds = repeating ? milliSeconds : -1; | 30 timer._milliSeconds = repeating ? milliSeconds : -1; |
35 timer._addTimerToList(); | 31 timer._addTimerToList(); |
36 if (identical(timer, _timers.first)) { | 32 if (identical(timer, _timers.first)) { |
37 // The new timer is the first in queue. Update event handler. | 33 // The new timer is the first in queue. Update event handler. |
38 timer._notifyEventHandler(); | 34 timer._notifyEventHandler(); |
39 } | 35 } |
40 return timer; | 36 return timer; |
41 } | 37 } |
42 | 38 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 _getTimerFactoryClosure() { | 188 _getTimerFactoryClosure() { |
193 return (int milliSeconds, void callback(Timer timer), bool repeating) { | 189 return (int milliSeconds, void callback(Timer timer), bool repeating) { |
194 if (repeating) { | 190 if (repeating) { |
195 return new _Timer.periodic(milliSeconds, callback); | 191 return new _Timer.periodic(milliSeconds, callback); |
196 } | 192 } |
197 return new _Timer(milliSeconds, callback); | 193 return new _Timer(milliSeconds, callback); |
198 }; | 194 }; |
199 } | 195 } |
200 | 196 |
201 | 197 |
OLD | NEW |