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

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

Issue 1848933002: Add tasks to zones. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Mark tasks as experimental. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sdk/lib/async/timer.dart ('k') | tests/lib/async/zone_task_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.async; 5 part of dart.async;
6 6
7 typedef R ZoneCallback<R>(); 7 typedef R ZoneCallback<R>();
8 typedef R ZoneUnaryCallback<R, T>(T arg); 8 typedef R ZoneUnaryCallback<R, T>(T arg);
9 typedef R ZoneBinaryCallback<R, T1, T2>(T1 arg1, T2 arg2); 9 typedef R ZoneBinaryCallback<R, T1, T2>(T1 arg1, T2 arg2);
10 10
11 /// *Experimental*. Might disappear without warning.
12 typedef T TaskCreate<T, S extends TaskSpecification>(
13 S specification, Zone zone);
14 /// *Experimental*. Might disappear without warning.
15 typedef void TaskRun<T, A>(T task, A arg);
16
17
11 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic 18 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic
12 // functions. 19 // functions.
13 /*ABUSE*/ 20 /*ABUSE*/
14 typedef R HandleUncaughtErrorHandler<R>( 21 typedef R HandleUncaughtErrorHandler<R>(
15 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace); 22 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace);
16 /*ABUSE*/ 23 /*ABUSE*/
17 typedef R RunHandler<R>(Zone self, ZoneDelegate parent, Zone zone, R f()); 24 typedef R RunHandler<R>(Zone self, ZoneDelegate parent, Zone zone, R f());
18 /*ABUSE*/ 25 /*ABUSE*/
19 typedef R RunUnaryHandler<R, T>( 26 typedef R RunUnaryHandler<R, T>(
20 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg); 27 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg);
21 /*ABUSE*/ 28 /*ABUSE*/
22 typedef R RunBinaryHandler<R, T1, T2>( 29 typedef R RunBinaryHandler<R, T1, T2>(
23 Zone self, ZoneDelegate parent, Zone zone, 30 Zone self, ZoneDelegate parent, Zone zone,
24 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); 31 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2);
25 /*ABUSE*/ 32 /*ABUSE*/
26 typedef ZoneCallback<R> RegisterCallbackHandler<R>( 33 typedef ZoneCallback<R> RegisterCallbackHandler<R>(
27 Zone self, ZoneDelegate parent, Zone zone, R f()); 34 Zone self, ZoneDelegate parent, Zone zone, R f());
28 /*ABUSE*/ 35 /*ABUSE*/
29 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>( 36 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>(
30 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)); 37 Zone self, ZoneDelegate parent, Zone zone, R f(T arg));
31 /*ABUSE*/ 38 /*ABUSE*/
32 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>( 39 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>(
33 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)); 40 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2));
34 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent, 41 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent,
35 Zone zone, Object error, StackTrace stackTrace); 42 Zone zone, Object error, StackTrace stackTrace);
43 /// *Experimental*. Might disappear without warning.
44 /*ABUSE*/
45 typedef Object/*=T*/ CreateTaskHandler/*<T, S extends TaskSpecification>*/(
46 Zone self, ZoneDelegate parent, Zone zone,
47 TaskCreate/*<T, S>*/ create, TaskSpecification/*=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, Object/*=T*/ task, Object/*=A*/ arg);
36 typedef void ScheduleMicrotaskHandler( 52 typedef void ScheduleMicrotaskHandler(
37 Zone self, ZoneDelegate parent, Zone zone, void f()); 53 Zone self, ZoneDelegate parent, Zone zone, void f());
38 typedef Timer CreateTimerHandler(
39 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f());
40 typedef Timer CreatePeriodicTimerHandler(
41 Zone self, ZoneDelegate parent, Zone zone,
42 Duration period, void f(Timer timer));
43 typedef void PrintHandler( 54 typedef void PrintHandler(
44 Zone self, ZoneDelegate parent, Zone zone, String line); 55 Zone self, ZoneDelegate parent, Zone zone, String line);
45 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone, 56 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone,
46 ZoneSpecification specification, 57 ZoneSpecification specification,
47 Map zoneValues); 58 Map zoneValues);
48 59
60 // Typedefs for methods, that will be deprecated once tasks are
61 // non-experimental.
Lasse Reichstein Nielsen 2016/07/01 11:39:11 remove comma. This is not a dart-doc, so it doesn
floitsch 2016/07/02 00:42:42 Done.
62 typedef Timer CreateTimerHandler(
63 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f());
64 typedef Timer CreatePeriodicTimerHandler(
65 Zone self, ZoneDelegate parent, Zone zone,
66 Duration period, void f(Timer timer));
67
49 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */ 68 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */
50 class AsyncError implements Error { 69 class AsyncError implements Error {
51 final Object error; 70 final Object error;
52 final StackTrace stackTrace; 71 final StackTrace stackTrace;
53 72
54 AsyncError(this.error, this.stackTrace); 73 AsyncError(this.error, this.stackTrace);
55 74
56 String toString() => '$error'; 75 String toString() => '$error';
57 } 76 }
58 77
78 /**
79 * A task specification contains the necessary information to create a task.
80 *
81 * See [Zone.createTask] for how a specification is used to create a task.
82 *
83 * Task specifications should be public and it should be possible to create
84 * new instances as a user. That is, custom zones should be able to replace
85 * an existing specification with a modified one.
86 *
87 * *Experimental*. This class might disappear without warning.
88 */
89 abstract class TaskSpecification {
90 /**
91 * Description of the task.
92 *
93 * This string is unused by the root-zone, but might be used for debugging,
94 * and testing. As such, it should be relatively unique in its category.
95 *
96 * As a general guideline we recommend: "package-name.library.action".
97 */
98 String get name;
99
100 /**
101 * Whether the scheduled task triggers at most once.
102 *
103 * If the task is not a one-shot task, it may need to be canceled to prevent
104 * further iterations of the task.
105 */
106 bool get isOneShot;
107 }
59 108
60 class _ZoneFunction<T extends Function> { 109 class _ZoneFunction<T extends Function> {
61 final _Zone zone; 110 final _Zone zone;
62 final T function; 111 final T function;
112
63 const _ZoneFunction(this.zone, this.function); 113 const _ZoneFunction(this.zone, this.function);
64 } 114 }
65 115
66 /** 116 /**
67 * This class provides the specification for a forked zone. 117 * This class provides the specification for a forked zone.
68 * 118 *
69 * When forking a new zone (see [Zone.fork]) one can override the default 119 * When forking a new zone (see [Zone.fork]) one can override the default
70 * behavior of the zone by providing callbacks. These callbacks must be 120 * behavior of the zone by providing callbacks. These callbacks must be
71 * given in an instance of this class. 121 * given in an instance of this class.
72 * 122 *
73 * Handlers have the same signature as the same-named methods on [Zone] but 123 * Handlers have the same signature as the same-named methods on [Zone] but
74 * receive three additional arguments: 124 * receive three additional arguments:
75 * 125 *
76 * 1. the zone the handlers are attached to (the "self" zone). 126 * 1. the zone the handlers are attached to (the "self" zone).
77 * 2. a [ZoneDelegate] to the parent zone. 127 * 2. a [ZoneDelegate] to the parent zone.
78 * 3. the zone that first received the request (before the request was 128 * 3. the zone that first received the request (before the request was
79 * bubbled up). 129 * bubbled up).
80 * 130 *
81 * Handlers can either stop propagation the request (by simply not calling the 131 * Handlers can either stop propagation the request (by simply not calling the
82 * parent handler), or forward to the parent zone, potentially modifying the 132 * parent handler), or forward to the parent zone, potentially modifying the
83 * arguments on the way. 133 * arguments on the way.
84 */ 134 */
85 abstract class ZoneSpecification { 135 abstract class ZoneSpecification {
86 /** 136 /**
87 * Creates a specification with the provided handlers. 137 * Creates a specification with the provided handlers.
138 *
139 * The task-related parameters ([createTask] and [runTask]) are experimental
140 * and might be removed without warning.
88 */ 141 */
89 const factory ZoneSpecification({ 142 const factory ZoneSpecification({
90 HandleUncaughtErrorHandler handleUncaughtError, 143 HandleUncaughtErrorHandler handleUncaughtError,
91 RunHandler run, 144 RunHandler run,
92 RunUnaryHandler runUnary, 145 RunUnaryHandler runUnary,
93 RunBinaryHandler runBinary, 146 RunBinaryHandler runBinary,
94 RegisterCallbackHandler registerCallback, 147 RegisterCallbackHandler registerCallback,
95 RegisterUnaryCallbackHandler registerUnaryCallback, 148 RegisterUnaryCallbackHandler registerUnaryCallback,
96 RegisterBinaryCallbackHandler registerBinaryCallback, 149 RegisterBinaryCallbackHandler registerBinaryCallback,
97 ErrorCallbackHandler errorCallback, 150 ErrorCallbackHandler errorCallback,
98 ScheduleMicrotaskHandler scheduleMicrotask, 151 ScheduleMicrotaskHandler scheduleMicrotask,
152 CreateTaskHandler createTask,
153 RunTaskHandler runTask,
154 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
99 CreateTimerHandler createTimer, 155 CreateTimerHandler createTimer,
156 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
100 CreatePeriodicTimerHandler createPeriodicTimer, 157 CreatePeriodicTimerHandler createPeriodicTimer,
101 PrintHandler print, 158 PrintHandler print,
102 ForkHandler fork 159 ForkHandler fork
103 }) = _ZoneSpecification; 160 }) = _ZoneSpecification;
104 161
105 /** 162 /**
106 * Creates a specification from [other] with the provided handlers overriding 163 * Creates a specification from [other] with the provided handlers overriding
107 * the ones in [other]. 164 * the ones in [other].
165 *
166 * The task-related parameters ([createTask] and [runTask]) are experimental
167 * and might be removed without warning.
108 */ 168 */
109 factory ZoneSpecification.from(ZoneSpecification other, { 169 factory ZoneSpecification.from(ZoneSpecification other, {
110 HandleUncaughtErrorHandler handleUncaughtError: null, 170 HandleUncaughtErrorHandler handleUncaughtError: null,
111 RunHandler run: null, 171 RunHandler run: null,
112 RunUnaryHandler runUnary: null, 172 RunUnaryHandler runUnary: null,
113 RunBinaryHandler runBinary: null, 173 RunBinaryHandler runBinary: null,
114 RegisterCallbackHandler registerCallback: null, 174 RegisterCallbackHandler registerCallback: null,
115 RegisterUnaryCallbackHandler registerUnaryCallback: null, 175 RegisterUnaryCallbackHandler registerUnaryCallback: null,
116 RegisterBinaryCallbackHandler registerBinaryCallback: null, 176 RegisterBinaryCallbackHandler registerBinaryCallback: null,
117 ErrorCallbackHandler errorCallback: null, 177 ErrorCallbackHandler errorCallback: null,
118 ScheduleMicrotaskHandler scheduleMicrotask: null, 178 ScheduleMicrotaskHandler scheduleMicrotask: null,
179 CreateTaskHandler createTask: null,
180 RunTaskHandler runTask: null,
181 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
119 CreateTimerHandler createTimer: null, 182 CreateTimerHandler createTimer: null,
183 // TODO(floitsch): mark as deprecated once tasks are non-experimental.
120 CreatePeriodicTimerHandler createPeriodicTimer: null, 184 CreatePeriodicTimerHandler createPeriodicTimer: null,
121 PrintHandler print: null, 185 PrintHandler print: null,
122 ForkHandler fork: null 186 ForkHandler fork: null
123 }) { 187 }) {
124 return new ZoneSpecification( 188 return new ZoneSpecification(
125 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError, 189 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError,
126 run: run ?? other.run, 190 run: run ?? other.run,
127 runUnary: runUnary ?? other.runUnary, 191 runUnary: runUnary ?? other.runUnary,
128 runBinary: runBinary ?? other.runBinary, 192 runBinary: runBinary ?? other.runBinary,
129 registerCallback: registerCallback ?? other.registerCallback, 193 registerCallback: registerCallback ?? other.registerCallback,
130 registerUnaryCallback: registerUnaryCallback ?? 194 registerUnaryCallback: registerUnaryCallback ??
131 other.registerUnaryCallback, 195 other.registerUnaryCallback,
132 registerBinaryCallback: registerBinaryCallback ?? 196 registerBinaryCallback: registerBinaryCallback ??
133 other.registerBinaryCallback, 197 other.registerBinaryCallback,
134 errorCallback: errorCallback ?? other.errorCallback, 198 errorCallback: errorCallback ?? other.errorCallback,
199
200 createTask: createTask ?? other.createTask,
201 runTask: runTask ?? other.runTask,
202 print : print ?? other.print,
203 fork: fork ?? other.fork,
135 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask, 204 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask,
136 createTimer : createTimer ?? other.createTimer, 205 createTimer : createTimer ?? other.createTimer,
137 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer, 206 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer);
138 print : print ?? other.print,
139 fork: fork ?? other.fork);
140 } 207 }
141 208
142 HandleUncaughtErrorHandler get handleUncaughtError; 209 HandleUncaughtErrorHandler get handleUncaughtError;
143 RunHandler get run; 210 RunHandler get run;
144 RunUnaryHandler get runUnary; 211 RunUnaryHandler get runUnary;
145 RunBinaryHandler get runBinary; 212 RunBinaryHandler get runBinary;
146 RegisterCallbackHandler get registerCallback; 213 RegisterCallbackHandler get registerCallback;
147 RegisterUnaryCallbackHandler get registerUnaryCallback; 214 RegisterUnaryCallbackHandler get registerUnaryCallback;
148 RegisterBinaryCallbackHandler get registerBinaryCallback; 215 RegisterBinaryCallbackHandler get registerBinaryCallback;
149 ErrorCallbackHandler get errorCallback; 216 ErrorCallbackHandler get errorCallback;
150 ScheduleMicrotaskHandler get scheduleMicrotask; 217 ScheduleMicrotaskHandler get scheduleMicrotask;
151 CreateTimerHandler get createTimer; 218 /// *Experimental*. Might disappear without warning.
152 CreatePeriodicTimerHandler get createPeriodicTimer; 219 CreateTaskHandler get createTask;
220 /// *Experimental*. Might disappear without warning.
221 RunTaskHandler get runTask;
153 PrintHandler get print; 222 PrintHandler get print;
154 ForkHandler get fork; 223 ForkHandler get fork;
224
225 // TODO(floitsch): deprecate once tasks are non-experimental.
226 CreateTimerHandler get createTimer;
227 // TODO(floitsch): deprecate once tasks are non-experimental.
228 CreatePeriodicTimerHandler get createPeriodicTimer;
155 } 229 }
156 230
157 /** 231 /**
158 * Internal [ZoneSpecification] class. 232 * Internal [ZoneSpecification] class.
159 * 233 *
160 * The implementation wants to rely on the fact that the getters cannot change 234 * The implementation wants to rely on the fact that the getters cannot change
161 * dynamically. We thus require users to go through the redirecting 235 * dynamically. We thus require users to go through the redirecting
162 * [ZoneSpecification] constructor which instantiates this class. 236 * [ZoneSpecification] constructor which instantiates this class.
163 */ 237 */
164 class _ZoneSpecification implements ZoneSpecification { 238 class _ZoneSpecification implements ZoneSpecification {
165 const _ZoneSpecification({ 239 const _ZoneSpecification({
166 this.handleUncaughtError: null, 240 this.handleUncaughtError: null,
167 this.run: null, 241 this.run: null,
168 this.runUnary: null, 242 this.runUnary: null,
169 this.runBinary: null, 243 this.runBinary: null,
170 this.registerCallback: null, 244 this.registerCallback: null,
171 this.registerUnaryCallback: null, 245 this.registerUnaryCallback: null,
172 this.registerBinaryCallback: null, 246 this.registerBinaryCallback: null,
173 this.errorCallback: null, 247 this.errorCallback: null,
174 this.scheduleMicrotask: null, 248 this.scheduleMicrotask: null,
249 this.createTask: null,
250 this.runTask: null,
251 this.print: null,
252 this.fork: null,
253 // TODO(floitsch): deprecate once tasks are non-experimental.
175 this.createTimer: null, 254 this.createTimer: null,
176 this.createPeriodicTimer: null, 255 // TODO(floitsch): deprecate once tasks are non-experimental.
177 this.print: null, 256 this.createPeriodicTimer: null
178 this.fork: null
179 }); 257 });
180 258
181 final HandleUncaughtErrorHandler handleUncaughtError; 259 final HandleUncaughtErrorHandler handleUncaughtError;
182 final RunHandler run; 260 final RunHandler run;
183 final RunUnaryHandler runUnary; 261 final RunUnaryHandler runUnary;
184 final RunBinaryHandler runBinary; 262 final RunBinaryHandler runBinary;
185 final RegisterCallbackHandler registerCallback; 263 final RegisterCallbackHandler registerCallback;
186 final RegisterUnaryCallbackHandler registerUnaryCallback; 264 final RegisterUnaryCallbackHandler registerUnaryCallback;
187 final RegisterBinaryCallbackHandler registerBinaryCallback; 265 final RegisterBinaryCallbackHandler registerBinaryCallback;
188 final ErrorCallbackHandler errorCallback; 266 final ErrorCallbackHandler errorCallback;
189 final ScheduleMicrotaskHandler scheduleMicrotask; 267 final ScheduleMicrotaskHandler scheduleMicrotask;
190 final CreateTimerHandler createTimer; 268 final CreateTaskHandler createTask;
191 final CreatePeriodicTimerHandler createPeriodicTimer; 269 final RunTaskHandler runTask;
192 final PrintHandler print; 270 final PrintHandler print;
193 final ForkHandler fork; 271 final ForkHandler fork;
272
273 // TODO(floitsch): deprecate once tasks are non-experimental.
274 final CreateTimerHandler createTimer;
275 // TODO(floitsch): deprecate once tasks are non-experimental.
276 final CreatePeriodicTimerHandler createPeriodicTimer;
194 } 277 }
195 278
196 /** 279 /**
197 * This class wraps zones for delegation. 280 * This class wraps zones for delegation.
198 * 281 *
199 * When forwarding to parent zones one can't just invoke the parent zone's 282 * When forwarding to parent zones one can't just invoke the parent zone's
200 * exposed functions (like [Zone.run]), but one needs to provide more 283 * exposed functions (like [Zone.run]), but one needs to provide more
201 * information (like the zone the `run` was initiated). Zone callbacks thus 284 * information (like the zone the `run` was initiated). Zone callbacks thus
202 * receive more information including this [ZoneDelegate] class. When delegating 285 * receive more information including this [ZoneDelegate] class. When delegating
203 * to the parent zone one should go through the given instance instead of 286 * to the parent zone one should go through the given instance instead of
204 * directly invoking the parent zone. 287 * directly invoking the parent zone.
205 */ 288 */
206 abstract class ZoneDelegate { 289 abstract class ZoneDelegate {
207 /*=R*/ handleUncaughtError/*<R>*/( 290 /*=R*/ handleUncaughtError/*<R>*/(
208 Zone zone, error, StackTrace stackTrace); 291 Zone zone, error, StackTrace stackTrace);
209 /*=R*/ run/*<R>*/(Zone zone, /*=R*/ f()); 292 /*=R*/ run/*<R>*/(Zone zone, /*=R*/ f());
210 /*=R*/ runUnary/*<R, T>*/(Zone zone, /*=R*/ f(/*=T*/ arg), /*=T*/ arg); 293 /*=R*/ runUnary/*<R, T>*/(Zone zone, /*=R*/ f(/*=T*/ arg), /*=T*/ arg);
211 /*=R*/ runBinary/*<R, T1, T2>*/(Zone zone, 294 /*=R*/ runBinary/*<R, T1, T2>*/(Zone zone,
212 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2), /*=T1*/ arg1, /*=T2*/ arg2); 295 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2), /*=T1*/ arg1, /*=T2*/ arg2);
213 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f()); 296 ZoneCallback/*<R>*/ registerCallback/*<R>*/(Zone zone, /*=R*/ f());
214 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 297 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
215 Zone zone, /*=R*/ f(/*=T*/ arg)); 298 Zone zone, /*=R*/ f(/*=T*/ arg));
216 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 299 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
217 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)); 300 Zone zone, /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2));
218 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace); 301 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace);
219 void scheduleMicrotask(Zone zone, void f()); 302 void scheduleMicrotask(Zone zone, void f());
220 Timer createTimer(Zone zone, Duration duration, void f()); 303
221 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)); 304 /// *Experimental*. Might disappear without notice.
305 Object/*=T*/ createTask/*<T, S>*/(
306 Zone zone, TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ task);
307 /// *Experimental*. Might disappear without notice.
308 void runTask/*T, A*/(
309 Zone zone, TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg);
310
222 void print(Zone zone, String line); 311 void print(Zone zone, String line);
223 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues); 312 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues);
313
314 // TODO(floitsch): deprecate once tasks are non-experimental.
315 Timer createTimer(Zone zone, Duration duration, void f());
316 // TODO(floitsch): deprecate once tasks are non-experimental.
317 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer));
224 } 318 }
225 319
226 /** 320 /**
227 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous 321 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous
228 * callbacks are executed in the zone they have been queued in. For example, 322 * callbacks are executed in the zone they have been queued in. For example,
229 * the callback of a `future.then` is executed in the same zone as the one where 323 * the callback of a `future.then` is executed in the same zone as the one where
230 * the `then` was invoked. 324 * the `then` was invoked.
231 */ 325 */
232 abstract class Zone { 326 abstract class Zone {
233 // Private constructor so that it is not possible instantiate a Zone class. 327 // Private constructor so that it is not possible instantiate a Zone class.
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 * If the [AsyncError.error] is `null`, it is replaced by a [NullThrownError]. 498 * If the [AsyncError.error] is `null`, it is replaced by a [NullThrownError].
405 */ 499 */
406 AsyncError errorCallback(Object error, StackTrace stackTrace); 500 AsyncError errorCallback(Object error, StackTrace stackTrace);
407 501
408 /** 502 /**
409 * Runs [f] asynchronously in this zone. 503 * Runs [f] asynchronously in this zone.
410 */ 504 */
411 void scheduleMicrotask(void f()); 505 void scheduleMicrotask(void f());
412 506
413 /** 507 /**
508 * Creates a task, given a [create] function and a [specification].
509 *
510 * The [create] function is invoked with the [specification] as argument. It
511 * returns a task object which is used for all future interactions with the
512 * zone.
513 *
514 * Custom zones may replace the [specification] with a different one, thus
515 * modifying the task parameters.
516 *
517 * Tasks are created when the program is starting an operation that returns
518 * through the event loop. For example, a timer or an http request both
Lasse Reichstein Nielsen 2016/07/01 11:39:11 Still not sure I like "operation that returns thro
floitsch 2016/07/02 00:42:42 Sorry for the confusion. I had a follow-up CL to i
519 * return through the event loop and are therefore tasks.
520 *
521 * If the [create] function is not invoked (because a custom zone has
522 * replaced or intercepted it), then the operation is *not* started. This
523 * means that a custom zone can intercept tasks, like http requests.
524 *
525 * *Experimental*. Might disappear without notice.
526 */
527 Object/*=T*/ createTask/*<T, S>*/(
528 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification);
529
530 /**
531 * Runs a task callback.
532 *
533 * This function is invoked, when an operation, started through [createTask],
Lasse Reichstein Nielsen 2016/07/01 11:39:11 remove comma before "when".
floitsch 2016/07/02 00:42:43 Was already fixed in the other CL. Again copied it
534 * returns to Dart code.
Lasse Reichstein Nielsen 2016/07/01 11:39:11 "returns to Dart code" -> "generates an event".
floitsch 2016/07/02 00:42:42 Done.
535 *
536 * Generally, tasks schedule Dart code in the global event loop. As such,
Lasse Reichstein Nielsen 2016/07/01 11:39:11 I think something is missing here, but I can't fin
floitsch 2016/07/02 00:42:42 Done.
537 * there is no return value, and [runTask] is a void function.
538 *
539 * The [task] object must be the same as the one created with [createTask].
540 *
541 * It is good practice, if task operations provide a meaningful [arg], so
Lasse Reichstein Nielsen 2016/07/01 11:39:11 , if -> that
floitsch 2016/07/02 00:42:43 Done.
542 * that custom zones can deal with it. They might want to log it, or
Lasse Reichstein Nielsen 2016/07/01 11:39:11 maybe: can deal with it -> can see what is happeni
Lasse Reichstein Nielsen 2016/07/01 11:39:11 it, or -> or
floitsch 2016/07/02 00:42:42 Done.
floitsch 2016/07/02 00:42:42 changed to 'interact'.
543 * replace it.
Lasse Reichstein Nielsen 2016/07/01 11:39:11 it -> the argument before calling the [run] functi
floitsch 2016/07/02 00:42:43 Done.
544 *
545 * *Experimental*. Might disappear without notice.
546 */
547 void runTask/*<T, A>*/(
548 TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg);
549
550 /**
414 * Creates a Timer where the callback is executed in this zone. 551 * Creates a Timer where the callback is executed in this zone.
415 */ 552 */
553 // TODO(floitsch): deprecate once tasks are non-experimental.
416 Timer createTimer(Duration duration, void callback()); 554 Timer createTimer(Duration duration, void callback());
417 555
418 /** 556 /**
419 * Creates a periodic Timer where the callback is executed in this zone. 557 * Creates a periodic Timer where the callback is executed in this zone.
420 */ 558 */
559 // TODO(floitsch): deprecate once tasks are non-experimental.
421 Timer createPeriodicTimer(Duration period, void callback(Timer timer)); 560 Timer createPeriodicTimer(Duration period, void callback(Timer timer));
422 561
423 /** 562 /**
424 * Prints the given [line]. 563 * Prints the given [line].
425 */ 564 */
426 void print(String line); 565 void print(String line);
427 566
428 /** 567 /**
429 * Call to enter the Zone. 568 * Call to enter the Zone.
430 * 569 *
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 error, stackTrace); 696 error, stackTrace);
558 } 697 }
559 698
560 void scheduleMicrotask(Zone zone, f()) { 699 void scheduleMicrotask(Zone zone, f()) {
561 var implementation = _delegationTarget._scheduleMicrotask; 700 var implementation = _delegationTarget._scheduleMicrotask;
562 _Zone implZone = implementation.zone; 701 _Zone implZone = implementation.zone;
563 ScheduleMicrotaskHandler handler = implementation.function; 702 ScheduleMicrotaskHandler handler = implementation.function;
564 handler(implZone, _parentDelegate(implZone), zone, f); 703 handler(implZone, _parentDelegate(implZone), zone, f);
565 } 704 }
566 705
567 Timer createTimer(Zone zone, Duration duration, void f()) { 706 Object createTask(
568 var implementation = _delegationTarget._createTimer; 707 Zone zone, TaskCreate create, TaskSpecification specification) {
708 _ZoneFunction implementation = _delegationTarget._createTask;
569 _Zone implZone = implementation.zone; 709 _Zone implZone = implementation.zone;
570 CreateTimerHandler handler = implementation.function; 710 CreateTaskHandler handler = implementation.function;
571 return handler(implZone, _parentDelegate(implZone), zone, duration, f); 711 return handler(
712 implZone, _parentDelegate(implZone), zone, create, specification);
572 } 713 }
573 714
574 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) { 715 void runTask(Zone zone, TaskRun run, Object task, Object arg) {
575 var implementation = _delegationTarget._createPeriodicTimer; 716 _ZoneFunction implementation = _delegationTarget._runTask;
576 _Zone implZone = implementation.zone; 717 _Zone implZone = implementation.zone;
577 CreatePeriodicTimerHandler handler = implementation.function; 718 RunTaskHandler handler = implementation.function;
578 return handler(implZone, _parentDelegate(implZone), zone, period, f); 719 handler(implZone, _parentDelegate(implZone), zone, run, task, arg);
579 } 720 }
580 721
581 void print(Zone zone, String line) { 722 void print(Zone zone, String line) {
582 var implementation = _delegationTarget._print; 723 var implementation = _delegationTarget._print;
583 _Zone implZone = implementation.zone; 724 _Zone implZone = implementation.zone;
584 PrintHandler handler = implementation.function; 725 PrintHandler handler = implementation.function;
585 handler(implZone, _parentDelegate(implZone), zone, line); 726 handler(implZone, _parentDelegate(implZone), zone, line);
586 } 727 }
587 728
588 Zone fork(Zone zone, ZoneSpecification specification, 729 Zone fork(Zone zone, ZoneSpecification specification,
589 Map zoneValues) { 730 Map zoneValues) {
590 var implementation = _delegationTarget._fork; 731 var implementation = _delegationTarget._fork;
591 _Zone implZone = implementation.zone; 732 _Zone implZone = implementation.zone;
592 ForkHandler handler = implementation.function; 733 ForkHandler handler = implementation.function;
593 return handler( 734 return handler(
594 implZone, _parentDelegate(implZone), zone, specification, zoneValues); 735 implZone, _parentDelegate(implZone), zone, specification, zoneValues);
595 } 736 }
737
738 // TODO(floitsch): deprecate once tasks are non-experimental.
739 Timer createTimer(Zone zone, Duration duration, void f()) {
740 var implementation = _delegationTarget._createTimer;
741 _Zone implZone = implementation.zone;
742 CreateTimerHandler handler = implementation.function;
743 return handler(implZone, _parentDelegate(implZone), zone, duration, f);
744 }
745
746 // TODO(floitsch): deprecate once tasks are non-experimental.
747 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) {
748 var implementation = _delegationTarget._createPeriodicTimer;
749 _Zone implZone = implementation.zone;
750 CreatePeriodicTimerHandler handler = implementation.function;
751 return handler(implZone, _parentDelegate(implZone), zone, period, f);
752 }
596 } 753 }
597 754
598 755
599 /** 756 /**
600 * Base class for Zone implementations. 757 * Base class for Zone implementations.
601 */ 758 */
602 abstract class _Zone implements Zone { 759 abstract class _Zone implements Zone {
603 const _Zone(); 760 const _Zone();
604 761
605 _ZoneFunction<RunHandler> get _run; 762 _ZoneFunction<RunHandler> get _run;
606 _ZoneFunction<RunUnaryHandler> get _runUnary; 763 _ZoneFunction<RunUnaryHandler> get _runUnary;
607 _ZoneFunction<RunBinaryHandler> get _runBinary; 764 _ZoneFunction<RunBinaryHandler> get _runBinary;
608 _ZoneFunction<RegisterCallbackHandler> get _registerCallback; 765 _ZoneFunction<RegisterCallbackHandler> get _registerCallback;
609 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback; 766 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback;
610 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback; 767 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback;
611 _ZoneFunction<ErrorCallbackHandler> get _errorCallback; 768 _ZoneFunction<ErrorCallbackHandler> get _errorCallback;
612 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask; 769 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask;
613 _ZoneFunction<CreateTimerHandler> get _createTimer; 770 _ZoneFunction<CreateTaskHandler> get _createTask;
614 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer; 771 _ZoneFunction<RunTaskHandler> get _runTask;
615 _ZoneFunction<PrintHandler> get _print; 772 _ZoneFunction<PrintHandler> get _print;
616 _ZoneFunction<ForkHandler> get _fork; 773 _ZoneFunction<ForkHandler> get _fork;
617 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError; 774 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError;
775
776 // TODO(floitsch): deprecate once tasks are non-experimental.
777 _ZoneFunction<CreateTimerHandler> get _createTimer;
778 // TODO(floitsch): deprecate once tasks are non-experimental.
779 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer;
780
618 _Zone get parent; 781 _Zone get parent;
619 ZoneDelegate get _delegate; 782 ZoneDelegate get _delegate;
620 Map get _map; 783 Map get _map;
621 784
622 bool inSameErrorZone(Zone otherZone) { 785 bool inSameErrorZone(Zone otherZone) {
623 return identical(this, otherZone) || 786 return identical(this, otherZone) ||
624 identical(errorZone, otherZone.errorZone); 787 identical(errorZone, otherZone.errorZone);
625 } 788 }
626 } 789 }
627 790
628 class _CustomZone extends _Zone { 791 class _CustomZone extends _Zone {
629 // The actual zone and implementation of each of these 792 // The actual zone and implementation of each of these
630 // inheritable zone functions. 793 // inheritable zone functions.
631 _ZoneFunction<RunHandler> _run; 794 _ZoneFunction<RunHandler> _run;
632 _ZoneFunction<RunUnaryHandler> _runUnary; 795 _ZoneFunction<RunUnaryHandler> _runUnary;
633 _ZoneFunction<RunBinaryHandler> _runBinary; 796 _ZoneFunction<RunBinaryHandler> _runBinary;
634 _ZoneFunction<RegisterCallbackHandler> _registerCallback; 797 _ZoneFunction<RegisterCallbackHandler> _registerCallback;
635 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback; 798 _ZoneFunction<RegisterUnaryCallbackHandler> _registerUnaryCallback;
636 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback; 799 _ZoneFunction<RegisterBinaryCallbackHandler> _registerBinaryCallback;
637 _ZoneFunction<ErrorCallbackHandler> _errorCallback; 800 _ZoneFunction<ErrorCallbackHandler> _errorCallback;
638 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask; 801 _ZoneFunction<ScheduleMicrotaskHandler> _scheduleMicrotask;
639 _ZoneFunction<CreateTimerHandler> _createTimer; 802 _ZoneFunction<CreateTaskHandler> _createTask;
640 _ZoneFunction<CreatePeriodicTimerHandler> _createPeriodicTimer; 803 _ZoneFunction<RunTaskHandler> _runTask;
641 _ZoneFunction<PrintHandler> _print; 804 _ZoneFunction<PrintHandler> _print;
642 _ZoneFunction<ForkHandler> _fork; 805 _ZoneFunction<ForkHandler> _fork;
643 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError; 806 _ZoneFunction<HandleUncaughtErrorHandler> _handleUncaughtError;
644 807
808 // TODO(floitsch): deprecate once tasks are non-experimental.
809 _ZoneFunction<CreateTimerHandler> _createTimer;
810 // TODO(floitsch): deprecate once tasks are non-experimental.
811 _ZoneFunction<CreatePeriodicTimerHandler> _createPeriodicTimer;
812
645 // A cached delegate to this zone. 813 // A cached delegate to this zone.
646 ZoneDelegate _delegateCache; 814 ZoneDelegate _delegateCache;
647 815
648 /// The parent zone. 816 /// The parent zone.
649 final _Zone parent; 817 final _Zone parent;
650 818
651 /// The zone's scoped value declaration map. 819 /// The zone's scoped value declaration map.
652 /// 820 ///
653 /// This is always a [HashMap]. 821 /// This is always a [HashMap].
654 final Map _map; 822 final Map _map;
(...skipping 30 matching lines...) Expand all
685 this, specification.registerBinaryCallback) 853 this, specification.registerBinaryCallback)
686 : parent._registerBinaryCallback; 854 : parent._registerBinaryCallback;
687 _errorCallback = (specification.errorCallback != null) 855 _errorCallback = (specification.errorCallback != null)
688 ? new _ZoneFunction<ErrorCallbackHandler>( 856 ? new _ZoneFunction<ErrorCallbackHandler>(
689 this, specification.errorCallback) 857 this, specification.errorCallback)
690 : parent._errorCallback; 858 : parent._errorCallback;
691 _scheduleMicrotask = (specification.scheduleMicrotask != null) 859 _scheduleMicrotask = (specification.scheduleMicrotask != null)
692 ? new _ZoneFunction<ScheduleMicrotaskHandler>( 860 ? new _ZoneFunction<ScheduleMicrotaskHandler>(
693 this, specification.scheduleMicrotask) 861 this, specification.scheduleMicrotask)
694 : parent._scheduleMicrotask; 862 : parent._scheduleMicrotask;
695 _createTimer = (specification.createTimer != null) 863 _createTask = (specification.createTask != null)
696 ? new _ZoneFunction<CreateTimerHandler>(this, specification.createTimer) 864 ? new _ZoneFunction<CreateTaskHandler>(
697 : parent._createTimer; 865 this, specification.createTask)
698 _createPeriodicTimer = (specification.createPeriodicTimer != null) 866 : parent._createTask;
699 ? new _ZoneFunction<CreatePeriodicTimerHandler>( 867 _runTask = (specification.runTask != null)
700 this, specification.createPeriodicTimer) 868 ? new _ZoneFunction<RunTaskHandler>(
701 : parent._createPeriodicTimer; 869 this, specification.runTask)
870 : parent._runTask;
702 _print = (specification.print != null) 871 _print = (specification.print != null)
703 ? new _ZoneFunction<PrintHandler>(this, specification.print) 872 ? new _ZoneFunction<PrintHandler>(this, specification.print)
704 : parent._print; 873 : parent._print;
705 _fork = (specification.fork != null) 874 _fork = (specification.fork != null)
706 ? new _ZoneFunction<ForkHandler>(this, specification.fork) 875 ? new _ZoneFunction<ForkHandler>(this, specification.fork)
707 : parent._fork; 876 : parent._fork;
708 _handleUncaughtError = (specification.handleUncaughtError != null) 877 _handleUncaughtError = (specification.handleUncaughtError != null)
709 ? new _ZoneFunction<HandleUncaughtErrorHandler>( 878 ? new _ZoneFunction<HandleUncaughtErrorHandler>(
710 this, specification.handleUncaughtError) 879 this, specification.handleUncaughtError)
711 : parent._handleUncaughtError; 880 : parent._handleUncaughtError;
881
882 // Deprecated fields, once tasks are non-experimental.
883 _createTimer = (specification.createTimer != null)
884 ? new _ZoneFunction<CreateTimerHandler>(
885 this, specification.createTimer)
886 : parent._createTimer;
887 _createPeriodicTimer = (specification.createPeriodicTimer != null)
888 ? new _ZoneFunction<CreatePeriodicTimerHandler>(
889 this, specification.createPeriodicTimer)
890 : parent._createPeriodicTimer;
712 } 891 }
713 892
714 /** 893 /**
715 * The closest error-handling zone. 894 * The closest error-handling zone.
716 * 895 *
717 * Returns `this` if `this` has an error-handler. Otherwise returns the 896 * Returns `this` if `this` has an error-handler. Otherwise returns the
718 * parent's error-zone. 897 * parent's error-zone.
719 */ 898 */
720 Zone get errorZone => _handleUncaughtError.zone; 899 Zone get errorZone => _handleUncaughtError.zone;
721 900
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 } 1031 }
853 1032
854 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) { 1033 ZoneCallback/*<R>*/ registerCallback/*<R>*/(/*=R*/ callback()) {
855 var implementation = this._registerCallback; 1034 var implementation = this._registerCallback;
856 assert(implementation != null); 1035 assert(implementation != null);
857 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1036 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
858 RegisterCallbackHandler handler = implementation.function; 1037 RegisterCallbackHandler handler = implementation.function;
859 // TODO(floitsch): make this a generic method call on '<R>' once it's 1038 // TODO(floitsch): make this a generic method call on '<R>' once it's
860 // supported. Remove the unnecessary cast. 1039 // supported. Remove the unnecessary cast.
861 return handler(implementation.zone, parentDelegate, this, callback) 1040 return handler(implementation.zone, parentDelegate, this, callback)
862 as Object/*=ZoneCallback<R>*/; 1041 as dynamic/*=ZoneCallback<R>*/;
863 } 1042 }
864 1043
865 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/( 1044 ZoneUnaryCallback/*<R, T>*/ registerUnaryCallback/*<R, T>*/(
866 /*=R*/ callback(/*=T*/ arg)) { 1045 /*=R*/ callback(/*=T*/ arg)) {
867 var implementation = this._registerUnaryCallback; 1046 var implementation = this._registerUnaryCallback;
868 assert(implementation != null); 1047 assert(implementation != null);
869 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1048 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
870 RegisterUnaryCallbackHandler handler = implementation.function; 1049 RegisterUnaryCallbackHandler handler = implementation.function;
871 // TODO(floitsch): make this a generic method call on '<R, T>' once it's 1050 // TODO(floitsch): make this a generic method call on '<R, T>' once it's
872 // supported. Remove the unnecessary cast. 1051 // supported. Remove the unnecessary cast.
873 return handler(implementation.zone, parentDelegate, this, callback) 1052 return handler(implementation.zone, parentDelegate, this, callback)
874 as Object/*=ZoneUnaryCallback<R, T>*/; 1053 as dynamic/*=ZoneUnaryCallback<R, T>*/;
875 } 1054 }
876 1055
877 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 1056 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
878 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) { 1057 /*=R*/ callback(/*=T1*/ arg1, /*=T2*/ arg2)) {
879 var implementation = this._registerBinaryCallback; 1058 var implementation = this._registerBinaryCallback;
880 assert(implementation != null); 1059 assert(implementation != null);
881 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1060 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
882 RegisterBinaryCallbackHandler handler = implementation.function; 1061 RegisterBinaryCallbackHandler handler = implementation.function;
883 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once 1062 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once
884 // it's supported. Remove the unnecessary cast. 1063 // it's supported. Remove the unnecessary cast.
885 return handler(implementation.zone, parentDelegate, this, callback) 1064 return handler(implementation.zone, parentDelegate, this, callback)
886 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; 1065 as dynamic/*=ZoneBinaryCallback<R, T1, T2>*/;
887 } 1066 }
888 1067
889 AsyncError errorCallback(Object error, StackTrace stackTrace) { 1068 AsyncError errorCallback(Object error, StackTrace stackTrace) {
890 var implementation = this._errorCallback; 1069 var implementation = this._errorCallback;
891 assert(implementation != null); 1070 assert(implementation != null);
892 final Zone implementationZone = implementation.zone; 1071 final Zone implementationZone = implementation.zone;
893 if (identical(implementationZone, _ROOT_ZONE)) return null; 1072 if (identical(implementationZone, _ROOT_ZONE)) return null;
894 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone); 1073 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone);
895 ErrorCallbackHandler handler = implementation.function; 1074 ErrorCallbackHandler handler = implementation.function;
896 return handler( 1075 return handler(
897 implementationZone, parentDelegate, this, error, stackTrace); 1076 implementationZone, parentDelegate, this, error, stackTrace);
898 } 1077 }
899 1078
900 void scheduleMicrotask(void f()) { 1079 void scheduleMicrotask(void f()) {
901 var implementation = this._scheduleMicrotask; 1080 var implementation = this._scheduleMicrotask;
902 assert(implementation != null); 1081 assert(implementation != null);
903 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1082 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
904 ScheduleMicrotaskHandler handler = implementation.function; 1083 ScheduleMicrotaskHandler handler = implementation.function;
905 return handler(implementation.zone, parentDelegate, this, f); 1084 handler(implementation.zone, parentDelegate, this, f);
906 } 1085 }
907 1086
1087 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
1088 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1089 var implementation = this._createTask;
1090 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1091 CreateTaskHandler handler = implementation.function;
1092 return handler(
1093 implementation.zone, parentDelegate, this, create, specification);
1094 }
1095
1096 void runTask/*<T, A>*/(
1097 TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg1) {
1098 var implementation = this._runTask;
1099 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1100 RunTaskHandler handler = implementation.function;
1101 handler(implementation.zone, parentDelegate, this, run, task, arg1);
1102 }
1103
1104 void print(String line) {
1105 var implementation = this._print;
1106 assert(implementation != null);
1107 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
1108 PrintHandler handler = implementation.function;
1109 return handler(implementation.zone, parentDelegate, this, line);
1110 }
1111
1112 // TODO(floitsch): deprecate once tasks are non-experimental.
908 Timer createTimer(Duration duration, void f()) { 1113 Timer createTimer(Duration duration, void f()) {
909 var implementation = this._createTimer; 1114 var implementation = this._createTimer;
910 assert(implementation != null); 1115 assert(implementation != null);
911 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1116 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
912 CreateTimerHandler handler = implementation.function; 1117 CreateTimerHandler handler = implementation.function;
913 return handler(implementation.zone, parentDelegate, this, duration, f); 1118 return handler(implementation.zone, parentDelegate, this, duration, f);
914 } 1119 }
915 1120
1121 // TODO(floitsch): deprecate once tasks are non-experimental.
916 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 1122 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
917 var implementation = this._createPeriodicTimer; 1123 var implementation = this._createPeriodicTimer;
918 assert(implementation != null); 1124 assert(implementation != null);
919 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); 1125 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
920 CreatePeriodicTimerHandler handler = implementation.function; 1126 CreatePeriodicTimerHandler handler = implementation.function;
921 return handler( 1127 return handler(
922 implementation.zone, parentDelegate, this, duration, f); 1128 implementation.zone, parentDelegate, this, duration, f);
923 } 1129 }
924
925 void print(String line) {
926 var implementation = this._print;
927 assert(implementation != null);
928 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
929 PrintHandler handler = implementation.function;
930 return handler(implementation.zone, parentDelegate, this, line);
931 }
932 } 1130 }
933 1131
934 /*=R*/ _rootHandleUncaughtError/*<R>*/( 1132 /*=R*/ _rootHandleUncaughtError/*<R>*/(
935 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) { 1133 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) {
936 _schedulePriorityAsyncCallback(() { 1134 _schedulePriorityAsyncCallback(() {
937 if (error == null) error = new NullThrownError(); 1135 if (error == null) error = new NullThrownError();
938 if (stackTrace == null) throw error; 1136 if (stackTrace == null) throw error;
939 _rethrow(error, stackTrace); 1137 _rethrow(error, stackTrace);
940 }); 1138 });
941 } 1139 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) { 1197 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) {
1000 if (!identical(_ROOT_ZONE, zone)) { 1198 if (!identical(_ROOT_ZONE, zone)) {
1001 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone); 1199 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone);
1002 f = zone.bindCallback(f, runGuarded: hasErrorHandler); 1200 f = zone.bindCallback(f, runGuarded: hasErrorHandler);
1003 // Use root zone as event zone if the function is already bound. 1201 // Use root zone as event zone if the function is already bound.
1004 zone = _ROOT_ZONE; 1202 zone = _ROOT_ZONE;
1005 } 1203 }
1006 _scheduleAsyncCallback(f); 1204 _scheduleAsyncCallback(f);
1007 } 1205 }
1008 1206
1207 Object/*=T*/ _rootCreateTask/*<T, S extends TaskSpecification>*/(
1208 Zone self, ZoneDelegate parent, Zone zone,
1209 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1210 return create(specification, zone);
1211 }
1212
1213 void _rootRunTask/*<T, A>*/(
1214 Zone self, ZoneDelegate parent, Zone zone, TaskRun run/*<T, A>*/,
1215 Object/*=T*/ task, Object/*=A*/ arg) {
1216 if (Zone._current == zone) {
1217 run(task, arg);
1218 return;
1219 }
1220
1221 Zone old = Zone._enter(zone);
1222 try {
1223 run(task, arg);
1224 } catch (e, s) {
1225 return zone.handleUncaughtError/*<dynamic>*/(e, s);
1226 } finally {
1227 Zone._leave(old);
1228 }
1229 }
1230
1009 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone, 1231 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone,
1010 Duration duration, void callback()) { 1232 Duration duration, void callback()) {
1011 if (!identical(_ROOT_ZONE, zone)) { 1233 return new Timer._task(zone, duration, callback);
1012 callback = zone.bindCallback(callback);
1013 }
1014 return Timer._createTimer(duration, callback);
1015 } 1234 }
1016 1235
1017 Timer _rootCreatePeriodicTimer( 1236 Timer _rootCreatePeriodicTimer(
1018 Zone self, ZoneDelegate parent, Zone zone, 1237 Zone self, ZoneDelegate parent, Zone zone,
1019 Duration duration, void callback(Timer timer)) { 1238 Duration duration, void callback(Timer timer)) {
1020 if (!identical(_ROOT_ZONE, zone)) { 1239 return new Timer._periodicTask(zone, duration, callback);
1021 // TODO(floitsch): the return type should be 'void'.
1022 callback = zone.bindUnaryCallback/*<dynamic, Timer>*/(callback);
1023 }
1024 return Timer._createPeriodicTimer(duration, callback);
1025 } 1240 }
1026 1241
1027 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) { 1242 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) {
1028 printToConsole(line); 1243 printToConsole(line);
1029 } 1244 }
1030 1245
1031 void _printToZone(String line) { 1246 void _printToZone(String line) {
1032 Zone.current.print(line); 1247 Zone.current.print(line);
1033 } 1248 }
1034 1249
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 const _ZoneFunction<RegisterUnaryCallbackHandler>( 1290 const _ZoneFunction<RegisterUnaryCallbackHandler>(
1076 _ROOT_ZONE, _rootRegisterUnaryCallback); 1291 _ROOT_ZONE, _rootRegisterUnaryCallback);
1077 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback => 1292 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback =>
1078 const _ZoneFunction<RegisterBinaryCallbackHandler>( 1293 const _ZoneFunction<RegisterBinaryCallbackHandler>(
1079 _ROOT_ZONE, _rootRegisterBinaryCallback); 1294 _ROOT_ZONE, _rootRegisterBinaryCallback);
1080 _ZoneFunction<ErrorCallbackHandler> get _errorCallback => 1295 _ZoneFunction<ErrorCallbackHandler> get _errorCallback =>
1081 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback); 1296 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback);
1082 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask => 1297 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask =>
1083 const _ZoneFunction<ScheduleMicrotaskHandler>( 1298 const _ZoneFunction<ScheduleMicrotaskHandler>(
1084 _ROOT_ZONE, _rootScheduleMicrotask); 1299 _ROOT_ZONE, _rootScheduleMicrotask);
1085 _ZoneFunction<CreateTimerHandler> get _createTimer => 1300 _ZoneFunction<CreateTaskHandler> get _createTask =>
1086 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer); 1301 const _ZoneFunction<CreateTaskHandler>(_ROOT_ZONE, _rootCreateTask);
1087 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer => 1302 _ZoneFunction<RunTaskHandler> get _runTask =>
1088 const _ZoneFunction<CreatePeriodicTimerHandler>(_ROOT_ZONE, _rootCreatePer iodicTimer); 1303 const _ZoneFunction<RunTaskHandler>(_ROOT_ZONE, _rootRunTask);
1089 _ZoneFunction<PrintHandler> get _print => 1304 _ZoneFunction<PrintHandler> get _print =>
1090 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint); 1305 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint);
1091 _ZoneFunction<ForkHandler> get _fork => 1306 _ZoneFunction<ForkHandler> get _fork =>
1092 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork); 1307 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork);
1093 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError => 1308 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError =>
1094 const _ZoneFunction<HandleUncaughtErrorHandler>( 1309 const _ZoneFunction<HandleUncaughtErrorHandler>(
1095 _ROOT_ZONE, _rootHandleUncaughtError); 1310 _ROOT_ZONE, _rootHandleUncaughtError);
1096 1311
1312 // TODO(floitsch): deprecate once tasks are non-experimental.
1313 _ZoneFunction get _createTimer =>
1314 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer);
1315 // TODO(floitsch): deprecate once tasks are non-experimental.
1316 _ZoneFunction get _createPeriodicTimer =>
1317 const _ZoneFunction<CreatePeriodicTimerHandler>(
1318 _ROOT_ZONE, _rootCreatePeriodicTimer);
1319
1097 // The parent zone. 1320 // The parent zone.
1098 _Zone get parent => null; 1321 _Zone get parent => null;
1099 1322
1100 /// The zone's scoped value declaration map. 1323 /// The zone's scoped value declaration map.
1101 /// 1324 ///
1102 /// This is always a [HashMap]. 1325 /// This is always a [HashMap].
1103 Map get _map => _rootMap; 1326 Map get _map => _rootMap;
1104 1327
1105 static Map _rootMap = new HashMap(); 1328 static Map _rootMap = new HashMap();
1106 1329
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 1441
1219 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/( 1442 ZoneBinaryCallback/*<R, T1, T2>*/ registerBinaryCallback/*<R, T1, T2>*/(
1220 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f; 1443 /*=R*/ f(/*=T1*/ arg1, /*=T2*/ arg2)) => f;
1221 1444
1222 AsyncError errorCallback(Object error, StackTrace stackTrace) => null; 1445 AsyncError errorCallback(Object error, StackTrace stackTrace) => null;
1223 1446
1224 void scheduleMicrotask(void f()) { 1447 void scheduleMicrotask(void f()) {
1225 _rootScheduleMicrotask(null, null, this, f); 1448 _rootScheduleMicrotask(null, null, this, f);
1226 } 1449 }
1227 1450
1451 Object/*=T*/ createTask/*<T, S extends TaskSpecification>*/(
1452 TaskCreate/*<T, S>*/ create, TaskSpecification/*=S*/ specification) {
1453 return _rootCreateTask/*<T, S>*/(null, null, this, create, specification);
1454 }
1455
1456 void runTask/*<T, A>*/(
1457 TaskRun/*<T, A>*/ run, Object/*=T*/ task, Object/*=A*/ arg) {
1458 _rootRunTask/*<T, A>*/(null, null, this, run, task, arg);
1459 }
1460
1228 Timer createTimer(Duration duration, void f()) { 1461 Timer createTimer(Duration duration, void f()) {
1229 return Timer._createTimer(duration, f); 1462 return Timer._createTimer(duration, f);
1230 } 1463 }
1231 1464
1232 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { 1465 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
1233 return Timer._createPeriodicTimer(duration, f); 1466 return Timer._createPeriodicTimer(duration, f);
1234 } 1467 }
1235 1468
1236 void print(String line) { 1469 void print(String line) {
1237 printToConsole(line); 1470 printToConsole(line);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 handleUncaughtError: errorHandler); 1529 handleUncaughtError: errorHandler);
1297 } 1530 }
1298 Zone zone = Zone.current.fork(specification: zoneSpecification, 1531 Zone zone = Zone.current.fork(specification: zoneSpecification,
1299 zoneValues: zoneValues); 1532 zoneValues: zoneValues);
1300 if (onError != null) { 1533 if (onError != null) {
1301 return zone.runGuarded(body); 1534 return zone.runGuarded(body);
1302 } else { 1535 } else {
1303 return zone.run(body); 1536 return zone.run(body);
1304 } 1537 }
1305 } 1538 }
OLDNEW
« no previous file with comments | « sdk/lib/async/timer.dart ('k') | tests/lib/async/zone_task_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698