Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: sdk/lib/async/zone.dart

Issue 2119243002: Reapply zone tasks. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fix test and update status files for IE. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sdk/lib/async/timer.dart ('k') | sdk/lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.async; 5 part of dart.async;
6 6
7 typedef R ZoneCallback<R>(); 7 typedef R ZoneCallback<R>();
8 typedef R ZoneUnaryCallback<R, T>(T arg); 8 typedef R ZoneUnaryCallback<R, T>(T arg);
9 typedef R ZoneBinaryCallback<R, T1, T2>(T1 arg1, T2 arg2); 9 typedef R ZoneBinaryCallback<R, T1, T2>(T1 arg1, T2 arg2);
10 10
11 /// *Experimental*. Might disappear without warning.
12 typedef T TaskCreate<T, S extends TaskSpecification>(
13 S specification, Zone zone);
14 /// *Experimental*. Might disappear without warning.
15 typedef void TaskRun<T, A>(T task, A arg);
16
17
11 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic 18 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic
12 // functions. 19 // functions.
13 /*ABUSE*/ 20 /*ABUSE*/
14 typedef R HandleUncaughtErrorHandler<R>( 21 typedef R HandleUncaughtErrorHandler<R>(
15 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace); 22 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace);
16 /*ABUSE*/ 23 /*ABUSE*/
17 typedef R RunHandler<R>(Zone self, ZoneDelegate parent, Zone zone, R f()); 24 typedef R RunHandler<R>(Zone self, ZoneDelegate parent, Zone zone, R f());
18 /*ABUSE*/ 25 /*ABUSE*/
19 typedef R RunUnaryHandler<R, T>( 26 typedef R RunUnaryHandler<R, T>(
20 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg); 27 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg);
21 /*ABUSE*/ 28 /*ABUSE*/
22 typedef R RunBinaryHandler<R, T1, T2>( 29 typedef R RunBinaryHandler<R, T1, T2>(
23 Zone self, ZoneDelegate parent, Zone zone, 30 Zone self, ZoneDelegate parent, Zone zone,
24 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); 31 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2);
25 /*ABUSE*/ 32 /*ABUSE*/
26 typedef ZoneCallback<R> RegisterCallbackHandler<R>( 33 typedef ZoneCallback<R> RegisterCallbackHandler<R>(
27 Zone self, ZoneDelegate parent, Zone zone, R f()); 34 Zone self, ZoneDelegate parent, Zone zone, R f());
28 /*ABUSE*/ 35 /*ABUSE*/
29 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>( 36 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>(
30 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)); 37 Zone self, ZoneDelegate parent, Zone zone, R f(T arg));
31 /*ABUSE*/ 38 /*ABUSE*/
32 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>( 39 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>(
33 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)); 40 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2));
34 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent, 41 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent,
35 Zone zone, Object error, StackTrace stackTrace); 42 Zone zone, Object error, StackTrace stackTrace);
43 /// *Experimental*. Might disappear without warning.
44 /*ABUSE*/
45 typedef T CreateTaskHandler<T, S extends TaskSpecification>(
46 Zone self, ZoneDelegate parent, Zone zone,
47 TaskCreate<T, S> create, S taskSpecification);
48 /// *Experimental*. Might disappear without warning.
49 /*ABUSE*/
50 typedef void RunTaskHandler<T, A>(Zone self, ZoneDelegate parent, Zone zone,
51 TaskRun<T, A> run, T task, A arg);
36 typedef void ScheduleMicrotaskHandler( 52 typedef void ScheduleMicrotaskHandler(
37 Zone self, ZoneDelegate parent, Zone zone, void f()); 53 Zone self, ZoneDelegate parent, Zone zone, void f());
38 typedef Timer CreateTimerHandler(
39 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f());
40 typedef Timer CreatePeriodicTimerHandler(
41 Zone self, ZoneDelegate parent, Zone zone,
42 Duration period, void f(Timer timer));
43 typedef void PrintHandler( 54 typedef void PrintHandler(
44 Zone self, ZoneDelegate parent, Zone zone, String line); 55 Zone self, ZoneDelegate parent, Zone zone, String line);
45 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone, 56 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone,
46 ZoneSpecification specification, 57 ZoneSpecification specification,
47 Map zoneValues); 58 Map zoneValues);
48 59
60 // The following typedef declarations are used by functionality which
61 // will be removed and replaced by tasksif the task experiment is successful.
62 typedef Timer CreateTimerHandler(
63 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f());
64 typedef Timer CreatePeriodicTimerHandler(
65 Zone self, ZoneDelegate parent, Zone zone,
66 Duration period, void f(Timer timer));
67
49 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */ 68 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */
50 class AsyncError implements Error { 69 class AsyncError implements Error {
51 final Object error; 70 final Object error;
52 final StackTrace stackTrace; 71 final StackTrace stackTrace;
53 72
54 AsyncError(this.error, this.stackTrace); 73 AsyncError(this.error, this.stackTrace);
55 74
56 String toString() => '$error'; 75 String toString() => '$error';
57 } 76 }
58 77
78 /**
79 * A task specification contains the necessary information to create a task.
80 *
81 * See [Zone.createTask] for how a specification is used to create a task.
82 *
83 * Task specifications should be public and it should be possible to create
84 * new instances as a user. That is, custom zones should be able to replace
85 * an existing specification with a modified one.
86 *
87 * *Experimental*. This class might disappear without warning.
88 */
89 abstract class TaskSpecification {
90 /**
91 * Description of the task.
92 *
93 * This string is unused by the root-zone, but might be used for debugging,
94 * and testing. As such, it should be relatively unique in its category.
95 *
96 * As a general guideline we recommend: "package-name.library.action".
97 */
98 String get name;
99
100 /**
101 * Whether the scheduled task triggers at most once.
102 *
103 * If the task is not a one-shot task, it may need to be canceled to prevent
104 * further iterations of the task.
105 */
106 bool get isOneShot;
107 }
59 108
60 class _ZoneFunction<T extends Function> { 109 class _ZoneFunction<T extends Function> {
61 final _Zone zone; 110 final _Zone zone;
62 final T function; 111 final T function;
112
63 const _ZoneFunction(this.zone, this.function); 113 const _ZoneFunction(this.zone, this.function);
64 } 114 }
65 115
66 /** 116 /**
67 * This class provides the specification for a forked zone. 117 * This class provides the specification for a forked zone.
68 * 118 *
69 * When forking a new zone (see [Zone.fork]) one can override the default 119 * When forking a new zone (see [Zone.fork]) one can override the default
70 * behavior of the zone by providing callbacks. These callbacks must be 120 * behavior of the zone by providing callbacks. These callbacks must be
71 * given in an instance of this class. 121 * given in an instance of this class.
72 * 122 *
73 * Handlers have the same signature as the same-named methods on [Zone] but 123 * Handlers have the same signature as the same-named methods on [Zone] but
74 * receive three additional arguments: 124 * receive three additional arguments:
75 * 125 *
76 * 1. the zone the handlers are attached to (the "self" zone). 126 * 1. the zone the handlers are attached to (the "self" zone).
77 * 2. a [ZoneDelegate] to the parent zone. 127 * 2. a [ZoneDelegate] to the parent zone.
78 * 3. the zone that first received the request (before the request was 128 * 3. the zone that first received the request (before the request was
79 * bubbled up). 129 * bubbled up).
80 * 130 *
81 * Handlers can either stop propagation the request (by simply not calling the 131 * Handlers can either stop propagation the request (by simply not calling the
82 * parent handler), or forward to the parent zone, potentially modifying the 132 * parent handler), or forward to the parent zone, potentially modifying the
83 * arguments on the way. 133 * arguments on the way.
84 */ 134 */
85 abstract class ZoneSpecification { 135 abstract class ZoneSpecification {
86 /** 136 /**
87 * Creates a specification with the provided handlers. 137 * Creates a specification with the provided handlers.
138 *
139 * The task-related parameters ([createTask] and [runTask]) are experimental
140 * and might be removed without warning.
88 */ 141 */
89 const factory ZoneSpecification({ 142 const factory ZoneSpecification({
90 HandleUncaughtErrorHandler handleUncaughtError, 143 HandleUncaughtErrorHandler handleUncaughtError,
91 RunHandler run, 144 RunHandler run,
92 RunUnaryHandler runUnary, 145 RunUnaryHandler runUnary,
93 RunBinaryHandler runBinary, 146 RunBinaryHandler runBinary,
94 RegisterCallbackHandler registerCallback, 147 RegisterCallbackHandler registerCallback,
95 RegisterUnaryCallbackHandler registerUnaryCallback, 148 RegisterUnaryCallbackHandler registerUnaryCallback,
96 RegisterBinaryCallbackHandler registerBinaryCallback, 149 RegisterBinaryCallbackHandler registerBinaryCallback,
97 ErrorCallbackHandler errorCallback, 150 ErrorCallbackHandler errorCallback,
98 ScheduleMicrotaskHandler scheduleMicrotask, 151 ScheduleMicrotaskHandler scheduleMicrotask,
152 CreateTaskHandler createTask,
153 RunTaskHandler runTask,
154 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
99 CreateTimerHandler createTimer, 155 CreateTimerHandler createTimer,
156 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
100 CreatePeriodicTimerHandler createPeriodicTimer, 157 CreatePeriodicTimerHandler createPeriodicTimer,
101 PrintHandler print, 158 PrintHandler print,
102 ForkHandler fork 159 ForkHandler fork
103 }) = _ZoneSpecification; 160 }) = _ZoneSpecification;
104 161
105 /** 162 /**
106 * Creates a specification from [other] with the provided handlers overriding 163 * Creates a specification from [other] with the provided handlers overriding
107 * the ones in [other]. 164 * the ones in [other].
165 *
166 * The task-related parameters ([createTask] and [runTask]) are experimental
167 * and might be removed without warning.
108 */ 168 */
109 factory ZoneSpecification.from(ZoneSpecification other, { 169 factory ZoneSpecification.from(ZoneSpecification other, {
110 HandleUncaughtErrorHandler handleUncaughtError: null, 170 HandleUncaughtErrorHandler handleUncaughtError: null,
111 RunHandler run: null, 171 RunHandler run: null,
112 RunUnaryHandler runUnary: null, 172 RunUnaryHandler runUnary: null,
113 RunBinaryHandler runBinary: null, 173 RunBinaryHandler runBinary: null,
114 RegisterCallbackHandler registerCallback: null, 174 RegisterCallbackHandler registerCallback: null,
115 RegisterUnaryCallbackHandler registerUnaryCallback: null, 175 RegisterUnaryCallbackHandler registerUnaryCallback: null,
116 RegisterBinaryCallbackHandler registerBinaryCallback: null, 176 RegisterBinaryCallbackHandler registerBinaryCallback: null,
117 ErrorCallbackHandler errorCallback: null, 177 ErrorCallbackHandler errorCallback: null,
118 ScheduleMicrotaskHandler scheduleMicrotask: null, 178 ScheduleMicrotaskHandler scheduleMicrotask: null,
179 CreateTaskHandler createTask: null,
180 RunTaskHandler runTask: null,
181 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
119 CreateTimerHandler createTimer: null, 182 CreateTimerHandler createTimer: null,
183 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
120 CreatePeriodicTimerHandler createPeriodicTimer: null, 184 CreatePeriodicTimerHandler createPeriodicTimer: null,
121 PrintHandler print: null, 185 PrintHandler print: null,
122 ForkHandler fork: null 186 ForkHandler fork: null
123 }) { 187 }) {
124 return new ZoneSpecification( 188 return new ZoneSpecification(
125 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError, 189 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError,
126 run: run ?? other.run, 190 run: run ?? other.run,
127 runUnary: runUnary ?? other.runUnary, 191 runUnary: runUnary ?? other.runUnary,
128 runBinary: runBinary ?? other.runBinary, 192 runBinary: runBinary ?? other.runBinary,
129 registerCallback: registerCallback ?? other.registerCallback, 193 registerCallback: registerCallback ?? other.registerCallback,
130 registerUnaryCallback: registerUnaryCallback ?? 194 registerUnaryCallback: registerUnaryCallback ??
131 other.registerUnaryCallback, 195 other.registerUnaryCallback,
132 registerBinaryCallback: registerBinaryCallback ?? 196 registerBinaryCallback: registerBinaryCallback ??
133 other.registerBinaryCallback, 197 other.registerBinaryCallback,
134 errorCallback: errorCallback ?? other.errorCallback, 198 errorCallback: errorCallback ?? other.errorCallback,
199
200 createTask: createTask ?? other.createTask,
201 runTask: runTask ?? other.runTask,
202 print : print ?? other.print,
203 fork: fork ?? other.fork,
135 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask, 204 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask,
136 createTimer : createTimer ?? other.createTimer, 205 createTimer : createTimer ?? other.createTimer,
137 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer, 206 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer);
138 print : print ?? other.print,
139 fork: fork ?? other.fork);
140 } 207 }
141 208
142 HandleUncaughtErrorHandler get handleUncaughtError; 209 HandleUncaughtErrorHandler get handleUncaughtError;
143 RunHandler get run; 210 RunHandler get run;
144 RunUnaryHandler get runUnary; 211 RunUnaryHandler get runUnary;
145 RunBinaryHandler get runBinary; 212 RunBinaryHandler get runBinary;
146 RegisterCallbackHandler get registerCallback; 213 RegisterCallbackHandler get registerCallback;
147 RegisterUnaryCallbackHandler get registerUnaryCallback; 214 RegisterUnaryCallbackHandler get registerUnaryCallback;
148 RegisterBinaryCallbackHandler get registerBinaryCallback; 215 RegisterBinaryCallbackHandler get registerBinaryCallback;
149 ErrorCallbackHandler get errorCallback; 216 ErrorCallbackHandler get errorCallback;
150 ScheduleMicrotaskHandler get scheduleMicrotask; 217 ScheduleMicrotaskHandler get scheduleMicrotask;
151 CreateTimerHandler get createTimer; 218 /// *Experimental*. Might disappear without warning.
152 CreatePeriodicTimerHandler get createPeriodicTimer; 219 CreateTaskHandler get createTask;
220 /// *Experimental*. Might disappear without warning.
221 RunTaskHandler get runTask;
153 PrintHandler get print; 222 PrintHandler get print;
154 ForkHandler get fork; 223 ForkHandler get fork;
224
225 // TODO(floitsch): deprecate once tasks are non-experimental.
226 CreateTimerHandler get createTimer;
227 // TODO(floitsch): deprecate once tasks are non-experimental.
228 CreatePeriodicTimerHandler get createPeriodicTimer;
155 } 229 }
156 230
157 /** 231 /**
158 * Internal [ZoneSpecification] class. 232 * Internal [ZoneSpecification] class.
159 * 233 *
160 * The implementation wants to rely on the fact that the getters cannot change 234 * The implementation wants to rely on the fact that the getters cannot change
161 * dynamically. We thus require users to go through the redirecting 235 * dynamically. We thus require users to go through the redirecting
162 * [ZoneSpecification] constructor which instantiates this class. 236 * [ZoneSpecification] constructor which instantiates this class.
163 */ 237 */
164 class _ZoneSpecification implements ZoneSpecification { 238 class _ZoneSpecification implements ZoneSpecification {
165 const _ZoneSpecification({ 239 const _ZoneSpecification({
166 this.handleUncaughtError: null, 240 this.handleUncaughtError: null,
167 this.run: null, 241 this.run: null,
168 this.runUnary: null, 242 this.runUnary: null,
169 this.runBinary: null, 243 this.runBinary: null,
170 this.registerCallback: null, 244 this.registerCallback: null,
171 this.registerUnaryCallback: null, 245 this.registerUnaryCallback: null,
172 this.registerBinaryCallback: null, 246 this.registerBinaryCallback: null,
173 this.errorCallback: null, 247 this.errorCallback: null,
174 this.scheduleMicrotask: null, 248 this.scheduleMicrotask: null,
249 this.createTask: null,
250 this.runTask: null,
251 this.print: null,
252 this.fork: null,
253 // TODO(floitsch): deprecate once tasks are non-experimental.
175 this.createTimer: null, 254 this.createTimer: null,
176 this.createPeriodicTimer: null, 255 // TODO(floitsch): deprecate once tasks are non-experimental.
177 this.print: null, 256 this.createPeriodicTimer: null
178 this.fork: null
179 }); 257 });
180 258
181 final HandleUncaughtErrorHandler handleUncaughtError; 259 final HandleUncaughtErrorHandler handleUncaughtError;
182 final RunHandler run; 260 final RunHandler run;
183 final RunUnaryHandler runUnary; 261 final RunUnaryHandler runUnary;
184 final RunBinaryHandler runBinary; 262 final RunBinaryHandler runBinary;
185 final RegisterCallbackHandler registerCallback; 263 final RegisterCallbackHandler registerCallback;
186 final RegisterUnaryCallbackHandler registerUnaryCallback; 264 final RegisterUnaryCallbackHandler registerUnaryCallback;
187 final RegisterBinaryCallbackHandler registerBinaryCallback; 265 final RegisterBinaryCallbackHandler registerBinaryCallback;
188 final ErrorCallbackHandler errorCallback; 266 final ErrorCallbackHandler errorCallback;
189 final ScheduleMicrotaskHandler scheduleMicrotask; 267 final ScheduleMicrotaskHandler scheduleMicrotask;
190 final CreateTimerHandler createTimer; 268 final CreateTaskHandler createTask;
191 final CreatePeriodicTimerHandler createPeriodicTimer; 269 final RunTaskHandler runTask;
192 final PrintHandler print; 270 final PrintHandler print;
193 final ForkHandler fork; 271 final ForkHandler fork;
272
273 // TODO(floitsch): deprecate once tasks are non-experimental.
274 final CreateTimerHandler createTimer;
275 // TODO(floitsch): deprecate once tasks are non-experimental.
276 final CreatePeriodicTimerHandler createPeriodicTimer;
194 } 277 }
195 278
196 /** 279 /**
197 * This class wraps zones for delegation. 280 * This class wraps zones for delegation.
198 * 281 *
199 * When forwarding to parent zones one can't just invoke the parent zone's 282 * When forwarding to parent zones one can't just invoke the parent zone's
200 * exposed functions (like [Zone.run]), but one needs to provide more 283 * exposed functions (like [Zone.run]), but one needs to provide more
201 * information (like the zone the `run` was initiated). Zone callbacks thus 284 * information (like the zone the `run` was initiated). Zone callbacks thus
202 * receive more information including this [ZoneDelegate] class. When delegating 285 * receive more information including this [ZoneDelegate] class. When delegating
203 * to the parent zone one should go through the given instance instead of 286 * to the parent zone one should go through the given instance instead of
204 * directly invoking the parent zone. 287 * directly invoking the parent zone.
205 */ 288 */
206 abstract class ZoneDelegate { 289 abstract class ZoneDelegate {
207 /*=R*/ handleUncaughtError/*<R>*/( 290 /*=R*/ handleUncaughtError/*<R>*/(
208 Zone zone, error, StackTrace stackTrace); 291 Zone zone, error, StackTrace stackTrace);
209 /*=R*/ run/*<R>*/(Zone zone, /*=R*/ f()); 292 /*=R*/ run/*<R>*/(Zone zone, /*=R*/ f());
210 /*=R*/ runUnary/*<R, T>*/(Zone zone, /*=R*/ f(/*=T*/ arg), /*=T*/ arg); 293 /*=R*/ runUnary/*<R, T>*/(Zone zone, /*=R*/ f(/*=T*/ arg), /*=T*/ arg);
211 /*=R*/ runBinary/*<R, T1, T2>*/(Zone zone, 294 /*=R*/ runBinary/*<R, T1, T2>*/(Zone zone,
212 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2), /*=T1*/ arg1, /*=T2*/ arg2); 295 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2), /*=T1*/ arg1, /*=T2*/ arg2);
213 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()); 296 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f());
214 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 297 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
215 Zone zone, /*=R*/ f(/*=T*/ arg)); 298 Zone zone, /*=R*/ f(/*=T*/ arg));
216 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 299 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
217 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)); 300 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2));
218 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace); 301 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace);
219 void scheduleMicrotask(Zone zone, void f()); 302 void scheduleMicrotask(Zone zone, void f());
220 Timer createTimer(Zone zone, Duration duration, void f()); 303
221 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)); 304 /// *Experimental*. Might disappear without notice.
305 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
306 Zone zone, TaskCreate/*<T, S>*/ create,
307 TaskSpecification/*=S*/ specification);
308 /// *Experimental*. Might disappear without notice.
309 void runTask/*<T, A>*/(
310 Zone zone, TaskRun/*<T, A>*/ run, Object/*=T*/ task,
311 Object/*=A*/ argument);
312
222 void print(Zone zone, String line); 313 void print(Zone zone, String line);
223 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues); 314 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues);
315
316 // TODO(floitsch): deprecate once tasks are non-experimental.
317 Timer createTimer(Zone zone, Duration duration, void f());
318 // TODO(floitsch): deprecate once tasks are non-experimental.
319 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer));
224 } 320 }
225 321
226 /** 322 /**
227 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous 323 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous
228 * callbacks are executed in the zone they have been queued in. For example, 324 * callbacks are executed in the zone they have been queued in. For example,
229 * the callback of a `future.then` is executed in the same zone as the one where 325 * the callback of a `future.then` is executed in the same zone as the one where
230 * the `then` was invoked. 326 * the `then` was invoked.
231 */ 327 */
232 abstract class Zone { 328 abstract class Zone {
233 // Private constructor so that it is not possible instantiate a Zone class. 329 // Private constructor so that it is not possible instantiate a Zone class.
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 * If the [AsyncError.error] is `null`, it is replaced by a [NullThrownError]. 500 * If the [AsyncError.error] is `null`, it is replaced by a [NullThrownError].
405 */ 501 */
406 AsyncError errorCallback(Object error, StackTrace stackTrace); 502 AsyncError errorCallback(Object error, StackTrace stackTrace);
407 503
408 /** 504 /**
409 * Runs [f] asynchronously in this zone. 505 * Runs [f] asynchronously in this zone.
410 */ 506 */
411 void scheduleMicrotask(void f()); 507 void scheduleMicrotask(void f());
412 508
413 /** 509 /**
510 * Creates a task in the current zone.
511 *
512 * A task represents an asynchronous operation or process that reports back
513 * through the event loop.
514 *
515 * This function allows the zone to intercept the initialization of the
516 * task while the [runTask] function is invoked when the task reports back.
517 *
518 * By default, in the root zone, the [create] function is invoked with the
519 * [specification] as argument. It returns a task object which is used for all
520 * future interactions between the zone and the task. The object is
521 * a unique instance representing the task. It is generally returned to
522 * whoever initiated the task.
523 * For example, the HTML library uses the returned [StreamSubscription] as
524 * task object when users register an event listener.
525 *
526 * Tasks are created when the program starts an operation that reports back
527 * through the event loop. For example, a timer or an HTTP request both
528 * return through the event loop and are therefore tasks.
529 *
530 * If the [create] function is not invoked (because a custom zone has
531 * replaced or intercepted it), then the operation is *not* started. This
532 * means that a custom zone can intercept tasks, like HTTP requests.
533 *
534 * A task goes through the following steps:
535 * - a user invokes a library function that should eventually return through
536 * the event loop.
537 * - the library function creates a [TaskSpecification] that contains the
538 * necessary information to start the operation, and invokes
539 * `Zone.current.createTask` with the specification and a [create] closure.
540 * The closure, when invoked, uses the specification to start the operation
541 * (usually by interacting with the underlying system, or as a native
542 * extension), and returns a task object that identifies the running task.
543 * - custom zones handle the request and (unless completely intercepted and
544 * aborted), end up calling the root zone's [createTask] which runs the
545 * provided `create` closure, which may have been replaced at this point.
546 * - later, the asynchronous operation returns through the event loop.
547 * It invokes [Zone.runTask] on the zone in which the task should run
548 * (and which was originally passed to the `create` function by
549 * `createTask`). The [runTask] function receives the
550 * task object, a `run` function and an argument. As before, custom zones
551 * may intercept this call. Eventually (unless aborted), the `run` function
552 * is invoked. This last step may happen multiple times for tasks that are
553 * not oneshot tasks (see [ZoneSpecification.isOneShot]).
554 *
555 * Custom zones may replace the [specification] with a different one, thus
556 * modifying the task parameters. An operation that wishes to be an
557 * interceptable task must publicly specify the types that intercepting code
558 * sees:
559 * - The specification type (extending [TaskSpecification]) which holds the
560 * information available when intercepting the `createTask` call.
561 * - The task object type, returned by `createTask` and [create]. This object
562 * may simply be typed as [Object].
563 * - The argument type, if [runTask] takes a meaningful argument.
564 *
565 * *Experimental*. Might disappear without notice.
566 */
567 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
568 /*=T*/ create(TaskSpecification/*=S*/ specification, Zone zone),
569 TaskSpecification/*=S*/ specification);
570
571 /**
572 * Runs a task callback.
573 *
574 * This function is invoked when an operation, started through [createTask],
575 * generates an event.
576 *
577 * Generally, tasks schedule Dart code in the global event loop when the
578 * [createTask] function is invoked. Since the
579 * event loop does not expect any return value from the code it runs, the
580 * [runTask] function is a void function.
581 *
582 * The [task] object must be the same as the one created with [createTask].
583 *
584 * It is good practice that task operations provide a meaningful [argument],
585 * so that custom zones can interact with it. They might want to log or
586 * replace the argument before calling the [run] function.
587 *
588 * See [createTask].
589 *
590 * *Experimental*. Might disappear without notice.
591 */
592 void runTask/*<T, A>*/(
593 /*=T*/ run(/*=T*/ task, /*=A*/ argument), Object/*=T*/ task,
594 Object/*=A*/ argument);
595
596 /**
414 * Creates a Timer where the callback is executed in this zone. 597 * Creates a Timer where the callback is executed in this zone.
415 */ 598 */
599 // TODO(floitsch): deprecate once tasks are non-experimental.
416 Timer createTimer(Duration duration, void callback()); 600 Timer createTimer(Duration duration, void callback());
417 601
418 /** 602 /**
419 * Creates a periodic Timer where the callback is executed in this zone. 603 * Creates a periodic Timer where the callback is executed in this zone.
420 */ 604 */
605 // TODO(floitsch): deprecate once tasks are non-experimental.
421 Timer createPeriodicTimer(Duration period, void callback(Timer timer)); 606 Timer createPeriodicTimer(Duration period, void callback(Timer timer));
422 607
423 /** 608 /**
424 * Prints the given [line]. 609 * Prints the given [line].
425 */ 610 */
426 void print(String line); 611 void print(String line);
427 612
428 /** 613 /**
429 * Call to enter the Zone. 614 * Call to enter the Zone.
430 * 615 *
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 as Object/*=R*/; 701 as Object/*=R*/;
517 } 702 }
518 703
519 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()) { 704 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()) {
520 var implementation = _delegationTarget._registerCallback; 705 var implementation = _delegationTarget._registerCallback;
521 _Zone implZone = implementation.zone; 706 _Zone implZone = implementation.zone;
522 RegisterCallbackHandler handler = implementation.function; 707 RegisterCallbackHandler handler = implementation.function;
523 // TODO(floitsch): make this a generic method call on '<R>' once it's 708 // TODO(floitsch): make this a generic method call on '<R>' once it's
524 // supported. Remove the unnecessary cast. 709 // supported. Remove the unnecessary cast.
525 return handler(implZone, _parentDelegate(implZone), zone, f) 710 return handler(implZone, _parentDelegate(implZone), zone, f)
526 as Object/*=ZoneCallback<R>*/; 711 as dynamic/*=ZoneCallback<R>*/;
527 } 712 }
528 713
529 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 714 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
530 Zone zone, /*=R*/ f(/*=T*/ arg)) { 715 Zone zone, /*=R*/ f(/*=T*/ arg)) {
531 var implementation = _delegationTarget._registerUnaryCallback; 716 var implementation = _delegationTarget._registerUnaryCallback;
532 _Zone implZone = implementation.zone; 717 _Zone implZone = implementation.zone;
533 RegisterUnaryCallbackHandler handler = implementation.function; 718 RegisterUnaryCallbackHandler handler = implementation.function;
534 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 719 // TODO(floitsch): make this a generic method call on '<R, T>' once it's
535 // supported. Remove the unnecessary cast. 720 // supported. Remove the unnecessary cast.
536 return handler(implZone, _parentDelegate(implZone), zone, f) 721 return handler(implZone, _parentDelegate(implZone), zone, f)
537 as Object/*=ZoneUnaryCallback<R, T>*/; 722 as dynamic/*=ZoneUnaryCallback<R, T>*/;
538 } 723 }
539 724
540 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 725 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
541 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) { 726 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) {
542 var implementation = _delegationTarget._registerBinaryCallback; 727 var implementation = _delegationTarget._registerBinaryCallback;
543 _Zone implZone = implementation.zone; 728 _Zone implZone = implementation.zone;
544 RegisterBinaryCallbackHandler handler = implementation.function; 729 RegisterBinaryCallbackHandler handler = implementation.function;
545 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 730 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once
546 // it's supported. Remove the unnecessary cast. 731 // it's supported. Remove the unnecessary cast.
547 return handler(implZone, _parentDelegate(implZone), zone, f) 732 return handler(implZone, _parentDelegate(implZone), zone, f)
548 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; 733 as dynamic/*=ZoneBinaryCallback<R, T1, T2>*/;
549 } 734 }
550 735
551 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) { 736 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) {
552 var implementation = _delegationTarget._errorCallback; 737 var implementation = _delegationTarget._errorCallback;
553 _Zone implZone = implementation.zone; 738 _Zone implZone = implementation.zone;
554 if (identical(implZone, _ROOT_ZONE)) return null; 739 if (identical(implZone, _ROOT_ZONE)) return null;
555 ErrorCallbackHandler handler = implementation.function; 740 ErrorCallbackHandler handler = implementation.function;
556 return handler(implZone, _parentDelegate(implZone), zone, 741 return handler(implZone, _parentDelegate(implZone), zone,
557 error, stackTrace); 742 error, stackTrace);
558 } 743 }
559 744
560 void scheduleMicrotask(Zone zone, f()) { 745 void scheduleMicrotask(Zone zone, f()) {
561 var implementation = _delegationTarget._scheduleMicrotask; 746 var implementation = _delegationTarget._scheduleMicrotask;
562 _Zone implZone = implementation.zone; 747 _Zone implZone = implementation.zone;
563 ScheduleMicrotaskHandler handler = implementation.function; 748 ScheduleMicrotaskHandler handler = implementation.function;
564 handler(implZone, _parentDelegate(implZone), zone, f); 749 handler(implZone, _parentDelegate(implZone), zone, f);
565 } 750 }
566 751
567 Timer createTimer(Zone zone, Duration duration, void f()) { 752 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
568 var implementation = _delegationTarget._createTimer; 753 Zone zone, TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specificat ion) {
754 var implementation = _delegationTarget._createTask;
569 _Zone implZone = implementation.zone; 755 _Zone implZone = implementation.zone;
570 CreateTimerHandler handler = implementation.function; 756 // TODO(floitsch): make the handler call a generic method call on '<T, S>'
571 return handler(implZone, _parentDelegate(implZone), zone, duration, f); 757 // once it's supported. Remove the unnecessary cast.
758 var handler =
759 implementation.function as CreateTaskHandler/*<T, S>*/;
760 return handler(
761 implZone, _parentDelegate(implZone), zone, create, specification);
572 } 762 }
573 763
574 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) { 764 void runTask/*<T, A>*/(Zone zone, TaskRun run, Object /*=T*/ task,
575 var implementation = _delegationTarget._createPeriodicTimer; 765 Object /*=A*/ argument) {
766 var implementation = _delegationTarget._runTask;
576 _Zone implZone = implementation.zone; 767 _Zone implZone = implementation.zone;
577 CreatePeriodicTimerHandler handler = implementation.function; 768 RunTaskHandler handler = implementation.function;
578 return handler(implZone, _parentDelegate(implZone), zone, period, f); 769 // TODO(floitsch): make this a generic call on '<T, A>'.
770 handler(implZone, _parentDelegate(implZone), zone, run, task, argument);
579 } 771 }
580 772
581 void print(Zone zone, String line) { 773 void print(Zone zone, String line) {
582 var implementation = _delegationTarget._print; 774 var implementation = _delegationTarget._print;
583 _Zone implZone = implementation.zone; 775 _Zone implZone = implementation.zone;
584 PrintHandler handler = implementation.function; 776 PrintHandler handler = implementation.function;
585 handler(implZone, _parentDelegate(implZone), zone, line); 777 handler(implZone, _parentDelegate(implZone), zone, line);
586 } 778 }
587 779
588 Zone fork(Zone zone, ZoneSpecification specification, 780 Zone fork(Zone zone, ZoneSpecification specification,
589 Map zoneValues) { 781 Map zoneValues) {
590 var implementation = _delegationTarget._fork; 782 var implementation = _delegationTarget._fork;
591 _Zone implZone = implementation.zone; 783 _Zone implZone = implementation.zone;
592 ForkHandler handler = implementation.function; 784 ForkHandler handler = implementation.function;
593 return handler( 785 return handler(
594 implZone, _parentDelegate(implZone), zone, specification, zoneValues); 786 implZone, _parentDelegate(implZone), zone, specification, zoneValues);
595 } 787 }
788
789 // TODO(floitsch): deprecate once tasks are non-experimental.
790 Timer createTimer(Zone zone, Duration duration, void f()) {
791 var implementation = _delegationTarget._createTimer;
792 _Zone implZone = implementation.zone;
793 CreateTimerHandler handler = implementation.function;
794 return handler(implZone, _parentDelegate(implZone), zone, duration, f);
795 }
796
797 // TODO(floitsch): deprecate once tasks are non-experimental.
798 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) {
799 var implementation = _delegationTarget._createPeriodicTimer;
800 _Zone implZone = implementation.zone;
801 CreatePeriodicTimerHandler handler = implementation.function;
802 return handler(implZone, _parentDelegate(implZone), zone, period, f);
803 }
596 } 804 }
597 805
598 806
599 /** 807 /**
600 * Base class for Zone implementations. 808 * Base class for Zone implementations.
601 */ 809 */
602 abstract class _Zone implements Zone { 810 abstract class _Zone implements Zone {
603 const _Zone(); 811 const _Zone();
604 812
605 _ZoneFunction<RunHandler> get _run; 813 _ZoneFunction<RunHandler> get _run;
606 _ZoneFunction<RunUnaryHandler> get _runUnary; 814 _ZoneFunction<RunUnaryHandler> get _runUnary;
607 _ZoneFunction<RunBinaryHandler> get _runBinary; 815 _ZoneFunction<RunBinaryHandler> get _runBinary;
608 _ZoneFunction<RegisterCallbackHandler> get _registerCallback; 816 _ZoneFunction<RegisterCallbackHandler> get _registerCallback;
609 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback; 817 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback;
610 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback; 818 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback;
611 _ZoneFunction<ErrorCallbackHandler> get _errorCallback; 819 _ZoneFunction<ErrorCallbackHandler> get _errorCallback;
612 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask; 820 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask;
613 _ZoneFunction<CreateTimerHandler> get _createTimer; 821 _ZoneFunction<CreateTaskHandler> get _createTask;
614 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer; 822 _ZoneFunction<RunTaskHandler> get _runTask;
615 _ZoneFunction<PrintHandler> get _print; 823 _ZoneFunction<PrintHandler> get _print;
616 _ZoneFunction<ForkHandler> get _fork; 824 _ZoneFunction<ForkHandler> get _fork;
617 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError; 825 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError;
826
827 // TODO(floitsch): deprecate once tasks are non-experimental.
828 _ZoneFunction<CreateTimerHandler> get _createTimer;
829 // TODO(floitsch): deprecate once tasks are non-experimental.
830 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer;
831
618 _Zone get parent; 832 _Zone get parent;
619 ZoneDelegate get _delegate; 833 ZoneDelegate get _delegate;
620 Map get _map; 834 Map get _map;
621 835
622 bool inSameErrorZone(Zone otherZone) { 836 bool inSameErrorZone(Zone otherZone) {
623 return identical(this, otherZone) || 837 return identical(this, otherZone) ||
624 identical(errorZone, otherZone.errorZone); 838 identical(errorZone, otherZone.errorZone);
625 } 839 }
626 } 840 }
627 841
628 class _CustomZone extends _Zone { 842 class _CustomZone extends _Zone {
629 // The actual zone and implementation of each of these 843 // The actual zone and implementation of each of these
630 // inheritable zone functions. 844 // inheritable zone functions.
631 _ZoneFunction<RunHandler> _run; 845 _ZoneFunction<RunHandler> _run;
632 _ZoneFunction<RunUnaryHandler> _runUnary; 846 _ZoneFunction<RunUnaryHandler> _runUnary;
633 _ZoneFunction<RunBinaryHandler> _runBinary; 847 _ZoneFunction<RunBinaryHandler> _runBinary;
634 _ZoneFunction<RegisterCallbackHandler> _registerCallback; 848 _ZoneFunction<RegisterCallbackHandler> _registerCallback;
635 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback; 849 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback;
636 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback; 850 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback;
637 _ZoneFunction<ErrorCallbackHandler> _errorCallback; 851 _ZoneFunction<ErrorCallbackHandler> _errorCallback;
638 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask; 852 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask;
639 _ZoneFunction<CreateTimerHandler> _createTimer; 853 _ZoneFunction<CreateTaskHandler> _createTask;
640 _ZoneFunction<CreatePeriodicTimerHandler> _createPeriodicTimer; 854 _ZoneFunction<RunTaskHandler> _runTask;
641 _ZoneFunction<PrintHandler> _print; 855 _ZoneFunction<PrintHandler> _print;
642 _ZoneFunction<ForkHandler> _fork; 856 _ZoneFunction<ForkHandler> _fork;
643 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError; 857 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError;
644 858
859 // TODO(floitsch): deprecate once tasks are non-experimental.
860 _ZoneFunction<CreateTimerHandler> _createTimer;
861 // TODO(floitsch): deprecate once tasks are non-experimental.
862 _ZoneFunction<CreatePeriodicTimerHandler> _createPeriodicTimer;
863
645 // A cached delegate to this zone. 864 // A cached delegate to this zone.
646 ZoneDelegate _delegateCache; 865 ZoneDelegate _delegateCache;
647 866
648 /// The parent zone. 867 /// The parent zone.
649 final _Zone parent; 868 final _Zone parent;
650 869
651 /// The zone's scoped value declaration map. 870 /// The zone's scoped value declaration map.
652 /// 871 ///
653 /// This is always a [HashMap]. 872 /// This is always a [HashMap].
654 final Map _map; 873 final Map _map;
(...skipping 30 matching lines...) Expand all
685 this, specification.registerBinaryCallback) 904 this, specification.registerBinaryCallback)
686 : parent._registerBinaryCallback; 905 : parent._registerBinaryCallback;
687 _errorCallback = (specification.errorCallback != null) 906 _errorCallback = (specification.errorCallback != null)
688 ? new _ZoneFunction<ErrorCallbackHandler>( 907 ? new _ZoneFunction<ErrorCallbackHandler>(
689 this, specification.errorCallback) 908 this, specification.errorCallback)
690 : parent._errorCallback; 909 : parent._errorCallback;
691 _scheduleMicrotask = (specification.scheduleMicrotask != null) 910 _scheduleMicrotask = (specification.scheduleMicrotask != null)
692 ? new _ZoneFunction<ScheduleMicrotaskHandler>( 911 ? new _ZoneFunction<ScheduleMicrotaskHandler>(
693 this, specification.scheduleMicrotask) 912 this, specification.scheduleMicrotask)
694 : parent._scheduleMicrotask; 913 : parent._scheduleMicrotask;
695 _createTimer = (specification.createTimer != null) 914 _createTask = (specification.createTask != null)
696 ? new _ZoneFunction<CreateTimerHandler>(this, specification.createTimer) 915 ? new _ZoneFunction<CreateTaskHandler>(
697 : parent._createTimer; 916 this, specification.createTask)
698 _createPeriodicTimer = (specification.createPeriodicTimer != null) 917 : parent._createTask;
699 ? new _ZoneFunction<CreatePeriodicTimerHandler>( 918 _runTask = (specification.runTask != null)
700 this, specification.createPeriodicTimer) 919 ? new _ZoneFunction<RunTaskHandler>(
701 : parent._createPeriodicTimer; 920 this, specification.runTask)
921 : parent._runTask;
702 _print = (specification.print != null) 922 _print = (specification.print != null)
703 ? new _ZoneFunction<PrintHandler>(this, specification.print) 923 ? new _ZoneFunction<PrintHandler>(this, specification.print)
704 : parent._print; 924 : parent._print;
705 _fork = (specification.fork != null) 925 _fork = (specification.fork != null)
706 ? new _ZoneFunction<ForkHandler>(this, specification.fork) 926 ? new _ZoneFunction<ForkHandler>(this, specification.fork)
707 : parent._fork; 927 : parent._fork;
708 _handleUncaughtError = (specification.handleUncaughtError != null) 928 _handleUncaughtError = (specification.handleUncaughtError != null)
709 ? new _ZoneFunction<HandleUncaughtErrorHandler>( 929 ? new _ZoneFunction<HandleUncaughtErrorHandler>(
710 this, specification.handleUncaughtError) 930 this, specification.handleUncaughtError)
711 : parent._handleUncaughtError; 931 : parent._handleUncaughtError;
932
933 // Deprecated fields, once tasks are non-experimental.
934 _createTimer = (specification.createTimer != null)
935 ? new _ZoneFunction<CreateTimerHandler>(
936 this, specification.createTimer)
937 : parent._createTimer;
938 _createPeriodicTimer = (specification.createPeriodicTimer != null)
939 ? new _ZoneFunction<CreatePeriodicTimerHandler>(
940 this, specification.createPeriodicTimer)
941 : parent._createPeriodicTimer;
712 } 942 }
713 943
714 /** 944 /**
715 * The closest error-handling zone. 945 * The closest error-handling zone.
716 * 946 *
717 * Returns `this` if `this` has an error-handler. Otherwise returns the 947 * Returns `this` if `this` has an error-handler. Otherwise returns the
718 * parent's error-zone. 948 * parent's error-zone.
719 */ 949 */
720 Zone get errorZone => _handleUncaughtError.zone; 950 Zone get errorZone => _handleUncaughtError.zone;
721 951
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 } 1082 }
853 1083
854 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) { 1084 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) {
855 var implementation = this._registerCallback; 1085 var implementation = this._registerCallback;
856 assert(implementation != null); 1086 assert(implementation != null);
857 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1087 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
858 RegisterCallbackHandler handler = implementation.function; 1088 RegisterCallbackHandler handler = implementation.function;
859 // TODO(floitsch): make this a generic method call on '<R>' once it's 1089 // TODO(floitsch): make this a generic method call on '<R>' once it's
860 // supported. Remove the unnecessary cast. 1090 // supported. Remove the unnecessary cast.
861 return handler(implementation.zone, parentDelegate, this, callback) 1091 return handler(implementation.zone, parentDelegate, this, callback)
862 as Object/*=ZoneCallback<R>*/; 1092 as dynamic/*=ZoneCallback<R>*/;
863 } 1093 }
864 1094
865 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 1095 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
866 /*=R*/ callback(/*=T*/ arg)) { 1096 /*=R*/ callback(/*=T*/ arg)) {
867 var implementation = this._registerUnaryCallback; 1097 var implementation = this._registerUnaryCallback;
868 assert(implementation != null); 1098 assert(implementation != null);
869 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1099 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
870 RegisterUnaryCallbackHandler handler = implementation.function; 1100 RegisterUnaryCallbackHandler handler = implementation.function;
871 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 1101 // TODO(floitsch): make this a generic method call on '<R, T>' once it's
872 // supported. Remove the unnecessary cast. 1102 // supported. Remove the unnecessary cast.
873 return handler(implementation.zone, parentDelegate, this, callback) 1103 return handler(implementation.zone, parentDelegate, this, callback)
874 as Object/*=ZoneUnaryCallback<R, T>*/; 1104 as dynamic/*=ZoneUnaryCallback<R, T>*/;
875 } 1105 }
876 1106
877 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 1107 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
878 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) { 1108 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) {
879 var implementation = this._registerBinaryCallback; 1109 var implementation = this._registerBinaryCallback;
880 assert(implementation != null); 1110 assert(implementation != null);
881 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1111 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
882 RegisterBinaryCallbackHandler handler = implementation.function; 1112 RegisterBinaryCallbackHandler handler = implementation.function;
883 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 1113 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once
884 // it's supported. Remove the unnecessary cast. 1114 // it's supported. Remove the unnecessary cast.
885 return handler(implementation.zone, parentDelegate, this, callback) 1115 return handler(implementation.zone, parentDelegate, this, callback)
886 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; 1116 as dynamic/*=ZoneBinaryCallback<R, T1, T2>*/;
887 } 1117 }
888 1118
889 AsyncError errorCallback(Object error, StackTrace stackTrace) { 1119 AsyncError errorCallback(Object error, StackTrace stackTrace) {
890 var implementation = this._errorCallback; 1120 var implementation = this._errorCallback;
891 assert(implementation != null); 1121 assert(implementation != null);
892 final Zone implementationZone = implementation.zone; 1122 final Zone implementationZone = implementation.zone;
893 if (identical(implementationZone, _ROOT_ZONE)) return null; 1123 if (identical(implementationZone, _ROOT_ZONE)) return null;
894 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone); 1124 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone);
895 ErrorCallbackHandler handler = implementation.function; 1125 ErrorCallbackHandler handler = implementation.function;
896 return handler( 1126 return handler(
897 implementationZone, parentDelegate, this, error, stackTrace); 1127 implementationZone, parentDelegate, this, error, stackTrace);
898 } 1128 }
899 1129
900 void scheduleMicrotask(void f()) { 1130 void scheduleMicrotask(void f()) {
901 var implementation = this._scheduleMicrotask; 1131 var implementation = this._scheduleMicrotask;
902 assert(implementation != null); 1132 assert(implementation != null);
903 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1133 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
904 ScheduleMicrotaskHandler handler = implementation.function; 1134 ScheduleMicrotaskHandler handler = implementation.function;
905 return handler(implementation.zone, parentDelegate, this, f); 1135 handler(implementation.zone, parentDelegate, this, f);
906 } 1136 }
907 1137
1138 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
1139 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1140 var implementation = this._createTask;
1141 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1142 // TODO(floitsch): make the handler call a generic method call on '<T, S>'
1143 // once it's supported. Remove the unnecessary cast.
1144 var handler =
1145 implementation.function as CreateTaskHandler/*<T, S>*/;
1146 return handler(
1147 implementation.zone, parentDelegate, this, create, specification);
1148 }
1149
1150 void runTask/*<T, A>*/(
1151 TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg1) {
1152 var implementation = this._runTask;
1153 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1154 RunTaskHandler handler = implementation.function;
1155 // TODO(floitsch): make this a generic method call on '<T, A>' once it's
1156 // supported.
1157 handler(implementation.zone, parentDelegate, this, run, task, arg1);
1158 }
1159
1160 void print(String line) {
1161 var implementation = this._print;
1162 assert(implementation != null);
1163 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1164 PrintHandler handler = implementation.function;
1165 return handler(implementation.zone, parentDelegate, this, line);
1166 }
1167
1168 // TODO(floitsch): deprecate once tasks are non-experimental.
908 Timer createTimer(Duration duration, void f()) { 1169 Timer createTimer(Duration duration, void f()) {
909 var implementation = this._createTimer; 1170 var implementation = this._createTimer;
910 assert(implementation != null); 1171 assert(implementation != null);
911 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1172 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
912 CreateTimerHandler handler = implementation.function; 1173 CreateTimerHandler handler = implementation.function;
913 return handler(implementation.zone, parentDelegate, this, duration, f); 1174 return handler(implementation.zone, parentDelegate, this, duration, f);
914 } 1175 }
915 1176
1177 // TODO(floitsch): deprecate once tasks are non-experimental.
916 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 1178 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
917 var implementation = this._createPeriodicTimer; 1179 var implementation = this._createPeriodicTimer;
918 assert(implementation != null); 1180 assert(implementation != null);
919 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1181 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
920 CreatePeriodicTimerHandler handler = implementation.function; 1182 CreatePeriodicTimerHandler handler = implementation.function;
921 return handler( 1183 return handler(
922 implementation.zone, parentDelegate, this, duration, f); 1184 implementation.zone, parentDelegate, this, duration, f);
923 } 1185 }
924
925 void print(String line) {
926 var implementation = this._print;
927 assert(implementation != null);
928 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
929 PrintHandler handler = implementation.function;
930 return handler(implementation.zone, parentDelegate, this, line);
931 }
932 } 1186 }
933 1187
934 /*=R*/ _rootHandleUncaughtError/*<R>*/( 1188 /*=R*/ _rootHandleUncaughtError/*<R>*/(
935 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) { 1189 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) {
936 _schedulePriorityAsyncCallback(() { 1190 _schedulePriorityAsyncCallback(() {
937 if (error == null) error = new NullThrownError(); 1191 if (error == null) error = new NullThrownError();
938 if (stackTrace == null) throw error; 1192 if (stackTrace == null) throw error;
939 _rethrow(error, stackTrace); 1193 _rethrow(error, stackTrace);
940 }); 1194 });
941 } 1195 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) { 1253 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) {
1000 if (!identical(_ROOT_ZONE, zone)) { 1254 if (!identical(_ROOT_ZONE, zone)) {
1001 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone); 1255 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone);
1002 f = zone.bindCallback(f, runGuarded: hasErrorHandler); 1256 f = zone.bindCallback(f, runGuarded: hasErrorHandler);
1003 // Use root zone as event zone if the function is already bound. 1257 // Use root zone as event zone if the function is already bound.
1004 zone = _ROOT_ZONE; 1258 zone = _ROOT_ZONE;
1005 } 1259 }
1006 _scheduleAsyncCallback(f); 1260 _scheduleAsyncCallback(f);
1007 } 1261 }
1008 1262
1263 Object/*=T*/ _rootCreateTask/*<T, S extends TaskSpecification>*/(
1264 Zone self, ZoneDelegate parent, Zone zone,
1265 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1266 return create(specification, zone);
1267 }
1268
1269 void _rootRunTask/*<T, A>*/(
1270 Zone self, ZoneDelegate parent, Zone zone, TaskRun run/*<T, A>*/,
1271 Object/*=T*/ task, Object/*=A*/ arg) {
1272 if (Zone._current == zone) {
1273 run(task, arg);
1274 return;
1275 }
1276
1277 Zone old = Zone._enter(zone);
1278 try {
1279 run(task, arg);
1280 } catch (e, s) {
1281 zone.handleUncaughtError/*<dynamic>*/(e, s);
1282 } finally {
1283 Zone._leave(old);
1284 }
1285 }
1286
1009 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone, 1287 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone,
1010 Duration duration, void callback()) { 1288 Duration duration, void callback()) {
1011 if (!identical(_ROOT_ZONE, zone)) { 1289 return new Timer._task(zone, duration, callback);
1012 callback = zone.bindCallback(callback);
1013 }
1014 return Timer._createTimer(duration, callback);
1015 } 1290 }
1016 1291
1017 Timer _rootCreatePeriodicTimer( 1292 Timer _rootCreatePeriodicTimer(
1018 Zone self, ZoneDelegate parent, Zone zone, 1293 Zone self, ZoneDelegate parent, Zone zone,
1019 Duration duration, void callback(Timer timer)) { 1294 Duration duration, void callback(Timer timer)) {
1020 if (!identical(_ROOT_ZONE, zone)) { 1295 return new Timer._periodicTask(zone, duration, callback);
1021 // TODO(floitsch): the return type should be 'void'.
1022 callback = zone.bindUnaryCallback/*<dynamic, Timer>*/(callback);
1023 }
1024 return Timer._createPeriodicTimer(duration, callback);
1025 } 1296 }
1026 1297
1027 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) { 1298 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) {
1028 printToConsole(line); 1299 printToConsole(line);
1029 } 1300 }
1030 1301
1031 void _printToZone(String line) { 1302 void _printToZone(String line) {
1032 Zone.current.print(line); 1303 Zone.current.print(line);
1033 } 1304 }
1034 1305
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 const _ZoneFunction<RegisterUnaryCallbackHandler>( 1346 const _ZoneFunction<RegisterUnaryCallbackHandler>(
1076 _ROOT_ZONE, _rootRegisterUnaryCallback); 1347 _ROOT_ZONE, _rootRegisterUnaryCallback);
1077 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback => 1348 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback =>
1078 const _ZoneFunction<RegisterBinaryCallbackHandler>( 1349 const _ZoneFunction<RegisterBinaryCallbackHandler>(
1079 _ROOT_ZONE, _rootRegisterBinaryCallback); 1350 _ROOT_ZONE, _rootRegisterBinaryCallback);
1080 _ZoneFunction<ErrorCallbackHandler> get _errorCallback => 1351 _ZoneFunction<ErrorCallbackHandler> get _errorCallback =>
1081 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback); 1352 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback);
1082 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask => 1353 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask =>
1083 const _ZoneFunction<ScheduleMicrotaskHandler>( 1354 const _ZoneFunction<ScheduleMicrotaskHandler>(
1084 _ROOT_ZONE, _rootScheduleMicrotask); 1355 _ROOT_ZONE, _rootScheduleMicrotask);
1085 _ZoneFunction<CreateTimerHandler> get _createTimer => 1356 _ZoneFunction<CreateTaskHandler> get _createTask =>
1086 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer); 1357 const _ZoneFunction<CreateTaskHandler>(_ROOT_ZONE, _rootCreateTask);
1087 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer => 1358 _ZoneFunction<RunTaskHandler> get _runTask =>
1088 const _ZoneFunction<CreatePeriodicTimerHandler>(_ROOT_ZONE, _rootCreatePer iodicTimer); 1359 const _ZoneFunction<RunTaskHandler>(_ROOT_ZONE, _rootRunTask);
1089 _ZoneFunction<PrintHandler> get _print => 1360 _ZoneFunction<PrintHandler> get _print =>
1090 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint); 1361 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint);
1091 _ZoneFunction<ForkHandler> get _fork => 1362 _ZoneFunction<ForkHandler> get _fork =>
1092 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork); 1363 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork);
1093 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError => 1364 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError =>
1094 const _ZoneFunction<HandleUncaughtErrorHandler>( 1365 const _ZoneFunction<HandleUncaughtErrorHandler>(
1095 _ROOT_ZONE, _rootHandleUncaughtError); 1366 _ROOT_ZONE, _rootHandleUncaughtError);
1096 1367
1368 // TODO(floitsch): deprecate once tasks are non-experimental.
1369 _ZoneFunction<CreateTimerHandler> get _createTimer =>
1370 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer);
1371 // TODO(floitsch): deprecate once tasks are non-experimental.
1372 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer =>
1373 const _ZoneFunction<CreatePeriodicTimerHandler>(
1374 _ROOT_ZONE, _rootCreatePeriodicTimer);
1375
1097 // The parent zone. 1376 // The parent zone.
1098 _Zone get parent => null; 1377 _Zone get parent => null;
1099 1378
1100 /// The zone's scoped value declaration map. 1379 /// The zone's scoped value declaration map.
1101 /// 1380 ///
1102 /// This is always a [HashMap]. 1381 /// This is always a [HashMap].
1103 Map get _map => _rootMap; 1382 Map get _map => _rootMap;
1104 1383
1105 static Map _rootMap = new HashMap(); 1384 static Map _rootMap = new HashMap();
1106 1385
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 1497
1219 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 1498 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
1220 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f; 1499 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f;
1221 1500
1222 AsyncError errorCallback(Object error, StackTrace stackTrace) => null; 1501 AsyncError errorCallback(Object error, StackTrace stackTrace) => null;
1223 1502
1224 void scheduleMicrotask(void f()) { 1503 void scheduleMicrotask(void f()) {
1225 _rootScheduleMicrotask(null, null, this, f); 1504 _rootScheduleMicrotask(null, null, this, f);
1226 } 1505 }
1227 1506
1507 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
1508 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1509 return _rootCreateTask/*<T, S>*/(null, null, this, create, specification);
1510 }
1511
1512 void runTask/*<T, A>*/(
1513 TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg) {
1514 _rootRunTask/*<T, A>*/(null, null, this, run, task, arg);
1515 }
1516
1228 Timer createTimer(Duration duration, void f()) { 1517 Timer createTimer(Duration duration, void f()) {
1229 return Timer._createTimer(duration, f); 1518 return Timer._createTimer(duration, f);
1230 } 1519 }
1231 1520
1232 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 1521 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
1233 return Timer._createPeriodicTimer(duration, f); 1522 return Timer._createPeriodicTimer(duration, f);
1234 } 1523 }
1235 1524
1236 void print(String line) { 1525 void print(String line) {
1237 printToConsole(line); 1526 printToConsole(line);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 handleUncaughtError: errorHandler); 1585 handleUncaughtError: errorHandler);
1297 } 1586 }
1298 Zone zone = Zone.current.fork(specification: zoneSpecification, 1587 Zone zone = Zone.current.fork(specification: zoneSpecification,
1299 zoneValues: zoneValues); 1588 zoneValues: zoneValues);
1300 if (onError != null) { 1589 if (onError != null) {
1301 return zone.runGuarded(body); 1590 return zone.runGuarded(body);
1302 } else { 1591 } else {
1303 return zone.run(body); 1592 return zone.run(body);
1304 } 1593 }
1305 } 1594 }
OLDNEW
« no previous file with comments | « sdk/lib/async/timer.dart ('k') | sdk/lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698