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

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

Issue 1848933002: Add tasks to zones. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments and copy types from other docs CL. 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') | tests/lib/async/zone_task_test.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 * *Experimental*. Might disappear without notice.
589 */
590 void runTask/*<T, A>*/(
591 /*=T*/ run(/*=T*/ task, /*=A*/ argument), Object/*=T*/ task,
592 Object/*=A*/ argument);
593
594 /**
414 * Creates a Timer where the callback is executed in this zone. 595 * Creates a Timer where the callback is executed in this zone.
415 */ 596 */
597 // TODO(floitsch): deprecate once tasks are non-experimental.
416 Timer createTimer(Duration duration, void callback()); 598 Timer createTimer(Duration duration, void callback());
417 599
418 /** 600 /**
419 * Creates a periodic Timer where the callback is executed in this zone. 601 * Creates a periodic Timer where the callback is executed in this zone.
420 */ 602 */
603 // TODO(floitsch): deprecate once tasks are non-experimental.
421 Timer createPeriodicTimer(Duration period, void callback(Timer timer)); 604 Timer createPeriodicTimer(Duration period, void callback(Timer timer));
422 605
423 /** 606 /**
424 * Prints the given [line]. 607 * Prints the given [line].
425 */ 608 */
426 void print(String line); 609 void print(String line);
427 610
428 /** 611 /**
429 * Call to enter the Zone. 612 * Call to enter the Zone.
430 * 613 *
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 as Object/*=R*/; 699 as Object/*=R*/;
517 } 700 }
518 701
519 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()) { 702 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()) {
520 var implementation = _delegationTarget._registerCallback; 703 var implementation = _delegationTarget._registerCallback;
521 _Zone implZone = implementation.zone; 704 _Zone implZone = implementation.zone;
522 RegisterCallbackHandler handler = implementation.function; 705 RegisterCallbackHandler handler = implementation.function;
523 // TODO(floitsch): make this a generic method call on '<R>' once it's 706 // TODO(floitsch): make this a generic method call on '<R>' once it's
524 // supported. Remove the unnecessary cast. 707 // supported. Remove the unnecessary cast.
525 return handler(implZone, _parentDelegate(implZone), zone, f) 708 return handler(implZone, _parentDelegate(implZone), zone, f)
526 as Object/*=ZoneCallback<R>*/; 709 as dynamic/*=ZoneCallback<R>*/;
527 } 710 }
528 711
529 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 712 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
530 Zone zone, /*=R*/ f(/*=T*/ arg)) { 713 Zone zone, /*=R*/ f(/*=T*/ arg)) {
531 var implementation = _delegationTarget._registerUnaryCallback; 714 var implementation = _delegationTarget._registerUnaryCallback;
532 _Zone implZone = implementation.zone; 715 _Zone implZone = implementation.zone;
533 RegisterUnaryCallbackHandler handler = implementation.function; 716 RegisterUnaryCallbackHandler handler = implementation.function;
534 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 717 // TODO(floitsch): make this a generic method call on '<R, T>' once it's
535 // supported. Remove the unnecessary cast. 718 // supported. Remove the unnecessary cast.
536 return handler(implZone, _parentDelegate(implZone), zone, f) 719 return handler(implZone, _parentDelegate(implZone), zone, f)
537 as Object/*=ZoneUnaryCallback<R, T>*/; 720 as dynamic/*=ZoneUnaryCallback<R, T>*/;
538 } 721 }
539 722
540 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 723 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
541 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) { 724 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) {
542 var implementation = _delegationTarget._registerBinaryCallback; 725 var implementation = _delegationTarget._registerBinaryCallback;
543 _Zone implZone = implementation.zone; 726 _Zone implZone = implementation.zone;
544 RegisterBinaryCallbackHandler handler = implementation.function; 727 RegisterBinaryCallbackHandler handler = implementation.function;
545 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 728 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once
546 // it's supported. Remove the unnecessary cast. 729 // it's supported. Remove the unnecessary cast.
547 return handler(implZone, _parentDelegate(implZone), zone, f) 730 return handler(implZone, _parentDelegate(implZone), zone, f)
548 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; 731 as dynamic/*=ZoneBinaryCallback<R, T1, T2>*/;
549 } 732 }
550 733
551 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) { 734 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) {
552 var implementation = _delegationTarget._errorCallback; 735 var implementation = _delegationTarget._errorCallback;
553 _Zone implZone = implementation.zone; 736 _Zone implZone = implementation.zone;
554 if (identical(implZone, _ROOT_ZONE)) return null; 737 if (identical(implZone, _ROOT_ZONE)) return null;
555 ErrorCallbackHandler handler = implementation.function; 738 ErrorCallbackHandler handler = implementation.function;
556 return handler(implZone, _parentDelegate(implZone), zone, 739 return handler(implZone, _parentDelegate(implZone), zone,
557 error, stackTrace); 740 error, stackTrace);
558 } 741 }
559 742
560 void scheduleMicrotask(Zone zone, f()) { 743 void scheduleMicrotask(Zone zone, f()) {
561 var implementation = _delegationTarget._scheduleMicrotask; 744 var implementation = _delegationTarget._scheduleMicrotask;
562 _Zone implZone = implementation.zone; 745 _Zone implZone = implementation.zone;
563 ScheduleMicrotaskHandler handler = implementation.function; 746 ScheduleMicrotaskHandler handler = implementation.function;
564 handler(implZone, _parentDelegate(implZone), zone, f); 747 handler(implZone, _parentDelegate(implZone), zone, f);
565 } 748 }
566 749
567 Timer createTimer(Zone zone, Duration duration, void f()) { 750 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
568 var implementation = _delegationTarget._createTimer; 751 Zone zone, TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specificat ion) {
752 var implementation = _delegationTarget._createTask;
569 _Zone implZone = implementation.zone; 753 _Zone implZone = implementation.zone;
570 CreateTimerHandler handler = implementation.function; 754 // TODO(floitsch): make the handler call a generic method call on '<T, S>'
571 return handler(implZone, _parentDelegate(implZone), zone, duration, f); 755 // once it's supported. Remove the unnecessary cast.
756 var handler =
757 implementation.function as CreateTaskHandler/*<T, S>*/;
758 return handler(
759 implZone, _parentDelegate(implZone), zone, create, specification);
572 } 760 }
573 761
574 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) { 762 void runTask/*<T, A>*/(Zone zone, TaskRun run, Object /*=T*/ task,
575 var implementation = _delegationTarget._createPeriodicTimer; 763 Object /*=A*/ argument) {
764 var implementation = _delegationTarget._runTask;
576 _Zone implZone = implementation.zone; 765 _Zone implZone = implementation.zone;
577 CreatePeriodicTimerHandler handler = implementation.function; 766 RunTaskHandler handler = implementation.function;
578 return handler(implZone, _parentDelegate(implZone), zone, period, f); 767 // TODO(floitsch): make this a generic call on '<T, A>'.
768 handler(implZone, _parentDelegate(implZone), zone, run, task, argument);
579 } 769 }
580 770
581 void print(Zone zone, String line) { 771 void print(Zone zone, String line) {
582 var implementation = _delegationTarget._print; 772 var implementation = _delegationTarget._print;
583 _Zone implZone = implementation.zone; 773 _Zone implZone = implementation.zone;
584 PrintHandler handler = implementation.function; 774 PrintHandler handler = implementation.function;
585 handler(implZone, _parentDelegate(implZone), zone, line); 775 handler(implZone, _parentDelegate(implZone), zone, line);
586 } 776 }
587 777
588 Zone fork(Zone zone, ZoneSpecification specification, 778 Zone fork(Zone zone, ZoneSpecification specification,
589 Map zoneValues) { 779 Map zoneValues) {
590 var implementation = _delegationTarget._fork; 780 var implementation = _delegationTarget._fork;
591 _Zone implZone = implementation.zone; 781 _Zone implZone = implementation.zone;
592 ForkHandler handler = implementation.function; 782 ForkHandler handler = implementation.function;
593 return handler( 783 return handler(
594 implZone, _parentDelegate(implZone), zone, specification, zoneValues); 784 implZone, _parentDelegate(implZone), zone, specification, zoneValues);
595 } 785 }
786
787 // TODO(floitsch): deprecate once tasks are non-experimental.
788 Timer createTimer(Zone zone, Duration duration, void f()) {
789 var implementation = _delegationTarget._createTimer;
790 _Zone implZone = implementation.zone;
791 CreateTimerHandler handler = implementation.function;
792 return handler(implZone, _parentDelegate(implZone), zone, duration, f);
793 }
794
795 // TODO(floitsch): deprecate once tasks are non-experimental.
796 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) {
797 var implementation = _delegationTarget._createPeriodicTimer;
798 _Zone implZone = implementation.zone;
799 CreatePeriodicTimerHandler handler = implementation.function;
800 return handler(implZone, _parentDelegate(implZone), zone, period, f);
801 }
596 } 802 }
597 803
598 804
599 /** 805 /**
600 * Base class for Zone implementations. 806 * Base class for Zone implementations.
601 */ 807 */
602 abstract class _Zone implements Zone { 808 abstract class _Zone implements Zone {
603 const _Zone(); 809 const _Zone();
604 810
605 _ZoneFunction<RunHandler> get _run; 811 _ZoneFunction<RunHandler> get _run;
606 _ZoneFunction<RunUnaryHandler> get _runUnary; 812 _ZoneFunction<RunUnaryHandler> get _runUnary;
607 _ZoneFunction<RunBinaryHandler> get _runBinary; 813 _ZoneFunction<RunBinaryHandler> get _runBinary;
608 _ZoneFunction<RegisterCallbackHandler> get _registerCallback; 814 _ZoneFunction<RegisterCallbackHandler> get _registerCallback;
609 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback; 815 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback;
610 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback; 816 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback;
611 _ZoneFunction<ErrorCallbackHandler> get _errorCallback; 817 _ZoneFunction<ErrorCallbackHandler> get _errorCallback;
612 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask; 818 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask;
613 _ZoneFunction<CreateTimerHandler> get _createTimer; 819 _ZoneFunction<CreateTaskHandler> get _createTask;
614 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer; 820 _ZoneFunction<RunTaskHandler> get _runTask;
615 _ZoneFunction<PrintHandler> get _print; 821 _ZoneFunction<PrintHandler> get _print;
616 _ZoneFunction<ForkHandler> get _fork; 822 _ZoneFunction<ForkHandler> get _fork;
617 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError; 823 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError;
824
825 // TODO(floitsch): deprecate once tasks are non-experimental.
826 _ZoneFunction<CreateTimerHandler> get _createTimer;
827 // TODO(floitsch): deprecate once tasks are non-experimental.
828 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer;
829
618 _Zone get parent; 830 _Zone get parent;
619 ZoneDelegate get _delegate; 831 ZoneDelegate get _delegate;
620 Map get _map; 832 Map get _map;
621 833
622 bool inSameErrorZone(Zone otherZone) { 834 bool inSameErrorZone(Zone otherZone) {
623 return identical(this, otherZone) || 835 return identical(this, otherZone) ||
624 identical(errorZone, otherZone.errorZone); 836 identical(errorZone, otherZone.errorZone);
625 } 837 }
626 } 838 }
627 839
628 class _CustomZone extends _Zone { 840 class _CustomZone extends _Zone {
629 // The actual zone and implementation of each of these 841 // The actual zone and implementation of each of these
630 // inheritable zone functions. 842 // inheritable zone functions.
631 _ZoneFunction<RunHandler> _run; 843 _ZoneFunction<RunHandler> _run;
632 _ZoneFunction<RunUnaryHandler> _runUnary; 844 _ZoneFunction<RunUnaryHandler> _runUnary;
633 _ZoneFunction<RunBinaryHandler> _runBinary; 845 _ZoneFunction<RunBinaryHandler> _runBinary;
634 _ZoneFunction<RegisterCallbackHandler> _registerCallback; 846 _ZoneFunction<RegisterCallbackHandler> _registerCallback;
635 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback; 847 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback;
636 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback; 848 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback;
637 _ZoneFunction<ErrorCallbackHandler> _errorCallback; 849 _ZoneFunction<ErrorCallbackHandler> _errorCallback;
638 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask; 850 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask;
639 _ZoneFunction<CreateTimerHandler> _createTimer; 851 _ZoneFunction<CreateTaskHandler> _createTask;
640 _ZoneFunction<CreatePeriodicTimerHandler> _createPeriodicTimer; 852 _ZoneFunction<RunTaskHandler> _runTask;
641 _ZoneFunction<PrintHandler> _print; 853 _ZoneFunction<PrintHandler> _print;
642 _ZoneFunction<ForkHandler> _fork; 854 _ZoneFunction<ForkHandler> _fork;
643 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError; 855 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError;
644 856
857 // TODO(floitsch): deprecate once tasks are non-experimental.
858 _ZoneFunction<CreateTimerHandler> _createTimer;
859 // TODO(floitsch): deprecate once tasks are non-experimental.
860 _ZoneFunction<CreatePeriodicTimerHandler> _createPeriodicTimer;
861
645 // A cached delegate to this zone. 862 // A cached delegate to this zone.
646 ZoneDelegate _delegateCache; 863 ZoneDelegate _delegateCache;
647 864
648 /// The parent zone. 865 /// The parent zone.
649 final _Zone parent; 866 final _Zone parent;
650 867
651 /// The zone's scoped value declaration map. 868 /// The zone's scoped value declaration map.
652 /// 869 ///
653 /// This is always a [HashMap]. 870 /// This is always a [HashMap].
654 final Map _map; 871 final Map _map;
(...skipping 30 matching lines...) Expand all
685 this, specification.registerBinaryCallback) 902 this, specification.registerBinaryCallback)
686 : parent._registerBinaryCallback; 903 : parent._registerBinaryCallback;
687 _errorCallback = (specification.errorCallback != null) 904 _errorCallback = (specification.errorCallback != null)
688 ? new _ZoneFunction<ErrorCallbackHandler>( 905 ? new _ZoneFunction<ErrorCallbackHandler>(
689 this, specification.errorCallback) 906 this, specification.errorCallback)
690 : parent._errorCallback; 907 : parent._errorCallback;
691 _scheduleMicrotask = (specification.scheduleMicrotask != null) 908 _scheduleMicrotask = (specification.scheduleMicrotask != null)
692 ? new _ZoneFunction<ScheduleMicrotaskHandler>( 909 ? new _ZoneFunction<ScheduleMicrotaskHandler>(
693 this, specification.scheduleMicrotask) 910 this, specification.scheduleMicrotask)
694 : parent._scheduleMicrotask; 911 : parent._scheduleMicrotask;
695 _createTimer = (specification.createTimer != null) 912 _createTask = (specification.createTask != null)
696 ? new _ZoneFunction<CreateTimerHandler>(this, specification.createTimer) 913 ? new _ZoneFunction<CreateTaskHandler>(
697 : parent._createTimer; 914 this, specification.createTask)
698 _createPeriodicTimer = (specification.createPeriodicTimer != null) 915 : parent._createTask;
699 ? new _ZoneFunction<CreatePeriodicTimerHandler>( 916 _runTask = (specification.runTask != null)
700 this, specification.createPeriodicTimer) 917 ? new _ZoneFunction<RunTaskHandler>(
701 : parent._createPeriodicTimer; 918 this, specification.runTask)
919 : parent._runTask;
702 _print = (specification.print != null) 920 _print = (specification.print != null)
703 ? new _ZoneFunction<PrintHandler>(this, specification.print) 921 ? new _ZoneFunction<PrintHandler>(this, specification.print)
704 : parent._print; 922 : parent._print;
705 _fork = (specification.fork != null) 923 _fork = (specification.fork != null)
706 ? new _ZoneFunction<ForkHandler>(this, specification.fork) 924 ? new _ZoneFunction<ForkHandler>(this, specification.fork)
707 : parent._fork; 925 : parent._fork;
708 _handleUncaughtError = (specification.handleUncaughtError != null) 926 _handleUncaughtError = (specification.handleUncaughtError != null)
709 ? new _ZoneFunction<HandleUncaughtErrorHandler>( 927 ? new _ZoneFunction<HandleUncaughtErrorHandler>(
710 this, specification.handleUncaughtError) 928 this, specification.handleUncaughtError)
711 : parent._handleUncaughtError; 929 : parent._handleUncaughtError;
930
931 // Deprecated fields, once tasks are non-experimental.
932 _createTimer = (specification.createTimer != null)
933 ? new _ZoneFunction<CreateTimerHandler>(
934 this, specification.createTimer)
935 : parent._createTimer;
936 _createPeriodicTimer = (specification.createPeriodicTimer != null)
937 ? new _ZoneFunction<CreatePeriodicTimerHandler>(
938 this, specification.createPeriodicTimer)
939 : parent._createPeriodicTimer;
712 } 940 }
713 941
714 /** 942 /**
715 * The closest error-handling zone. 943 * The closest error-handling zone.
716 * 944 *
717 * Returns `this` if `this` has an error-handler. Otherwise returns the 945 * Returns `this` if `this` has an error-handler. Otherwise returns the
718 * parent's error-zone. 946 * parent's error-zone.
719 */ 947 */
720 Zone get errorZone => _handleUncaughtError.zone; 948 Zone get errorZone => _handleUncaughtError.zone;
721 949
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 } 1080 }
853 1081
854 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) { 1082 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) {
855 var implementation = this._registerCallback; 1083 var implementation = this._registerCallback;
856 assert(implementation != null); 1084 assert(implementation != null);
857 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1085 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
858 RegisterCallbackHandler handler = implementation.function; 1086 RegisterCallbackHandler handler = implementation.function;
859 // TODO(floitsch): make this a generic method call on '<R>' once it's 1087 // TODO(floitsch): make this a generic method call on '<R>' once it's
860 // supported. Remove the unnecessary cast. 1088 // supported. Remove the unnecessary cast.
861 return handler(implementation.zone, parentDelegate, this, callback) 1089 return handler(implementation.zone, parentDelegate, this, callback)
862 as Object/*=ZoneCallback<R>*/; 1090 as dynamic/*=ZoneCallback<R>*/;
863 } 1091 }
864 1092
865 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 1093 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
866 /*=R*/ callback(/*=T*/ arg)) { 1094 /*=R*/ callback(/*=T*/ arg)) {
867 var implementation = this._registerUnaryCallback; 1095 var implementation = this._registerUnaryCallback;
868 assert(implementation != null); 1096 assert(implementation != null);
869 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1097 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
870 RegisterUnaryCallbackHandler handler = implementation.function; 1098 RegisterUnaryCallbackHandler handler = implementation.function;
871 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 1099 // TODO(floitsch): make this a generic method call on '<R, T>' once it's
872 // supported. Remove the unnecessary cast. 1100 // supported. Remove the unnecessary cast.
873 return handler(implementation.zone, parentDelegate, this, callback) 1101 return handler(implementation.zone, parentDelegate, this, callback)
874 as Object/*=ZoneUnaryCallback<R, T>*/; 1102 as dynamic/*=ZoneUnaryCallback<R, T>*/;
875 } 1103 }
876 1104
877 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 1105 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
878 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) { 1106 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) {
879 var implementation = this._registerBinaryCallback; 1107 var implementation = this._registerBinaryCallback;
880 assert(implementation != null); 1108 assert(implementation != null);
881 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1109 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
882 RegisterBinaryCallbackHandler handler = implementation.function; 1110 RegisterBinaryCallbackHandler handler = implementation.function;
883 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 1111 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once
884 // it's supported. Remove the unnecessary cast. 1112 // it's supported. Remove the unnecessary cast.
885 return handler(implementation.zone, parentDelegate, this, callback) 1113 return handler(implementation.zone, parentDelegate, this, callback)
886 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; 1114 as dynamic/*=ZoneBinaryCallback<R, T1, T2>*/;
887 } 1115 }
888 1116
889 AsyncError errorCallback(Object error, StackTrace stackTrace) { 1117 AsyncError errorCallback(Object error, StackTrace stackTrace) {
890 var implementation = this._errorCallback; 1118 var implementation = this._errorCallback;
891 assert(implementation != null); 1119 assert(implementation != null);
892 final Zone implementationZone = implementation.zone; 1120 final Zone implementationZone = implementation.zone;
893 if (identical(implementationZone, _ROOT_ZONE)) return null; 1121 if (identical(implementationZone, _ROOT_ZONE)) return null;
894 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone); 1122 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone);
895 ErrorCallbackHandler handler = implementation.function; 1123 ErrorCallbackHandler handler = implementation.function;
896 return handler( 1124 return handler(
897 implementationZone, parentDelegate, this, error, stackTrace); 1125 implementationZone, parentDelegate, this, error, stackTrace);
898 } 1126 }
899 1127
900 void scheduleMicrotask(void f()) { 1128 void scheduleMicrotask(void f()) {
901 var implementation = this._scheduleMicrotask; 1129 var implementation = this._scheduleMicrotask;
902 assert(implementation != null); 1130 assert(implementation != null);
903 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1131 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
904 ScheduleMicrotaskHandler handler = implementation.function; 1132 ScheduleMicrotaskHandler handler = implementation.function;
905 return handler(implementation.zone, parentDelegate, this, f); 1133 handler(implementation.zone, parentDelegate, this, f);
906 } 1134 }
907 1135
1136 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
1137 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1138 var implementation = this._createTask;
1139 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1140 // TODO(floitsch): make the handler call a generic method call on '<T, S>'
1141 // once it's supported. Remove the unnecessary cast.
1142 var handler =
1143 implementation.function as CreateTaskHandler/*<T, S>*/;
1144 return handler(
1145 implementation.zone, parentDelegate, this, create, specification);
1146 }
1147
1148 void runTask/*<T, A>*/(
1149 TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg1) {
1150 var implementation = this._runTask;
1151 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1152 RunTaskHandler handler = implementation.function;
1153 // TODO(floitsch): make this a generic method call on '<T, A>' once it's
1154 // supported.
1155 handler(implementation.zone, parentDelegate, this, run, task, arg1);
1156 }
1157
1158 void print(String line) {
1159 var implementation = this._print;
1160 assert(implementation != null);
1161 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1162 PrintHandler handler = implementation.function;
1163 return handler(implementation.zone, parentDelegate, this, line);
1164 }
1165
1166 // TODO(floitsch): deprecate once tasks are non-experimental.
908 Timer createTimer(Duration duration, void f()) { 1167 Timer createTimer(Duration duration, void f()) {
909 var implementation = this._createTimer; 1168 var implementation = this._createTimer;
910 assert(implementation != null); 1169 assert(implementation != null);
911 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1170 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
912 CreateTimerHandler handler = implementation.function; 1171 CreateTimerHandler handler = implementation.function;
913 return handler(implementation.zone, parentDelegate, this, duration, f); 1172 return handler(implementation.zone, parentDelegate, this, duration, f);
914 } 1173 }
915 1174
1175 // TODO(floitsch): deprecate once tasks are non-experimental.
916 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 1176 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
917 var implementation = this._createPeriodicTimer; 1177 var implementation = this._createPeriodicTimer;
918 assert(implementation != null); 1178 assert(implementation != null);
919 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1179 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
920 CreatePeriodicTimerHandler handler = implementation.function; 1180 CreatePeriodicTimerHandler handler = implementation.function;
921 return handler( 1181 return handler(
922 implementation.zone, parentDelegate, this, duration, f); 1182 implementation.zone, parentDelegate, this, duration, f);
923 } 1183 }
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 } 1184 }
933 1185
934 /*=R*/ _rootHandleUncaughtError/*<R>*/( 1186 /*=R*/ _rootHandleUncaughtError/*<R>*/(
935 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) { 1187 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) {
936 _schedulePriorityAsyncCallback(() { 1188 _schedulePriorityAsyncCallback(() {
937 if (error == null) error = new NullThrownError(); 1189 if (error == null) error = new NullThrownError();
938 if (stackTrace == null) throw error; 1190 if (stackTrace == null) throw error;
939 _rethrow(error, stackTrace); 1191 _rethrow(error, stackTrace);
940 }); 1192 });
941 } 1193 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) { 1251 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) {
1000 if (!identical(_ROOT_ZONE, zone)) { 1252 if (!identical(_ROOT_ZONE, zone)) {
1001 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone); 1253 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone);
1002 f = zone.bindCallback(f, runGuarded: hasErrorHandler); 1254 f = zone.bindCallback(f, runGuarded: hasErrorHandler);
1003 // Use root zone as event zone if the function is already bound. 1255 // Use root zone as event zone if the function is already bound.
1004 zone = _ROOT_ZONE; 1256 zone = _ROOT_ZONE;
1005 } 1257 }
1006 _scheduleAsyncCallback(f); 1258 _scheduleAsyncCallback(f);
1007 } 1259 }
1008 1260
1261 Object/*=T*/ _rootCreateTask/*<T, S extends TaskSpecification>*/(
1262 Zone self, ZoneDelegate parent, Zone zone,
1263 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1264 return create(specification, zone);
1265 }
1266
1267 void _rootRunTask/*<T, A>*/(
1268 Zone self, ZoneDelegate parent, Zone zone, TaskRun run/*<T, A>*/,
1269 Object/*=T*/ task, Object/*=A*/ arg) {
1270 if (Zone._current == zone) {
1271 run(task, arg);
1272 return;
1273 }
1274
1275 Zone old = Zone._enter(zone);
1276 try {
1277 run(task, arg);
1278 } catch (e, s) {
1279 zone.handleUncaughtError/*<dynamic>*/(e, s);
1280 } finally {
1281 Zone._leave(old);
1282 }
1283 }
1284
1009 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone, 1285 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone,
1010 Duration duration, void callback()) { 1286 Duration duration, void callback()) {
1011 if (!identical(_ROOT_ZONE, zone)) { 1287 return new Timer._task(zone, duration, callback);
1012 callback = zone.bindCallback(callback);
1013 }
1014 return Timer._createTimer(duration, callback);
1015 } 1288 }
1016 1289
1017 Timer _rootCreatePeriodicTimer( 1290 Timer _rootCreatePeriodicTimer(
1018 Zone self, ZoneDelegate parent, Zone zone, 1291 Zone self, ZoneDelegate parent, Zone zone,
1019 Duration duration, void callback(Timer timer)) { 1292 Duration duration, void callback(Timer timer)) {
1020 if (!identical(_ROOT_ZONE, zone)) { 1293 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 } 1294 }
1026 1295
1027 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) { 1296 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) {
1028 printToConsole(line); 1297 printToConsole(line);
1029 } 1298 }
1030 1299
1031 void _printToZone(String line) { 1300 void _printToZone(String line) {
1032 Zone.current.print(line); 1301 Zone.current.print(line);
1033 } 1302 }
1034 1303
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 const _ZoneFunction<RegisterUnaryCallbackHandler>( 1344 const _ZoneFunction<RegisterUnaryCallbackHandler>(
1076 _ROOT_ZONE, _rootRegisterUnaryCallback); 1345 _ROOT_ZONE, _rootRegisterUnaryCallback);
1077 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback => 1346 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback =>
1078 const _ZoneFunction<RegisterBinaryCallbackHandler>( 1347 const _ZoneFunction<RegisterBinaryCallbackHandler>(
1079 _ROOT_ZONE, _rootRegisterBinaryCallback); 1348 _ROOT_ZONE, _rootRegisterBinaryCallback);
1080 _ZoneFunction<ErrorCallbackHandler> get _errorCallback => 1349 _ZoneFunction<ErrorCallbackHandler> get _errorCallback =>
1081 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback); 1350 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback);
1082 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask => 1351 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask =>
1083 const _ZoneFunction<ScheduleMicrotaskHandler>( 1352 const _ZoneFunction<ScheduleMicrotaskHandler>(
1084 _ROOT_ZONE, _rootScheduleMicrotask); 1353 _ROOT_ZONE, _rootScheduleMicrotask);
1085 _ZoneFunction<CreateTimerHandler> get _createTimer => 1354 _ZoneFunction<CreateTaskHandler> get _createTask =>
1086 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer); 1355 const _ZoneFunction<CreateTaskHandler>(_ROOT_ZONE, _rootCreateTask);
1087 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer => 1356 _ZoneFunction<RunTaskHandler> get _runTask =>
1088 const _ZoneFunction<CreatePeriodicTimerHandler>(_ROOT_ZONE, _rootCreatePer iodicTimer); 1357 const _ZoneFunction<RunTaskHandler>(_ROOT_ZONE, _rootRunTask);
1089 _ZoneFunction<PrintHandler> get _print => 1358 _ZoneFunction<PrintHandler> get _print =>
1090 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint); 1359 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint);
1091 _ZoneFunction<ForkHandler> get _fork => 1360 _ZoneFunction<ForkHandler> get _fork =>
1092 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork); 1361 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork);
1093 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError => 1362 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError =>
1094 const _ZoneFunction<HandleUncaughtErrorHandler>( 1363 const _ZoneFunction<HandleUncaughtErrorHandler>(
1095 _ROOT_ZONE, _rootHandleUncaughtError); 1364 _ROOT_ZONE, _rootHandleUncaughtError);
1096 1365
1366 // TODO(floitsch): deprecate once tasks are non-experimental.
1367 _ZoneFunction<CreateTimerHandler> get _createTimer =>
1368 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer);
1369 // TODO(floitsch): deprecate once tasks are non-experimental.
1370 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer =>
1371 const _ZoneFunction<CreatePeriodicTimerHandler>(
1372 _ROOT_ZONE, _rootCreatePeriodicTimer);
1373
1097 // The parent zone. 1374 // The parent zone.
1098 _Zone get parent => null; 1375 _Zone get parent => null;
1099 1376
1100 /// The zone's scoped value declaration map. 1377 /// The zone's scoped value declaration map.
1101 /// 1378 ///
1102 /// This is always a [HashMap]. 1379 /// This is always a [HashMap].
1103 Map get _map => _rootMap; 1380 Map get _map => _rootMap;
1104 1381
1105 static Map _rootMap = new HashMap(); 1382 static Map _rootMap = new HashMap();
1106 1383
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 1495
1219 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 1496 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
1220 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f; 1497 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f;
1221 1498
1222 AsyncError errorCallback(Object error, StackTrace stackTrace) => null; 1499 AsyncError errorCallback(Object error, StackTrace stackTrace) => null;
1223 1500
1224 void scheduleMicrotask(void f()) { 1501 void scheduleMicrotask(void f()) {
1225 _rootScheduleMicrotask(null, null, this, f); 1502 _rootScheduleMicrotask(null, null, this, f);
1226 } 1503 }
1227 1504
1505 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
1506 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1507 return _rootCreateTask/*<T, S>*/(null, null, this, create, specification);
1508 }
1509
1510 void runTask/*<T, A>*/(
1511 TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg) {
1512 _rootRunTask/*<T, A>*/(null, null, this, run, task, arg);
1513 }
1514
1228 Timer createTimer(Duration duration, void f()) { 1515 Timer createTimer(Duration duration, void f()) {
1229 return Timer._createTimer(duration, f); 1516 return Timer._createTimer(duration, f);
1230 } 1517 }
1231 1518
1232 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 1519 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
1233 return Timer._createPeriodicTimer(duration, f); 1520 return Timer._createPeriodicTimer(duration, f);
1234 } 1521 }
1235 1522
1236 void print(String line) { 1523 void print(String line) {
1237 printToConsole(line); 1524 printToConsole(line);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 handleUncaughtError: errorHandler); 1583 handleUncaughtError: errorHandler);
1297 } 1584 }
1298 Zone zone = Zone.current.fork(specification: zoneSpecification, 1585 Zone zone = Zone.current.fork(specification: zoneSpecification,
1299 zoneValues: zoneValues); 1586 zoneValues: zoneValues);
1300 if (onError != null) { 1587 if (onError != null) {
1301 return zone.runGuarded(body); 1588 return zone.runGuarded(body);
1302 } else { 1589 } else {
1303 return zone.run(body); 1590 return zone.run(body);
1304 } 1591 }
1305 } 1592 }
OLDNEW
« no previous file with comments | « sdk/lib/async/timer.dart ('k') | tests/lib/async/zone_task_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698