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 typedef void _TimerCallback0(); |
| 6 typedef void _TimerCallback1(Timer timer); |
| 7 |
5 patch class Timer { | 8 patch class Timer { |
6 /* patch */ factory Timer(int milliseconds, void callback(Timer timer)) { | 9 /* patch */ factory Timer(var duration, Function callback) { |
| 10 // TODO(floitsch): remove these checks when we remove the deprecated |
| 11 // millisecond argument and the 1-argument callback. Also remove |
| 12 // the int-test below. |
| 13 if (callback is! _TimerCallback0 && callback is! _TimerCallback1) { |
| 14 throw new ArgumentError(callback); |
| 15 } |
| 16 int milliseconds = duration is int ? duration : duration.inMilliseconds; |
| 17 if (milliseconds < 0) milliseconds = 0; |
| 18 _TimerCallback1 oneArgumentCallback = |
| 19 callback is _TimerCallback1 ? callback : (_) { callback(); }; |
7 if (_TimerFactory._factory == null) { | 20 if (_TimerFactory._factory == null) { |
8 throw new UnsupportedError("Timer interface not supported."); | 21 throw new UnsupportedError("Timer interface not supported."); |
9 } | 22 } |
10 return _TimerFactory._factory(milliseconds, callback, false); | 23 return _TimerFactory._factory(milliseconds, oneArgumentCallback, false); |
11 } | 24 } |
12 | 25 |
13 /** | 26 /** |
14 * Creates a new repeating timer. The [callback] is invoked every | 27 * Creates a new repeating timer. The [callback] is invoked every |
15 * [milliseconds] millisecond until cancelled. | 28 * [milliseconds] millisecond until cancelled. |
16 */ | 29 */ |
17 /* patch */ factory Timer.repeating(int milliseconds, | 30 /* patch */ factory Timer.repeating(var duration, |
18 void callback(Timer timer)) { | 31 void callback(Timer timer)) { |
19 if (_TimerFactory._factory == null) { | 32 if (_TimerFactory._factory == null) { |
20 throw new UnsupportedError("Timer interface not supported."); | 33 throw new UnsupportedError("Timer interface not supported."); |
21 } | 34 } |
| 35 // TODO(floitsch): remove this check when we remove the deprecated |
| 36 // millisecond argument. |
| 37 int milliseconds = duration is int ? duration : duration.inMilliseconds; |
| 38 if (milliseconds < 0) milliseconds = 0; |
22 return _TimerFactory._factory(milliseconds, callback, true); | 39 return _TimerFactory._factory(milliseconds, callback, true); |
23 } | 40 } |
24 } | 41 } |
25 | 42 |
26 typedef Timer _TimerFactoryClosure(int milliseconds, | 43 typedef Timer _TimerFactoryClosure(int milliseconds, |
27 void callback(Timer timer), | 44 void callback(Timer timer), |
28 bool repeating); | 45 bool repeating); |
29 | 46 |
30 class _TimerFactory { | 47 class _TimerFactory { |
31 static _TimerFactoryClosure _factory; | 48 static _TimerFactoryClosure _factory; |
32 } | 49 } |
33 | 50 |
34 // TODO(ahe): Warning: this is NOT called by Dartium. Instead, it sets | 51 // TODO(ahe): Warning: this is NOT called by Dartium. Instead, it sets |
35 // [_TimerFactory._factory] directly. | 52 // [_TimerFactory._factory] directly. |
36 void _setTimerFactoryClosure(_TimerFactoryClosure closure) { | 53 void _setTimerFactoryClosure(_TimerFactoryClosure closure) { |
37 _TimerFactory._factory = closure; | 54 _TimerFactory._factory = closure; |
38 } | 55 } |
OLD | NEW |