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

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

Issue 2120063002: Revert zone tasks. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: 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
18 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic 11 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic
19 // functions. 12 // functions.
20 /*ABUSE*/ 13 /*ABUSE*/
21 typedef R HandleUncaughtErrorHandler<R>( 14 typedef R HandleUncaughtErrorHandler<R>(
22 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace); 15 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace);
23 /*ABUSE*/ 16 /*ABUSE*/
24 typedef R RunHandler<R>(Zone self, ZoneDelegate parent, Zone zone, R f()); 17 typedef R RunHandler<R>(Zone self, ZoneDelegate parent, Zone zone, R f());
25 /*ABUSE*/ 18 /*ABUSE*/
26 typedef R RunUnaryHandler<R, T>( 19 typedef R RunUnaryHandler<R, T>(
27 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg); 20 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg);
28 /*ABUSE*/ 21 /*ABUSE*/
29 typedef R RunBinaryHandler<R, T1, T2>( 22 typedef R RunBinaryHandler<R, T1, T2>(
30 Zone self, ZoneDelegate parent, Zone zone, 23 Zone self, ZoneDelegate parent, Zone zone,
31 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); 24 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2);
32 /*ABUSE*/ 25 /*ABUSE*/
33 typedef ZoneCallback<R> RegisterCallbackHandler<R>( 26 typedef ZoneCallback<R> RegisterCallbackHandler<R>(
34 Zone self, ZoneDelegate parent, Zone zone, R f()); 27 Zone self, ZoneDelegate parent, Zone zone, R f());
35 /*ABUSE*/ 28 /*ABUSE*/
36 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>( 29 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>(
37 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)); 30 Zone self, ZoneDelegate parent, Zone zone, R f(T arg));
38 /*ABUSE*/ 31 /*ABUSE*/
39 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>( 32 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>(
40 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)); 33 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2));
41 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent, 34 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent,
42 Zone zone, Object error, StackTrace stackTrace); 35 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);
52 typedef void ScheduleMicrotaskHandler( 36 typedef void ScheduleMicrotaskHandler(
53 Zone self, ZoneDelegate parent, Zone zone, void f()); 37 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));
54 typedef void PrintHandler( 43 typedef void PrintHandler(
55 Zone self, ZoneDelegate parent, Zone zone, String line); 44 Zone self, ZoneDelegate parent, Zone zone, String line);
56 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone, 45 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone,
57 ZoneSpecification specification, 46 ZoneSpecification specification,
58 Map zoneValues); 47 Map zoneValues);
59 48
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
68 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */ 49 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */
69 class AsyncError implements Error { 50 class AsyncError implements Error {
70 final Object error; 51 final Object error;
71 final StackTrace stackTrace; 52 final StackTrace stackTrace;
72 53
73 AsyncError(this.error, this.stackTrace); 54 AsyncError(this.error, this.stackTrace);
74 55
75 String toString() => '$error'; 56 String toString() => '$error';
76 } 57 }
77 58
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 }
108 59
109 class _ZoneFunction<T extends Function> { 60 class _ZoneFunction<T extends Function> {
110 final _Zone zone; 61 final _Zone zone;
111 final T function; 62 final T function;
112
113 const _ZoneFunction(this.zone, this.function); 63 const _ZoneFunction(this.zone, this.function);
114 } 64 }
115 65
116 /** 66 /**
117 * This class provides the specification for a forked zone. 67 * This class provides the specification for a forked zone.
118 * 68 *
119 * When forking a new zone (see [Zone.fork]) one can override the default 69 * When forking a new zone (see [Zone.fork]) one can override the default
120 * behavior of the zone by providing callbacks. These callbacks must be 70 * behavior of the zone by providing callbacks. These callbacks must be
121 * given in an instance of this class. 71 * given in an instance of this class.
122 * 72 *
123 * Handlers have the same signature as the same-named methods on [Zone] but 73 * Handlers have the same signature as the same-named methods on [Zone] but
124 * receive three additional arguments: 74 * receive three additional arguments:
125 * 75 *
126 * 1. the zone the handlers are attached to (the "self" zone). 76 * 1. the zone the handlers are attached to (the "self" zone).
127 * 2. a [ZoneDelegate] to the parent zone. 77 * 2. a [ZoneDelegate] to the parent zone.
128 * 3. the zone that first received the request (before the request was 78 * 3. the zone that first received the request (before the request was
129 * bubbled up). 79 * bubbled up).
130 * 80 *
131 * Handlers can either stop propagation the request (by simply not calling the 81 * Handlers can either stop propagation the request (by simply not calling the
132 * parent handler), or forward to the parent zone, potentially modifying the 82 * parent handler), or forward to the parent zone, potentially modifying the
133 * arguments on the way. 83 * arguments on the way.
134 */ 84 */
135 abstract class ZoneSpecification { 85 abstract class ZoneSpecification {
136 /** 86 /**
137 * Creates a specification with the provided handlers. 87 * 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.
141 */ 88 */
142 const factory ZoneSpecification({ 89 const factory ZoneSpecification({
143 HandleUncaughtErrorHandler handleUncaughtError, 90 HandleUncaughtErrorHandler handleUncaughtError,
144 RunHandler run, 91 RunHandler run,
145 RunUnaryHandler runUnary, 92 RunUnaryHandler runUnary,
146 RunBinaryHandler runBinary, 93 RunBinaryHandler runBinary,
147 RegisterCallbackHandler registerCallback, 94 RegisterCallbackHandler registerCallback,
148 RegisterUnaryCallbackHandler registerUnaryCallback, 95 RegisterUnaryCallbackHandler registerUnaryCallback,
149 RegisterBinaryCallbackHandler registerBinaryCallback, 96 RegisterBinaryCallbackHandler registerBinaryCallback,
150 ErrorCallbackHandler errorCallback, 97 ErrorCallbackHandler errorCallback,
151 ScheduleMicrotaskHandler scheduleMicrotask, 98 ScheduleMicrotaskHandler scheduleMicrotask,
152 CreateTaskHandler createTask,
153 RunTaskHandler runTask,
154 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
155 CreateTimerHandler createTimer, 99 CreateTimerHandler createTimer,
156 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
157 CreatePeriodicTimerHandler createPeriodicTimer, 100 CreatePeriodicTimerHandler createPeriodicTimer,
158 PrintHandler print, 101 PrintHandler print,
159 ForkHandler fork 102 ForkHandler fork
160 }) = _ZoneSpecification; 103 }) = _ZoneSpecification;
161 104
162 /** 105 /**
163 * Creates a specification from [other] with the provided handlers overriding 106 * Creates a specification from [other] with the provided handlers overriding
164 * the ones in [other]. 107 * the ones in [other].
165 *
166 * The task-related parameters ([createTask] and [runTask]) are experimental
167 * and might be removed without warning.
168 */ 108 */
169 factory ZoneSpecification.from(ZoneSpecification other, { 109 factory ZoneSpecification.from(ZoneSpecification other, {
170 HandleUncaughtErrorHandler handleUncaughtError: null, 110 HandleUncaughtErrorHandler handleUncaughtError: null,
171 RunHandler run: null, 111 RunHandler run: null,
172 RunUnaryHandler runUnary: null, 112 RunUnaryHandler runUnary: null,
173 RunBinaryHandler runBinary: null, 113 RunBinaryHandler runBinary: null,
174 RegisterCallbackHandler registerCallback: null, 114 RegisterCallbackHandler registerCallback: null,
175 RegisterUnaryCallbackHandler registerUnaryCallback: null, 115 RegisterUnaryCallbackHandler registerUnaryCallback: null,
176 RegisterBinaryCallbackHandler registerBinaryCallback: null, 116 RegisterBinaryCallbackHandler registerBinaryCallback: null,
177 ErrorCallbackHandler errorCallback: null, 117 ErrorCallbackHandler errorCallback: null,
178 ScheduleMicrotaskHandler scheduleMicrotask: null, 118 ScheduleMicrotaskHandler scheduleMicrotask: null,
179 CreateTaskHandler createTask: null,
180 RunTaskHandler runTask: null,
181 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
182 CreateTimerHandler createTimer: null, 119 CreateTimerHandler createTimer: null,
183 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
184 CreatePeriodicTimerHandler createPeriodicTimer: null, 120 CreatePeriodicTimerHandler createPeriodicTimer: null,
185 PrintHandler print: null, 121 PrintHandler print: null,
186 ForkHandler fork: null 122 ForkHandler fork: null
187 }) { 123 }) {
188 return new ZoneSpecification( 124 return new ZoneSpecification(
189 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError, 125 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError,
190 run: run ?? other.run, 126 run: run ?? other.run,
191 runUnary: runUnary ?? other.runUnary, 127 runUnary: runUnary ?? other.runUnary,
192 runBinary: runBinary ?? other.runBinary, 128 runBinary: runBinary ?? other.runBinary,
193 registerCallback: registerCallback ?? other.registerCallback, 129 registerCallback: registerCallback ?? other.registerCallback,
194 registerUnaryCallback: registerUnaryCallback ?? 130 registerUnaryCallback: registerUnaryCallback ??
195 other.registerUnaryCallback, 131 other.registerUnaryCallback,
196 registerBinaryCallback: registerBinaryCallback ?? 132 registerBinaryCallback: registerBinaryCallback ??
197 other.registerBinaryCallback, 133 other.registerBinaryCallback,
198 errorCallback: errorCallback ?? other.errorCallback, 134 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,
204 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask, 135 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask,
205 createTimer : createTimer ?? other.createTimer, 136 createTimer : createTimer ?? other.createTimer,
206 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer); 137 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer,
138 print : print ?? other.print,
139 fork: fork ?? other.fork);
207 } 140 }
208 141
209 HandleUncaughtErrorHandler get handleUncaughtError; 142 HandleUncaughtErrorHandler get handleUncaughtError;
210 RunHandler get run; 143 RunHandler get run;
211 RunUnaryHandler get runUnary; 144 RunUnaryHandler get runUnary;
212 RunBinaryHandler get runBinary; 145 RunBinaryHandler get runBinary;
213 RegisterCallbackHandler get registerCallback; 146 RegisterCallbackHandler get registerCallback;
214 RegisterUnaryCallbackHandler get registerUnaryCallback; 147 RegisterUnaryCallbackHandler get registerUnaryCallback;
215 RegisterBinaryCallbackHandler get registerBinaryCallback; 148 RegisterBinaryCallbackHandler get registerBinaryCallback;
216 ErrorCallbackHandler get errorCallback; 149 ErrorCallbackHandler get errorCallback;
217 ScheduleMicrotaskHandler get scheduleMicrotask; 150 ScheduleMicrotaskHandler get scheduleMicrotask;
218 /// *Experimental*. Might disappear without warning. 151 CreateTimerHandler get createTimer;
219 CreateTaskHandler get createTask; 152 CreatePeriodicTimerHandler get createPeriodicTimer;
220 /// *Experimental*. Might disappear without warning.
221 RunTaskHandler get runTask;
222 PrintHandler get print; 153 PrintHandler get print;
223 ForkHandler get fork; 154 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;
229 } 155 }
230 156
231 /** 157 /**
232 * Internal [ZoneSpecification] class. 158 * Internal [ZoneSpecification] class.
233 * 159 *
234 * The implementation wants to rely on the fact that the getters cannot change 160 * The implementation wants to rely on the fact that the getters cannot change
235 * dynamically. We thus require users to go through the redirecting 161 * dynamically. We thus require users to go through the redirecting
236 * [ZoneSpecification] constructor which instantiates this class. 162 * [ZoneSpecification] constructor which instantiates this class.
237 */ 163 */
238 class _ZoneSpecification implements ZoneSpecification { 164 class _ZoneSpecification implements ZoneSpecification {
239 const _ZoneSpecification({ 165 const _ZoneSpecification({
240 this.handleUncaughtError: null, 166 this.handleUncaughtError: null,
241 this.run: null, 167 this.run: null,
242 this.runUnary: null, 168 this.runUnary: null,
243 this.runBinary: null, 169 this.runBinary: null,
244 this.registerCallback: null, 170 this.registerCallback: null,
245 this.registerUnaryCallback: null, 171 this.registerUnaryCallback: null,
246 this.registerBinaryCallback: null, 172 this.registerBinaryCallback: null,
247 this.errorCallback: null, 173 this.errorCallback: null,
248 this.scheduleMicrotask: null, 174 this.scheduleMicrotask: null,
249 this.createTask: null, 175 this.createTimer: null,
250 this.runTask: null, 176 this.createPeriodicTimer: null,
251 this.print: null, 177 this.print: null,
252 this.fork: null, 178 this.fork: null
253 // TODO(floitsch): deprecate once tasks are non-experimental.
254 this.createTimer: null,
255 // TODO(floitsch): deprecate once tasks are non-experimental.
256 this.createPeriodicTimer: null
257 }); 179 });
258 180
259 final HandleUncaughtErrorHandler handleUncaughtError; 181 final HandleUncaughtErrorHandler handleUncaughtError;
260 final RunHandler run; 182 final RunHandler run;
261 final RunUnaryHandler runUnary; 183 final RunUnaryHandler runUnary;
262 final RunBinaryHandler runBinary; 184 final RunBinaryHandler runBinary;
263 final RegisterCallbackHandler registerCallback; 185 final RegisterCallbackHandler registerCallback;
264 final RegisterUnaryCallbackHandler registerUnaryCallback; 186 final RegisterUnaryCallbackHandler registerUnaryCallback;
265 final RegisterBinaryCallbackHandler registerBinaryCallback; 187 final RegisterBinaryCallbackHandler registerBinaryCallback;
266 final ErrorCallbackHandler errorCallback; 188 final ErrorCallbackHandler errorCallback;
267 final ScheduleMicrotaskHandler scheduleMicrotask; 189 final ScheduleMicrotaskHandler scheduleMicrotask;
268 final CreateTaskHandler createTask; 190 final CreateTimerHandler createTimer;
269 final RunTaskHandler runTask; 191 final CreatePeriodicTimerHandler createPeriodicTimer;
270 final PrintHandler print; 192 final PrintHandler print;
271 final ForkHandler fork; 193 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;
277 } 194 }
278 195
279 /** 196 /**
280 * This class wraps zones for delegation. 197 * This class wraps zones for delegation.
281 * 198 *
282 * When forwarding to parent zones one can't just invoke the parent zone's 199 * When forwarding to parent zones one can't just invoke the parent zone's
283 * exposed functions (like [Zone.run]), but one needs to provide more 200 * exposed functions (like [Zone.run]), but one needs to provide more
284 * information (like the zone the `run` was initiated). Zone callbacks thus 201 * information (like the zone the `run` was initiated). Zone callbacks thus
285 * receive more information including this [ZoneDelegate] class. When delegating 202 * receive more information including this [ZoneDelegate] class. When delegating
286 * to the parent zone one should go through the given instance instead of 203 * to the parent zone one should go through the given instance instead of
287 * directly invoking the parent zone. 204 * directly invoking the parent zone.
288 */ 205 */
289 abstract class ZoneDelegate { 206 abstract class ZoneDelegate {
290 /*=R*/ handleUncaughtError/*<R>*/( 207 /*=R*/ handleUncaughtError/*<R>*/(
291 Zone zone, error, StackTrace stackTrace); 208 Zone zone, error, StackTrace stackTrace);
292 /*=R*/ run/*<R>*/(Zone zone, /*=R*/ f()); 209 /*=R*/ run/*<R>*/(Zone zone, /*=R*/ f());
293 /*=R*/ runUnary/*<R, T>*/(Zone zone, /*=R*/ f(/*=T*/ arg), /*=T*/ arg); 210 /*=R*/ runUnary/*<R, T>*/(Zone zone, /*=R*/ f(/*=T*/ arg), /*=T*/ arg);
294 /*=R*/ runBinary/*<R, T1, T2>*/(Zone zone, 211 /*=R*/ runBinary/*<R, T1, T2>*/(Zone zone,
295 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2), /*=T1*/ arg1, /*=T2*/ arg2); 212 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2), /*=T1*/ arg1, /*=T2*/ arg2);
296 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()); 213 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f());
297 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 214 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
298 Zone zone, /*=R*/ f(/*=T*/ arg)); 215 Zone zone, /*=R*/ f(/*=T*/ arg));
299 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 216 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
300 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)); 217 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2));
301 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace); 218 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace);
302 void scheduleMicrotask(Zone zone, void f()); 219 void scheduleMicrotask(Zone zone, void f());
303 220 Timer createTimer(Zone zone, Duration duration, void f());
304 /// *Experimental*. Might disappear without notice. 221 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer));
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
313 void print(Zone zone, String line); 222 void print(Zone zone, String line);
314 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues); 223 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));
320 } 224 }
321 225
322 /** 226 /**
323 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous 227 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous
324 * callbacks are executed in the zone they have been queued in. For example, 228 * callbacks are executed in the zone they have been queued in. For example,
325 * the callback of a `future.then` is executed in the same zone as the one where 229 * the callback of a `future.then` is executed in the same zone as the one where
326 * the `then` was invoked. 230 * the `then` was invoked.
327 */ 231 */
328 abstract class Zone { 232 abstract class Zone {
329 // Private constructor so that it is not possible instantiate a Zone class. 233 // Private constructor so that it is not possible instantiate a Zone class.
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 * If the [AsyncError.error] is `null`, it is replaced by a [NullThrownError]. 404 * If the [AsyncError.error] is `null`, it is replaced by a [NullThrownError].
501 */ 405 */
502 AsyncError errorCallback(Object error, StackTrace stackTrace); 406 AsyncError errorCallback(Object error, StackTrace stackTrace);
503 407
504 /** 408 /**
505 * Runs [f] asynchronously in this zone. 409 * Runs [f] asynchronously in this zone.
506 */ 410 */
507 void scheduleMicrotask(void f()); 411 void scheduleMicrotask(void f());
508 412
509 /** 413 /**
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 /**
595 * Creates a Timer where the callback is executed in this zone. 414 * Creates a Timer where the callback is executed in this zone.
596 */ 415 */
597 // TODO(floitsch): deprecate once tasks are non-experimental.
598 Timer createTimer(Duration duration, void callback()); 416 Timer createTimer(Duration duration, void callback());
599 417
600 /** 418 /**
601 * Creates a periodic Timer where the callback is executed in this zone. 419 * Creates a periodic Timer where the callback is executed in this zone.
602 */ 420 */
603 // TODO(floitsch): deprecate once tasks are non-experimental.
604 Timer createPeriodicTimer(Duration period, void callback(Timer timer)); 421 Timer createPeriodicTimer(Duration period, void callback(Timer timer));
605 422
606 /** 423 /**
607 * Prints the given [line]. 424 * Prints the given [line].
608 */ 425 */
609 void print(String line); 426 void print(String line);
610 427
611 /** 428 /**
612 * Call to enter the Zone. 429 * Call to enter the Zone.
613 * 430 *
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 as Object/*=R*/; 516 as Object/*=R*/;
700 } 517 }
701 518
702 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()) { 519 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()) {
703 var implementation = _delegationTarget._registerCallback; 520 var implementation = _delegationTarget._registerCallback;
704 _Zone implZone = implementation.zone; 521 _Zone implZone = implementation.zone;
705 RegisterCallbackHandler handler = implementation.function; 522 RegisterCallbackHandler handler = implementation.function;
706 // TODO(floitsch): make this a generic method call on '<R>' once it's 523 // TODO(floitsch): make this a generic method call on '<R>' once it's
707 // supported. Remove the unnecessary cast. 524 // supported. Remove the unnecessary cast.
708 return handler(implZone, _parentDelegate(implZone), zone, f) 525 return handler(implZone, _parentDelegate(implZone), zone, f)
709 as dynamic/*=ZoneCallback<R>*/; 526 as Object/*=ZoneCallback<R>*/;
710 } 527 }
711 528
712 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 529 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
713 Zone zone, /*=R*/ f(/*=T*/ arg)) { 530 Zone zone, /*=R*/ f(/*=T*/ arg)) {
714 var implementation = _delegationTarget._registerUnaryCallback; 531 var implementation = _delegationTarget._registerUnaryCallback;
715 _Zone implZone = implementation.zone; 532 _Zone implZone = implementation.zone;
716 RegisterUnaryCallbackHandler handler = implementation.function; 533 RegisterUnaryCallbackHandler handler = implementation.function;
717 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 534 // TODO(floitsch): make this a generic method call on '<R, T>' once it's
718 // supported. Remove the unnecessary cast. 535 // supported. Remove the unnecessary cast.
719 return handler(implZone, _parentDelegate(implZone), zone, f) 536 return handler(implZone, _parentDelegate(implZone), zone, f)
720 as dynamic/*=ZoneUnaryCallback<R, T>*/; 537 as Object/*=ZoneUnaryCallback<R, T>*/;
721 } 538 }
722 539
723 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 540 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
724 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) { 541 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) {
725 var implementation = _delegationTarget._registerBinaryCallback; 542 var implementation = _delegationTarget._registerBinaryCallback;
726 _Zone implZone = implementation.zone; 543 _Zone implZone = implementation.zone;
727 RegisterBinaryCallbackHandler handler = implementation.function; 544 RegisterBinaryCallbackHandler handler = implementation.function;
728 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 545 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once
729 // it's supported. Remove the unnecessary cast. 546 // it's supported. Remove the unnecessary cast.
730 return handler(implZone, _parentDelegate(implZone), zone, f) 547 return handler(implZone, _parentDelegate(implZone), zone, f)
731 as dynamic/*=ZoneBinaryCallback<R, T1, T2>*/; 548 as Object/*=ZoneBinaryCallback<R, T1, T2>*/;
732 } 549 }
733 550
734 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) { 551 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) {
735 var implementation = _delegationTarget._errorCallback; 552 var implementation = _delegationTarget._errorCallback;
736 _Zone implZone = implementation.zone; 553 _Zone implZone = implementation.zone;
737 if (identical(implZone, _ROOT_ZONE)) return null; 554 if (identical(implZone, _ROOT_ZONE)) return null;
738 ErrorCallbackHandler handler = implementation.function; 555 ErrorCallbackHandler handler = implementation.function;
739 return handler(implZone, _parentDelegate(implZone), zone, 556 return handler(implZone, _parentDelegate(implZone), zone,
740 error, stackTrace); 557 error, stackTrace);
741 } 558 }
742 559
743 void scheduleMicrotask(Zone zone, f()) { 560 void scheduleMicrotask(Zone zone, f()) {
744 var implementation = _delegationTarget._scheduleMicrotask; 561 var implementation = _delegationTarget._scheduleMicrotask;
745 _Zone implZone = implementation.zone; 562 _Zone implZone = implementation.zone;
746 ScheduleMicrotaskHandler handler = implementation.function; 563 ScheduleMicrotaskHandler handler = implementation.function;
747 handler(implZone, _parentDelegate(implZone), zone, f); 564 handler(implZone, _parentDelegate(implZone), zone, f);
748 } 565 }
749 566
750 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/( 567 Timer createTimer(Zone zone, Duration duration, void f()) {
751 Zone zone, TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specificat ion) { 568 var implementation = _delegationTarget._createTimer;
752 var implementation = _delegationTarget._createTask;
753 _Zone implZone = implementation.zone; 569 _Zone implZone = implementation.zone;
754 // TODO(floitsch): make the handler call a generic method call on '<T, S>' 570 CreateTimerHandler handler = implementation.function;
755 // once it's supported. Remove the unnecessary cast. 571 return handler(implZone, _parentDelegate(implZone), zone, duration, f);
756 var handler =
757 implementation.function as CreateTaskHandler/*<T, S>*/;
758 return handler(
759 implZone, _parentDelegate(implZone), zone, create, specification);
760 } 572 }
761 573
762 void runTask/*<T, A>*/(Zone zone, TaskRun run, Object /*=T*/ task, 574 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) {
763 Object /*=A*/ argument) { 575 var implementation = _delegationTarget._createPeriodicTimer;
764 var implementation = _delegationTarget._runTask;
765 _Zone implZone = implementation.zone; 576 _Zone implZone = implementation.zone;
766 RunTaskHandler handler = implementation.function; 577 CreatePeriodicTimerHandler handler = implementation.function;
767 // TODO(floitsch): make this a generic call on '<T, A>'. 578 return handler(implZone, _parentDelegate(implZone), zone, period, f);
768 handler(implZone, _parentDelegate(implZone), zone, run, task, argument);
769 } 579 }
770 580
771 void print(Zone zone, String line) { 581 void print(Zone zone, String line) {
772 var implementation = _delegationTarget._print; 582 var implementation = _delegationTarget._print;
773 _Zone implZone = implementation.zone; 583 _Zone implZone = implementation.zone;
774 PrintHandler handler = implementation.function; 584 PrintHandler handler = implementation.function;
775 handler(implZone, _parentDelegate(implZone), zone, line); 585 handler(implZone, _parentDelegate(implZone), zone, line);
776 } 586 }
777 587
778 Zone fork(Zone zone, ZoneSpecification specification, 588 Zone fork(Zone zone, ZoneSpecification specification,
779 Map zoneValues) { 589 Map zoneValues) {
780 var implementation = _delegationTarget._fork; 590 var implementation = _delegationTarget._fork;
781 _Zone implZone = implementation.zone; 591 _Zone implZone = implementation.zone;
782 ForkHandler handler = implementation.function; 592 ForkHandler handler = implementation.function;
783 return handler( 593 return handler(
784 implZone, _parentDelegate(implZone), zone, specification, zoneValues); 594 implZone, _parentDelegate(implZone), zone, specification, zoneValues);
785 } 595 }
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 }
802 } 596 }
803 597
804 598
805 /** 599 /**
806 * Base class for Zone implementations. 600 * Base class for Zone implementations.
807 */ 601 */
808 abstract class _Zone implements Zone { 602 abstract class _Zone implements Zone {
809 const _Zone(); 603 const _Zone();
810 604
811 _ZoneFunction<RunHandler> get _run; 605 _ZoneFunction<RunHandler> get _run;
812 _ZoneFunction<RunUnaryHandler> get _runUnary; 606 _ZoneFunction<RunUnaryHandler> get _runUnary;
813 _ZoneFunction<RunBinaryHandler> get _runBinary; 607 _ZoneFunction<RunBinaryHandler> get _runBinary;
814 _ZoneFunction<RegisterCallbackHandler> get _registerCallback; 608 _ZoneFunction<RegisterCallbackHandler> get _registerCallback;
815 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback; 609 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback;
816 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback; 610 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback;
817 _ZoneFunction<ErrorCallbackHandler> get _errorCallback; 611 _ZoneFunction<ErrorCallbackHandler> get _errorCallback;
818 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask; 612 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask;
819 _ZoneFunction<CreateTaskHandler> get _createTask; 613 _ZoneFunction<CreateTimerHandler> get _createTimer;
820 _ZoneFunction<RunTaskHandler> get _runTask; 614 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer;
821 _ZoneFunction<PrintHandler> get _print; 615 _ZoneFunction<PrintHandler> get _print;
822 _ZoneFunction<ForkHandler> get _fork; 616 _ZoneFunction<ForkHandler> get _fork;
823 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError; 617 _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
830 _Zone get parent; 618 _Zone get parent;
831 ZoneDelegate get _delegate; 619 ZoneDelegate get _delegate;
832 Map get _map; 620 Map get _map;
833 621
834 bool inSameErrorZone(Zone otherZone) { 622 bool inSameErrorZone(Zone otherZone) {
835 return identical(this, otherZone) || 623 return identical(this, otherZone) ||
836 identical(errorZone, otherZone.errorZone); 624 identical(errorZone, otherZone.errorZone);
837 } 625 }
838 } 626 }
839 627
840 class _CustomZone extends _Zone { 628 class _CustomZone extends _Zone {
841 // The actual zone and implementation of each of these 629 // The actual zone and implementation of each of these
842 // inheritable zone functions. 630 // inheritable zone functions.
843 _ZoneFunction<RunHandler> _run; 631 _ZoneFunction<RunHandler> _run;
844 _ZoneFunction<RunUnaryHandler> _runUnary; 632 _ZoneFunction<RunUnaryHandler> _runUnary;
845 _ZoneFunction<RunBinaryHandler> _runBinary; 633 _ZoneFunction<RunBinaryHandler> _runBinary;
846 _ZoneFunction<RegisterCallbackHandler> _registerCallback; 634 _ZoneFunction<RegisterCallbackHandler> _registerCallback;
847 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback; 635 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback;
848 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback; 636 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback;
849 _ZoneFunction<ErrorCallbackHandler> _errorCallback; 637 _ZoneFunction<ErrorCallbackHandler> _errorCallback;
850 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask; 638 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask;
851 _ZoneFunction<CreateTaskHandler> _createTask; 639 _ZoneFunction<CreateTimerHandler> _createTimer;
852 _ZoneFunction<RunTaskHandler> _runTask; 640 _ZoneFunction<CreatePeriodicTimerHandler> _createPeriodicTimer;
853 _ZoneFunction<PrintHandler> _print; 641 _ZoneFunction<PrintHandler> _print;
854 _ZoneFunction<ForkHandler> _fork; 642 _ZoneFunction<ForkHandler> _fork;
855 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError; 643 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError;
856 644
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
862 // A cached delegate to this zone. 645 // A cached delegate to this zone.
863 ZoneDelegate _delegateCache; 646 ZoneDelegate _delegateCache;
864 647
865 /// The parent zone. 648 /// The parent zone.
866 final _Zone parent; 649 final _Zone parent;
867 650
868 /// The zone's scoped value declaration map. 651 /// The zone's scoped value declaration map.
869 /// 652 ///
870 /// This is always a [HashMap]. 653 /// This is always a [HashMap].
871 final Map _map; 654 final Map _map;
(...skipping 30 matching lines...) Expand all
902 this, specification.registerBinaryCallback) 685 this, specification.registerBinaryCallback)
903 : parent._registerBinaryCallback; 686 : parent._registerBinaryCallback;
904 _errorCallback = (specification.errorCallback != null) 687 _errorCallback = (specification.errorCallback != null)
905 ? new _ZoneFunction<ErrorCallbackHandler>( 688 ? new _ZoneFunction<ErrorCallbackHandler>(
906 this, specification.errorCallback) 689 this, specification.errorCallback)
907 : parent._errorCallback; 690 : parent._errorCallback;
908 _scheduleMicrotask = (specification.scheduleMicrotask != null) 691 _scheduleMicrotask = (specification.scheduleMicrotask != null)
909 ? new _ZoneFunction<ScheduleMicrotaskHandler>( 692 ? new _ZoneFunction<ScheduleMicrotaskHandler>(
910 this, specification.scheduleMicrotask) 693 this, specification.scheduleMicrotask)
911 : parent._scheduleMicrotask; 694 : parent._scheduleMicrotask;
912 _createTask = (specification.createTask != null) 695 _createTimer = (specification.createTimer != null)
913 ? new _ZoneFunction<CreateTaskHandler>( 696 ? new _ZoneFunction<CreateTimerHandler>(this, specification.createTimer)
914 this, specification.createTask) 697 : parent._createTimer;
915 : parent._createTask; 698 _createPeriodicTimer = (specification.createPeriodicTimer != null)
916 _runTask = (specification.runTask != null) 699 ? new _ZoneFunction<CreatePeriodicTimerHandler>(
917 ? new _ZoneFunction<RunTaskHandler>( 700 this, specification.createPeriodicTimer)
918 this, specification.runTask) 701 : parent._createPeriodicTimer;
919 : parent._runTask;
920 _print = (specification.print != null) 702 _print = (specification.print != null)
921 ? new _ZoneFunction<PrintHandler>(this, specification.print) 703 ? new _ZoneFunction<PrintHandler>(this, specification.print)
922 : parent._print; 704 : parent._print;
923 _fork = (specification.fork != null) 705 _fork = (specification.fork != null)
924 ? new _ZoneFunction<ForkHandler>(this, specification.fork) 706 ? new _ZoneFunction<ForkHandler>(this, specification.fork)
925 : parent._fork; 707 : parent._fork;
926 _handleUncaughtError = (specification.handleUncaughtError != null) 708 _handleUncaughtError = (specification.handleUncaughtError != null)
927 ? new _ZoneFunction<HandleUncaughtErrorHandler>( 709 ? new _ZoneFunction<HandleUncaughtErrorHandler>(
928 this, specification.handleUncaughtError) 710 this, specification.handleUncaughtError)
929 : parent._handleUncaughtError; 711 : 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;
940 } 712 }
941 713
942 /** 714 /**
943 * The closest error-handling zone. 715 * The closest error-handling zone.
944 * 716 *
945 * Returns `this` if `this` has an error-handler. Otherwise returns the 717 * Returns `this` if `this` has an error-handler. Otherwise returns the
946 * parent's error-zone. 718 * parent's error-zone.
947 */ 719 */
948 Zone get errorZone => _handleUncaughtError.zone; 720 Zone get errorZone => _handleUncaughtError.zone;
949 721
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 } 852 }
1081 853
1082 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) { 854 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) {
1083 var implementation = this._registerCallback; 855 var implementation = this._registerCallback;
1084 assert(implementation != null); 856 assert(implementation != null);
1085 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 857 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1086 RegisterCallbackHandler handler = implementation.function; 858 RegisterCallbackHandler handler = implementation.function;
1087 // TODO(floitsch): make this a generic method call on '<R>' once it's 859 // TODO(floitsch): make this a generic method call on '<R>' once it's
1088 // supported. Remove the unnecessary cast. 860 // supported. Remove the unnecessary cast.
1089 return handler(implementation.zone, parentDelegate, this, callback) 861 return handler(implementation.zone, parentDelegate, this, callback)
1090 as dynamic/*=ZoneCallback<R>*/; 862 as Object/*=ZoneCallback<R>*/;
1091 } 863 }
1092 864
1093 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 865 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
1094 /*=R*/ callback(/*=T*/ arg)) { 866 /*=R*/ callback(/*=T*/ arg)) {
1095 var implementation = this._registerUnaryCallback; 867 var implementation = this._registerUnaryCallback;
1096 assert(implementation != null); 868 assert(implementation != null);
1097 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 869 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1098 RegisterUnaryCallbackHandler handler = implementation.function; 870 RegisterUnaryCallbackHandler handler = implementation.function;
1099 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 871 // TODO(floitsch): make this a generic method call on '<R, T>' once it's
1100 // supported. Remove the unnecessary cast. 872 // supported. Remove the unnecessary cast.
1101 return handler(implementation.zone, parentDelegate, this, callback) 873 return handler(implementation.zone, parentDelegate, this, callback)
1102 as dynamic/*=ZoneUnaryCallback<R, T>*/; 874 as Object/*=ZoneUnaryCallback<R, T>*/;
1103 } 875 }
1104 876
1105 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 877 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
1106 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) { 878 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) {
1107 var implementation = this._registerBinaryCallback; 879 var implementation = this._registerBinaryCallback;
1108 assert(implementation != null); 880 assert(implementation != null);
1109 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 881 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1110 RegisterBinaryCallbackHandler handler = implementation.function; 882 RegisterBinaryCallbackHandler handler = implementation.function;
1111 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 883 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once
1112 // it's supported. Remove the unnecessary cast. 884 // it's supported. Remove the unnecessary cast.
1113 return handler(implementation.zone, parentDelegate, this, callback) 885 return handler(implementation.zone, parentDelegate, this, callback)
1114 as dynamic/*=ZoneBinaryCallback<R, T1, T2>*/; 886 as Object/*=ZoneBinaryCallback<R, T1, T2>*/;
1115 } 887 }
1116 888
1117 AsyncError errorCallback(Object error, StackTrace stackTrace) { 889 AsyncError errorCallback(Object error, StackTrace stackTrace) {
1118 var implementation = this._errorCallback; 890 var implementation = this._errorCallback;
1119 assert(implementation != null); 891 assert(implementation != null);
1120 final Zone implementationZone = implementation.zone; 892 final Zone implementationZone = implementation.zone;
1121 if (identical(implementationZone, _ROOT_ZONE)) return null; 893 if (identical(implementationZone, _ROOT_ZONE)) return null;
1122 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone); 894 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone);
1123 ErrorCallbackHandler handler = implementation.function; 895 ErrorCallbackHandler handler = implementation.function;
1124 return handler( 896 return handler(
1125 implementationZone, parentDelegate, this, error, stackTrace); 897 implementationZone, parentDelegate, this, error, stackTrace);
1126 } 898 }
1127 899
1128 void scheduleMicrotask(void f()) { 900 void scheduleMicrotask(void f()) {
1129 var implementation = this._scheduleMicrotask; 901 var implementation = this._scheduleMicrotask;
1130 assert(implementation != null); 902 assert(implementation != null);
1131 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 903 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1132 ScheduleMicrotaskHandler handler = implementation.function; 904 ScheduleMicrotaskHandler handler = implementation.function;
1133 handler(implementation.zone, parentDelegate, this, f); 905 return handler(implementation.zone, parentDelegate, this, f);
1134 } 906 }
1135 907
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.
1167 Timer createTimer(Duration duration, void f()) { 908 Timer createTimer(Duration duration, void f()) {
1168 var implementation = this._createTimer; 909 var implementation = this._createTimer;
1169 assert(implementation != null); 910 assert(implementation != null);
1170 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 911 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1171 CreateTimerHandler handler = implementation.function; 912 CreateTimerHandler handler = implementation.function;
1172 return handler(implementation.zone, parentDelegate, this, duration, f); 913 return handler(implementation.zone, parentDelegate, this, duration, f);
1173 } 914 }
1174 915
1175 // TODO(floitsch): deprecate once tasks are non-experimental.
1176 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 916 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
1177 var implementation = this._createPeriodicTimer; 917 var implementation = this._createPeriodicTimer;
1178 assert(implementation != null); 918 assert(implementation != null);
1179 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 919 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1180 CreatePeriodicTimerHandler handler = implementation.function; 920 CreatePeriodicTimerHandler handler = implementation.function;
1181 return handler( 921 return handler(
1182 implementation.zone, parentDelegate, this, duration, f); 922 implementation.zone, parentDelegate, this, duration, f);
1183 } 923 }
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 }
1184 } 932 }
1185 933
1186 /*=R*/ _rootHandleUncaughtError/*<R>*/( 934 /*=R*/ _rootHandleUncaughtError/*<R>*/(
1187 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) { 935 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) {
1188 _schedulePriorityAsyncCallback(() { 936 _schedulePriorityAsyncCallback(() {
1189 if (error == null) error = new NullThrownError(); 937 if (error == null) error = new NullThrownError();
1190 if (stackTrace == null) throw error; 938 if (stackTrace == null) throw error;
1191 _rethrow(error, stackTrace); 939 _rethrow(error, stackTrace);
1192 }); 940 });
1193 } 941 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1251 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) { 999 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) {
1252 if (!identical(_ROOT_ZONE, zone)) { 1000 if (!identical(_ROOT_ZONE, zone)) {
1253 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone); 1001 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone);
1254 f = zone.bindCallback(f, runGuarded: hasErrorHandler); 1002 f = zone.bindCallback(f, runGuarded: hasErrorHandler);
1255 // Use root zone as event zone if the function is already bound. 1003 // Use root zone as event zone if the function is already bound.
1256 zone = _ROOT_ZONE; 1004 zone = _ROOT_ZONE;
1257 } 1005 }
1258 _scheduleAsyncCallback(f); 1006 _scheduleAsyncCallback(f);
1259 } 1007 }
1260 1008
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
1285 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone, 1009 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone,
1286 Duration duration, void callback()) { 1010 Duration duration, void callback()) {
1287 return new Timer._task(zone, duration, callback); 1011 if (!identical(_ROOT_ZONE, zone)) {
1012 callback = zone.bindCallback(callback);
1013 }
1014 return Timer._createTimer(duration, callback);
1288 } 1015 }
1289 1016
1290 Timer _rootCreatePeriodicTimer( 1017 Timer _rootCreatePeriodicTimer(
1291 Zone self, ZoneDelegate parent, Zone zone, 1018 Zone self, ZoneDelegate parent, Zone zone,
1292 Duration duration, void callback(Timer timer)) { 1019 Duration duration, void callback(Timer timer)) {
1293 return new Timer._periodicTask(zone, duration, callback); 1020 if (!identical(_ROOT_ZONE, zone)) {
1021 // TODO(floitsch): the return type should be 'void'.
1022 callback = zone.bindUnaryCallback/*<dynamic, Timer>*/(callback);
1023 }
1024 return Timer._createPeriodicTimer(duration, callback);
1294 } 1025 }
1295 1026
1296 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) { 1027 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) {
1297 printToConsole(line); 1028 printToConsole(line);
1298 } 1029 }
1299 1030
1300 void _printToZone(String line) { 1031 void _printToZone(String line) {
1301 Zone.current.print(line); 1032 Zone.current.print(line);
1302 } 1033 }
1303 1034
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 const _ZoneFunction<RegisterUnaryCallbackHandler>( 1075 const _ZoneFunction<RegisterUnaryCallbackHandler>(
1345 _ROOT_ZONE, _rootRegisterUnaryCallback); 1076 _ROOT_ZONE, _rootRegisterUnaryCallback);
1346 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback => 1077 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback =>
1347 const _ZoneFunction<RegisterBinaryCallbackHandler>( 1078 const _ZoneFunction<RegisterBinaryCallbackHandler>(
1348 _ROOT_ZONE, _rootRegisterBinaryCallback); 1079 _ROOT_ZONE, _rootRegisterBinaryCallback);
1349 _ZoneFunction<ErrorCallbackHandler> get _errorCallback => 1080 _ZoneFunction<ErrorCallbackHandler> get _errorCallback =>
1350 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback); 1081 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback);
1351 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask => 1082 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask =>
1352 const _ZoneFunction<ScheduleMicrotaskHandler>( 1083 const _ZoneFunction<ScheduleMicrotaskHandler>(
1353 _ROOT_ZONE, _rootScheduleMicrotask); 1084 _ROOT_ZONE, _rootScheduleMicrotask);
1354 _ZoneFunction<CreateTaskHandler> get _createTask => 1085 _ZoneFunction<CreateTimerHandler> get _createTimer =>
1355 const _ZoneFunction<CreateTaskHandler>(_ROOT_ZONE, _rootCreateTask); 1086 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer);
1356 _ZoneFunction<RunTaskHandler> get _runTask => 1087 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer =>
1357 const _ZoneFunction<RunTaskHandler>(_ROOT_ZONE, _rootRunTask); 1088 const _ZoneFunction<CreatePeriodicTimerHandler>(_ROOT_ZONE, _rootCreatePer iodicTimer);
1358 _ZoneFunction<PrintHandler> get _print => 1089 _ZoneFunction<PrintHandler> get _print =>
1359 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint); 1090 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint);
1360 _ZoneFunction<ForkHandler> get _fork => 1091 _ZoneFunction<ForkHandler> get _fork =>
1361 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork); 1092 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork);
1362 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError => 1093 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError =>
1363 const _ZoneFunction<HandleUncaughtErrorHandler>( 1094 const _ZoneFunction<HandleUncaughtErrorHandler>(
1364 _ROOT_ZONE, _rootHandleUncaughtError); 1095 _ROOT_ZONE, _rootHandleUncaughtError);
1365 1096
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
1374 // The parent zone. 1097 // The parent zone.
1375 _Zone get parent => null; 1098 _Zone get parent => null;
1376 1099
1377 /// The zone's scoped value declaration map. 1100 /// The zone's scoped value declaration map.
1378 /// 1101 ///
1379 /// This is always a [HashMap]. 1102 /// This is always a [HashMap].
1380 Map get _map => _rootMap; 1103 Map get _map => _rootMap;
1381 1104
1382 static Map _rootMap = new HashMap(); 1105 static Map _rootMap = new HashMap();
1383 1106
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 1218
1496 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 1219 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
1497 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f; 1220 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f;
1498 1221
1499 AsyncError errorCallback(Object error, StackTrace stackTrace) => null; 1222 AsyncError errorCallback(Object error, StackTrace stackTrace) => null;
1500 1223
1501 void scheduleMicrotask(void f()) { 1224 void scheduleMicrotask(void f()) {
1502 _rootScheduleMicrotask(null, null, this, f); 1225 _rootScheduleMicrotask(null, null, this, f);
1503 } 1226 }
1504 1227
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
1515 Timer createTimer(Duration duration, void f()) { 1228 Timer createTimer(Duration duration, void f()) {
1516 return Timer._createTimer(duration, f); 1229 return Timer._createTimer(duration, f);
1517 } 1230 }
1518 1231
1519 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 1232 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
1520 return Timer._createPeriodicTimer(duration, f); 1233 return Timer._createPeriodicTimer(duration, f);
1521 } 1234 }
1522 1235
1523 void print(String line) { 1236 void print(String line) {
1524 printToConsole(line); 1237 printToConsole(line);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1583 handleUncaughtError: errorHandler); 1296 handleUncaughtError: errorHandler);
1584 } 1297 }
1585 Zone zone = Zone.current.fork(specification: zoneSpecification, 1298 Zone zone = Zone.current.fork(specification: zoneSpecification,
1586 zoneValues: zoneValues); 1299 zoneValues: zoneValues);
1587 if (onError != null) { 1300 if (onError != null) {
1588 return zone.runGuarded(body); 1301 return zone.runGuarded(body);
1589 } else { 1302 } else {
1590 return zone.run(body); 1303 return zone.run(body);
1591 } 1304 }
1592 } 1305 }
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