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

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

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