4
4
5
5
part of dart.async;
6
6
7
- abstract class _TimerTask implements Timer {
8
- final Zone _zone;
9
- final Timer _nativeTimer;
10
-
11
- _TimerTask (this ._nativeTimer, this ._zone);
12
-
13
- void cancel () {
14
- _nativeTimer.cancel ();
15
- }
16
-
17
- bool get isActive => _nativeTimer.isActive;
18
- }
19
-
20
- class _SingleShotTimerTask extends _TimerTask {
21
- // TODO(floitsch): the generic argument should be 'void'.
22
- final ZoneCallback <dynamic > _callback;
23
-
24
- _SingleShotTimerTask (Timer timer, this ._callback, Zone zone)
25
- : super (timer, zone);
26
- }
27
-
28
- class _PeriodicTimerTask extends _TimerTask {
29
- // TODO(floitsch): the first generic argument should be 'void'.
30
- final ZoneUnaryCallback <dynamic , Timer > _callback;
31
-
32
- _PeriodicTimerTask (Timer timer, this ._callback, Zone zone)
33
- : super (timer, zone);
34
- }
35
-
36
- /**
37
- * A task specification for a single-shot timer.
38
- *
39
- * *Experimental*. Might disappear without notice.
40
- */
41
- class SingleShotTimerTaskSpecification implements TaskSpecification {
42
- static const String specificationName = "dart.async.timer" ;
43
-
44
- /** The duration after which the timer should invoke the [callback] . */
45
- final Duration duration;
46
-
47
- /** The callback that should be run when the timer triggers. */
48
- // TODO(floitsch): the generic argument should be void.
49
- final ZoneCallback <dynamic > callback;
50
-
51
- SingleShotTimerTaskSpecification (this .duration, void this .callback ());
52
-
53
- @override
54
- String get name => specificationName;
55
-
56
- @override
57
- bool get isOneShot => true ;
58
- }
59
-
60
- /**
61
- * A task specification for a periodic timer.
62
- *
63
- * *Experimental*. Might disappear without notice.
64
- */
65
- class PeriodicTimerTaskSpecification implements TaskSpecification {
66
- static const String specificationName = "dart.async.periodic-timer" ;
67
-
68
- /** The interval at which the periodic timer should invoke the [callback] . */
69
- final Duration duration;
70
-
71
- /** The callback that should be run when the timer triggers. */
72
- // TODO(floitsch): the first generic argument should be void.
73
- final ZoneUnaryCallback <dynamic , Timer > callback;
74
-
75
- PeriodicTimerTaskSpecification (
76
- this .duration, void this .callback (Timer timer));
77
-
78
- @override
79
- String get name => specificationName;
80
-
81
- @override
82
- bool get isOneShot => false ;
83
- }
84
-
85
7
/**
86
8
* A count-down timer that can be configured to fire once or repeatedly.
87
9
*
@@ -125,15 +47,10 @@ abstract class Timer {
125
47
if (Zone .current == Zone .ROOT ) {
126
48
// No need to bind the callback. We know that the root's timer will
127
49
// be invoked in the root zone.
128
- return Timer . _createTimer (duration, callback);
50
+ return Zone .current. createTimer (duration, callback);
129
51
}
130
- return Zone .current.createTimer (duration, callback);
131
- }
132
-
133
- factory Timer ._task (Zone zone, Duration duration, void callback ()) {
134
- SingleShotTimerTaskSpecification specification =
135
- new SingleShotTimerTaskSpecification (duration, callback);
136
- return zone.createTask (_createSingleShotTimerTask, specification);
52
+ return Zone .current.createTimer (
53
+ duration, Zone .current.bindCallback (callback, runGuarded: true ));
137
54
}
138
55
139
56
/**
@@ -153,65 +70,17 @@ abstract class Timer {
153
70
* scheduled for - even if the actual callback was delayed.
154
71
*/
155
72
factory Timer .periodic (Duration duration,
156
- void callback (Timer timer)) {
73
+ void callback (Timer timer)) {
157
74
if (Zone .current == Zone .ROOT ) {
158
75
// No need to bind the callback. We know that the root's timer will
159
76
// be invoked in the root zone.
160
- return Timer . _createPeriodicTimer (duration, callback);
77
+ return Zone .current. createPeriodicTimer (duration, callback);
161
78
}
162
- return Zone .current.createPeriodicTimer (duration, callback);
163
- }
164
-
165
- factory Timer ._periodicTask (Zone zone, Duration duration,
166
- void callback (Timer timer)) {
167
- PeriodicTimerTaskSpecification specification =
168
- new PeriodicTimerTaskSpecification (duration, callback);
169
- return zone.createTask (_createPeriodicTimerTask, specification);
170
- }
171
-
172
- static Timer _createSingleShotTimerTask (
173
- SingleShotTimerTaskSpecification specification, Zone zone) {
174
- ZoneCallback registeredCallback = identical (_ROOT_ZONE , zone)
175
- ? specification.callback
176
- : zone.registerCallback (specification.callback);
177
-
178
- _TimerTask timerTask;
179
-
180
- Timer nativeTimer = Timer ._createTimer (specification.duration, () {
181
- timerTask._zone.runTask (_runSingleShotCallback, timerTask, null );
182
- });
183
-
184
- timerTask = new _SingleShotTimerTask (nativeTimer, registeredCallback, zone);
185
- return timerTask;
186
- }
187
-
188
- static void _runSingleShotCallback (_SingleShotTimerTask timerTask, Object _) {
189
- timerTask._callback ();
190
- }
191
-
192
- static Timer _createPeriodicTimerTask (
193
- PeriodicTimerTaskSpecification specification, Zone zone) {
194
79
// TODO(floitsch): the return type should be 'void', and the type
195
80
// should be inferred.
196
- ZoneUnaryCallback <dynamic , Timer > registeredCallback =
197
- identical (_ROOT_ZONE , zone)
198
- ? specification.callback
199
- : zone.registerUnaryCallback/*<dynamic, Timer>*/ (
200
- specification.callback);
201
-
202
- _TimerTask timerTask;
203
-
204
- Timer nativeTimer =
205
- Timer ._createPeriodicTimer (specification.duration, (Timer _) {
206
- timerTask._zone.runTask (_runPeriodicCallback, timerTask, null );
207
- });
208
-
209
- timerTask = new _PeriodicTimerTask (nativeTimer, registeredCallback, zone);
210
- return timerTask;
211
- }
212
-
213
- static void _runPeriodicCallback (_PeriodicTimerTask timerTask, Object _) {
214
- timerTask._callback (timerTask);
81
+ var boundCallback = Zone .current.bindUnaryCallback/*<dynamic, Timer>*/ (
82
+ callback, runGuarded: true );
83
+ return Zone .current.createPeriodicTimer (duration, boundCallback);
215
84
}
216
85
217
86
/**
0 commit comments