OLD | NEW |
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 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic | 11 // TODO(floitsch): we are abusing generic typedefs as typedefs for generic |
12 // functions. | 12 // functions. |
13 /*ABUSE*/ | 13 /*ABUSE*/ |
14 typedef R HandleUncaughtErrorHandler<R>( | 14 typedef R HandleUncaughtErrorHandler<R>( |
15 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace); | 15 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace); |
16 /*ABUSE*/ | 16 /*ABUSE*/ |
17 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()); |
18 /*ABUSE*/ | 18 /*ABUSE*/ |
19 typedef R RunUnaryHandler<R, T>( | 19 typedef R RunUnaryHandler<R, T>( |
20 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); |
21 /*ABUSE*/ | 21 /*ABUSE*/ |
22 typedef R RunBinaryHandler<R, T1, T2>( | 22 typedef R RunBinaryHandler<R, T1, T2>(Zone self, ZoneDelegate parent, Zone zone, |
23 Zone self, ZoneDelegate parent, Zone zone, | |
24 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); | 23 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); |
25 /*ABUSE*/ | 24 /*ABUSE*/ |
26 typedef ZoneCallback<R> RegisterCallbackHandler<R>( | 25 typedef ZoneCallback<R> RegisterCallbackHandler<R>( |
27 Zone self, ZoneDelegate parent, Zone zone, R f()); | 26 Zone self, ZoneDelegate parent, Zone zone, R f()); |
28 /*ABUSE*/ | 27 /*ABUSE*/ |
29 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>( | 28 typedef ZoneUnaryCallback<R, T> RegisterUnaryCallbackHandler<R, T>( |
30 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)); | 29 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)); |
31 /*ABUSE*/ | 30 /*ABUSE*/ |
32 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>( | 31 typedef ZoneBinaryCallback<R, T1, T2> RegisterBinaryCallbackHandler<R, T1, T2>( |
33 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)); | 32 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)); |
34 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent, | 33 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent, |
35 Zone zone, Object error, StackTrace stackTrace); | 34 Zone zone, Object error, StackTrace stackTrace); |
36 typedef void ScheduleMicrotaskHandler( | 35 typedef void ScheduleMicrotaskHandler( |
37 Zone self, ZoneDelegate parent, Zone zone, void f()); | 36 Zone self, ZoneDelegate parent, Zone zone, void f()); |
38 typedef Timer CreateTimerHandler( | 37 typedef Timer CreateTimerHandler( |
39 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()); | 38 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()); |
40 typedef Timer CreatePeriodicTimerHandler( | 39 typedef Timer CreatePeriodicTimerHandler(Zone self, ZoneDelegate parent, |
41 Zone self, ZoneDelegate parent, Zone zone, | 40 Zone zone, Duration period, void f(Timer timer)); |
42 Duration period, void f(Timer timer)); | |
43 typedef void PrintHandler( | 41 typedef void PrintHandler( |
44 Zone self, ZoneDelegate parent, Zone zone, String line); | 42 Zone self, ZoneDelegate parent, Zone zone, String line); |
45 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone, | 43 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone, |
46 ZoneSpecification specification, | 44 ZoneSpecification specification, Map zoneValues); |
47 Map zoneValues); | |
48 | 45 |
49 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */ | 46 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */ |
50 class AsyncError implements Error { | 47 class AsyncError implements Error { |
51 final Object error; | 48 final Object error; |
52 final StackTrace stackTrace; | 49 final StackTrace stackTrace; |
53 | 50 |
54 AsyncError(this.error, this.stackTrace); | 51 AsyncError(this.error, this.stackTrace); |
55 | 52 |
56 String toString() => '$error'; | 53 String toString() => '$error'; |
57 } | 54 } |
58 | 55 |
59 | |
60 class _ZoneFunction<T extends Function> { | 56 class _ZoneFunction<T extends Function> { |
61 final _Zone zone; | 57 final _Zone zone; |
62 final T function; | 58 final T function; |
63 const _ZoneFunction(this.zone, this.function); | 59 const _ZoneFunction(this.zone, this.function); |
64 } | 60 } |
65 | 61 |
66 /** | 62 /** |
67 * This class provides the specification for a forked zone. | 63 * This class provides the specification for a forked zone. |
68 * | 64 * |
69 * When forking a new zone (see [Zone.fork]) one can override the default | 65 * 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 | 66 * behavior of the zone by providing callbacks. These callbacks must be |
71 * given in an instance of this class. | 67 * given in an instance of this class. |
72 * | 68 * |
73 * Handlers have the same signature as the same-named methods on [Zone] but | 69 * Handlers have the same signature as the same-named methods on [Zone] but |
74 * receive three additional arguments: | 70 * receive three additional arguments: |
75 * | 71 * |
76 * 1. the zone the handlers are attached to (the "self" zone). | 72 * 1. the zone the handlers are attached to (the "self" zone). |
77 * 2. a [ZoneDelegate] to the parent zone. | 73 * 2. a [ZoneDelegate] to the parent zone. |
78 * 3. the zone that first received the request (before the request was | 74 * 3. the zone that first received the request (before the request was |
79 * bubbled up). | 75 * bubbled up). |
80 * | 76 * |
81 * Handlers can either stop propagation the request (by simply not calling the | 77 * Handlers can either stop propagation the request (by simply not calling the |
82 * parent handler), or forward to the parent zone, potentially modifying the | 78 * parent handler), or forward to the parent zone, potentially modifying the |
83 * arguments on the way. | 79 * arguments on the way. |
84 */ | 80 */ |
85 abstract class ZoneSpecification { | 81 abstract class ZoneSpecification { |
86 /** | 82 /** |
87 * Creates a specification with the provided handlers. | 83 * Creates a specification with the provided handlers. |
88 */ | 84 */ |
89 const factory ZoneSpecification({ | 85 const factory ZoneSpecification( |
90 HandleUncaughtErrorHandler handleUncaughtError, | 86 {HandleUncaughtErrorHandler handleUncaughtError, |
91 RunHandler run, | 87 RunHandler run, |
92 RunUnaryHandler runUnary, | 88 RunUnaryHandler runUnary, |
93 RunBinaryHandler runBinary, | 89 RunBinaryHandler runBinary, |
94 RegisterCallbackHandler registerCallback, | 90 RegisterCallbackHandler registerCallback, |
95 RegisterUnaryCallbackHandler registerUnaryCallback, | 91 RegisterUnaryCallbackHandler registerUnaryCallback, |
96 RegisterBinaryCallbackHandler registerBinaryCallback, | 92 RegisterBinaryCallbackHandler registerBinaryCallback, |
97 ErrorCallbackHandler errorCallback, | 93 ErrorCallbackHandler errorCallback, |
98 ScheduleMicrotaskHandler scheduleMicrotask, | 94 ScheduleMicrotaskHandler scheduleMicrotask, |
99 CreateTimerHandler createTimer, | 95 CreateTimerHandler createTimer, |
100 CreatePeriodicTimerHandler createPeriodicTimer, | 96 CreatePeriodicTimerHandler createPeriodicTimer, |
101 PrintHandler print, | 97 PrintHandler print, |
102 ForkHandler fork | 98 ForkHandler fork}) = _ZoneSpecification; |
103 }) = _ZoneSpecification; | |
104 | 99 |
105 /** | 100 /** |
106 * Creates a specification from [other] with the provided handlers overriding | 101 * Creates a specification from [other] with the provided handlers overriding |
107 * the ones in [other]. | 102 * the ones in [other]. |
108 */ | 103 */ |
109 factory ZoneSpecification.from(ZoneSpecification other, { | 104 factory ZoneSpecification.from(ZoneSpecification other, |
110 HandleUncaughtErrorHandler handleUncaughtError: null, | 105 {HandleUncaughtErrorHandler handleUncaughtError: null, |
111 RunHandler run: null, | 106 RunHandler run: null, |
112 RunUnaryHandler runUnary: null, | 107 RunUnaryHandler runUnary: null, |
113 RunBinaryHandler runBinary: null, | 108 RunBinaryHandler runBinary: null, |
114 RegisterCallbackHandler registerCallback: null, | 109 RegisterCallbackHandler registerCallback: null, |
115 RegisterUnaryCallbackHandler registerUnaryCallback: null, | 110 RegisterUnaryCallbackHandler registerUnaryCallback: null, |
116 RegisterBinaryCallbackHandler registerBinaryCallback: null, | 111 RegisterBinaryCallbackHandler registerBinaryCallback: null, |
117 ErrorCallbackHandler errorCallback: null, | 112 ErrorCallbackHandler errorCallback: null, |
118 ScheduleMicrotaskHandler scheduleMicrotask: null, | 113 ScheduleMicrotaskHandler scheduleMicrotask: null, |
119 CreateTimerHandler createTimer: null, | 114 CreateTimerHandler createTimer: null, |
120 CreatePeriodicTimerHandler createPeriodicTimer: null, | 115 CreatePeriodicTimerHandler createPeriodicTimer: null, |
121 PrintHandler print: null, | 116 PrintHandler print: null, |
122 ForkHandler fork: null | 117 ForkHandler fork: null}) { |
123 }) { | |
124 return new ZoneSpecification( | 118 return new ZoneSpecification( |
125 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError, | 119 handleUncaughtError: handleUncaughtError ?? other.handleUncaughtError, |
126 run: run ?? other.run, | 120 run: run ?? other.run, |
127 runUnary: runUnary ?? other.runUnary, | 121 runUnary: runUnary ?? other.runUnary, |
128 runBinary: runBinary ?? other.runBinary, | 122 runBinary: runBinary ?? other.runBinary, |
129 registerCallback: registerCallback ?? other.registerCallback, | 123 registerCallback: registerCallback ?? other.registerCallback, |
130 registerUnaryCallback: registerUnaryCallback ?? | 124 registerUnaryCallback: |
131 other.registerUnaryCallback, | 125 registerUnaryCallback ?? other.registerUnaryCallback, |
132 registerBinaryCallback: registerBinaryCallback ?? | 126 registerBinaryCallback: |
133 other.registerBinaryCallback, | 127 registerBinaryCallback ?? other.registerBinaryCallback, |
134 errorCallback: errorCallback ?? other.errorCallback, | 128 errorCallback: errorCallback ?? other.errorCallback, |
135 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask, | 129 scheduleMicrotask: scheduleMicrotask ?? other.scheduleMicrotask, |
136 createTimer : createTimer ?? other.createTimer, | 130 createTimer: createTimer ?? other.createTimer, |
137 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer, | 131 createPeriodicTimer: createPeriodicTimer ?? other.createPeriodicTimer, |
138 print : print ?? other.print, | 132 print: print ?? other.print, |
139 fork: fork ?? other.fork); | 133 fork: fork ?? other.fork); |
140 } | 134 } |
141 | 135 |
142 HandleUncaughtErrorHandler get handleUncaughtError; | 136 HandleUncaughtErrorHandler get handleUncaughtError; |
143 RunHandler get run; | 137 RunHandler get run; |
144 RunUnaryHandler get runUnary; | 138 RunUnaryHandler get runUnary; |
145 RunBinaryHandler get runBinary; | 139 RunBinaryHandler get runBinary; |
146 RegisterCallbackHandler get registerCallback; | 140 RegisterCallbackHandler get registerCallback; |
147 RegisterUnaryCallbackHandler get registerUnaryCallback; | 141 RegisterUnaryCallbackHandler get registerUnaryCallback; |
148 RegisterBinaryCallbackHandler get registerBinaryCallback; | 142 RegisterBinaryCallbackHandler get registerBinaryCallback; |
149 ErrorCallbackHandler get errorCallback; | 143 ErrorCallbackHandler get errorCallback; |
150 ScheduleMicrotaskHandler get scheduleMicrotask; | 144 ScheduleMicrotaskHandler get scheduleMicrotask; |
151 CreateTimerHandler get createTimer; | 145 CreateTimerHandler get createTimer; |
152 CreatePeriodicTimerHandler get createPeriodicTimer; | 146 CreatePeriodicTimerHandler get createPeriodicTimer; |
153 PrintHandler get print; | 147 PrintHandler get print; |
154 ForkHandler get fork; | 148 ForkHandler get fork; |
155 } | 149 } |
156 | 150 |
157 /** | 151 /** |
158 * Internal [ZoneSpecification] class. | 152 * Internal [ZoneSpecification] class. |
159 * | 153 * |
160 * The implementation wants to rely on the fact that the getters cannot change | 154 * The implementation wants to rely on the fact that the getters cannot change |
161 * dynamically. We thus require users to go through the redirecting | 155 * dynamically. We thus require users to go through the redirecting |
162 * [ZoneSpecification] constructor which instantiates this class. | 156 * [ZoneSpecification] constructor which instantiates this class. |
163 */ | 157 */ |
164 class _ZoneSpecification implements ZoneSpecification { | 158 class _ZoneSpecification implements ZoneSpecification { |
165 const _ZoneSpecification({ | 159 const _ZoneSpecification( |
166 this.handleUncaughtError: null, | 160 {this.handleUncaughtError: null, |
167 this.run: null, | 161 this.run: null, |
168 this.runUnary: null, | 162 this.runUnary: null, |
169 this.runBinary: null, | 163 this.runBinary: null, |
170 this.registerCallback: null, | 164 this.registerCallback: null, |
171 this.registerUnaryCallback: null, | 165 this.registerUnaryCallback: null, |
172 this.registerBinaryCallback: null, | 166 this.registerBinaryCallback: null, |
173 this.errorCallback: null, | 167 this.errorCallback: null, |
174 this.scheduleMicrotask: null, | 168 this.scheduleMicrotask: null, |
175 this.createTimer: null, | 169 this.createTimer: null, |
176 this.createPeriodicTimer: null, | 170 this.createPeriodicTimer: null, |
177 this.print: null, | 171 this.print: null, |
178 this.fork: null | 172 this.fork: null}); |
179 }); | |
180 | 173 |
181 final HandleUncaughtErrorHandler handleUncaughtError; | 174 final HandleUncaughtErrorHandler handleUncaughtError; |
182 final RunHandler run; | 175 final RunHandler run; |
183 final RunUnaryHandler runUnary; | 176 final RunUnaryHandler runUnary; |
184 final RunBinaryHandler runBinary; | 177 final RunBinaryHandler runBinary; |
185 final RegisterCallbackHandler registerCallback; | 178 final RegisterCallbackHandler registerCallback; |
186 final RegisterUnaryCallbackHandler registerUnaryCallback; | 179 final RegisterUnaryCallbackHandler registerUnaryCallback; |
187 final RegisterBinaryCallbackHandler registerBinaryCallback; | 180 final RegisterBinaryCallbackHandler registerBinaryCallback; |
188 final ErrorCallbackHandler errorCallback; | 181 final ErrorCallbackHandler errorCallback; |
189 final ScheduleMicrotaskHandler scheduleMicrotask; | 182 final ScheduleMicrotaskHandler scheduleMicrotask; |
(...skipping 19 matching lines...) Expand all Loading... |
209 * | 202 * |
210 * While zones have access to their parent zone (through [Zone.parent]) it is | 203 * While zones have access to their parent zone (through [Zone.parent]) it is |
211 * recommended to call the methods on the provided parent delegate for two | 204 * recommended to call the methods on the provided parent delegate for two |
212 * reasons: | 205 * reasons: |
213 * 1. the delegate methods take an additional `zone` argument which is the | 206 * 1. the delegate methods take an additional `zone` argument which is the |
214 * zone the action has been initiated in. | 207 * zone the action has been initiated in. |
215 * 2. delegate calls are more efficient, since the implementation knows how | 208 * 2. delegate calls are more efficient, since the implementation knows how |
216 * to skip zones that would just delegate to their parents. | 209 * to skip zones that would just delegate to their parents. |
217 */ | 210 */ |
218 abstract class ZoneDelegate { | 211 abstract class ZoneDelegate { |
219 R handleUncaughtError<R>( | 212 R handleUncaughtError<R>(Zone zone, error, StackTrace stackTrace); |
220 Zone zone, error, StackTrace stackTrace); | |
221 R run<R>(Zone zone, R f()); | 213 R run<R>(Zone zone, R f()); |
222 R runUnary<R, T>(Zone zone, R f(T arg), T arg); | 214 R runUnary<R, T>(Zone zone, R f(T arg), T arg); |
223 R runBinary<R, T1, T2>(Zone zone, | 215 R runBinary<R, T1, T2>(Zone zone, R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); |
224 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2); | |
225 ZoneCallback<R> registerCallback<R>(Zone zone, R f()); | 216 ZoneCallback<R> registerCallback<R>(Zone zone, R f()); |
226 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>( | 217 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(Zone zone, R f(T arg)); |
227 Zone zone, R f(T arg)); | |
228 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( | 218 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( |
229 Zone zone, R f(T1 arg1, T2 arg2)); | 219 Zone zone, R f(T1 arg1, T2 arg2)); |
230 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace); | 220 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace); |
231 void scheduleMicrotask(Zone zone, void f()); | 221 void scheduleMicrotask(Zone zone, void f()); |
232 Timer createTimer(Zone zone, Duration duration, void f()); | 222 Timer createTimer(Zone zone, Duration duration, void f()); |
233 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)); | 223 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)); |
234 void print(Zone zone, String line); | 224 void print(Zone zone, String line); |
235 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues); | 225 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues); |
236 } | 226 } |
237 | 227 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 * inherit the behavior from the parent zone (`this`). | 390 * inherit the behavior from the parent zone (`this`). |
401 * | 391 * |
402 * The new zone inherits the stored values (accessed through [[]]) | 392 * The new zone inherits the stored values (accessed through [[]]) |
403 * of this zone and updates them with values from [zoneValues], which either | 393 * of this zone and updates them with values from [zoneValues], which either |
404 * adds new values or overrides existing ones. | 394 * adds new values or overrides existing ones. |
405 * | 395 * |
406 * Note that the fork operation is interceptible. A zone can thus change | 396 * Note that the fork operation is interceptible. A zone can thus change |
407 * the zone specification (or zone values), giving the forking zone full | 397 * the zone specification (or zone values), giving the forking zone full |
408 * control over the child zone. | 398 * control over the child zone. |
409 */ | 399 */ |
410 Zone fork({ZoneSpecification specification, | 400 Zone fork({ZoneSpecification specification, Map zoneValues}); |
411 Map zoneValues}); | |
412 | 401 |
413 /** | 402 /** |
414 * Executes [action] in this zone. | 403 * Executes [action] in this zone. |
415 * | 404 * |
416 * By default (as implemented in the [ROOT] zone), runs [action] | 405 * By default (as implemented in the [ROOT] zone), runs [action] |
417 * with [current] set to this zone. | 406 * with [current] set to this zone. |
418 * | 407 * |
419 * If [action] throws, the synchronous exception is not caught by the zone's | 408 * If [action] throws, the synchronous exception is not caught by the zone's |
420 * error handler. Use [runGuarded] to achieve that. | 409 * error handler. Use [runGuarded] to achieve that. |
421 * | 410 * |
(...skipping 11 matching lines...) Expand all Loading... |
433 */ | 422 */ |
434 R runUnary<R, T>(R action(T argument), T argument); | 423 R runUnary<R, T>(R action(T argument), T argument); |
435 | 424 |
436 /** | 425 /** |
437 * Executes the given [action] with [argument1] and [argument2] in this | 426 * Executes the given [action] with [argument1] and [argument2] in this |
438 * zone. | 427 * zone. |
439 * | 428 * |
440 * As [run] except that [action] is called with two arguments instead of none. | 429 * As [run] except that [action] is called with two arguments instead of none. |
441 */ | 430 */ |
442 R runBinary<R, T1, T2>( | 431 R runBinary<R, T1, T2>( |
443 R action(T1 argument1, T2 argument2), T1 argument1, | 432 R action(T1 argument1, T2 argument2), T1 argument1, T2 argument2); |
444 T2 argument2); | |
445 | 433 |
446 /** | 434 /** |
447 * Executes the given [action] in this zone and catches synchronous | 435 * Executes the given [action] in this zone and catches synchronous |
448 * errors. | 436 * errors. |
449 * | 437 * |
450 * This function is equivalent to: | 438 * This function is equivalent to: |
451 * ``` | 439 * ``` |
452 * try { | 440 * try { |
453 * return this.run(action); | 441 * return this.run(action); |
454 * } catch (e, s) { | 442 * } catch (e, s) { |
455 * return this.handleUncaughtError(e, s); | 443 * return this.handleUncaughtError(e, s); |
456 * } | 444 * } |
457 * ``` | 445 * ``` |
458 * | 446 * |
459 * See [run]. | 447 * See [run]. |
460 */ | 448 */ |
461 R runGuarded<R>(R action()); | 449 R runGuarded<R>(R action()); |
462 | 450 |
463 /** | 451 /** |
464 * Executes the given [action] with [argument] in this zone and | 452 * Executes the given [action] with [argument] in this zone and |
465 * catches synchronous errors. | 453 * catches synchronous errors. |
466 * | 454 * |
467 * See [runGuarded]. | 455 * See [runGuarded]. |
468 */ | 456 */ |
469 R runUnaryGuarded<R, T>(R action(T argument), | 457 R runUnaryGuarded<R, T>(R action(T argument), T argument); |
470 T argument); | |
471 | 458 |
472 /** | 459 /** |
473 * Executes the given [action] with [argument1] and [argument2] in this | 460 * Executes the given [action] with [argument1] and [argument2] in this |
474 * zone and catches synchronous errors. | 461 * zone and catches synchronous errors. |
475 * | 462 * |
476 * See [runGuarded]. | 463 * See [runGuarded]. |
477 */ | 464 */ |
478 R runBinaryGuarded<R, T1, T2>( | 465 R runBinaryGuarded<R, T1, T2>( |
479 R action(T1 argument1, T2 argument2), T1 argument1, | 466 R action(T1 argument1, T2 argument2), T1 argument1, T2 argument2); |
480 T2 argument2); | |
481 | 467 |
482 /** | 468 /** |
483 * Registers the given callback in this zone. | 469 * Registers the given callback in this zone. |
484 * | 470 * |
485 * When implementing an asynchronous primitive that uses callbacks, the | 471 * When implementing an asynchronous primitive that uses callbacks, the |
486 * callback must be registered using [registerCallback] at the point where the | 472 * callback must be registered using [registerCallback] at the point where the |
487 * user provides the callback. This allows zones to record other information | 473 * user provides the callback. This allows zones to record other information |
488 * that they need at the same time, perhaps even wrapping the callback, so | 474 * that they need at the same time, perhaps even wrapping the callback, so |
489 * that the callback is prepared when it is later run in the same zones | 475 * that the callback is prepared when it is later run in the same zones |
490 * (using [run]). For example, a zone may decide | 476 * (using [run]). For example, a zone may decide |
491 * to store the stack trace (at the time of the registration) with the | 477 * to store the stack trace (at the time of the registration) with the |
492 * callback. | 478 * callback. |
493 * | 479 * |
494 * Returns the callback that should be used in place of the provided | 480 * Returns the callback that should be used in place of the provided |
495 * [callback]. Frequently zones simply return the original callback. | 481 * [callback]. Frequently zones simply return the original callback. |
496 * | 482 * |
497 * Custom zones may intercept this operation. The default implementation in | 483 * Custom zones may intercept this operation. The default implementation in |
498 * [Zone.ROOT] returns the original callback unchanged. | 484 * [Zone.ROOT] returns the original callback unchanged. |
499 */ | 485 */ |
500 ZoneCallback<R> registerCallback<R>(R callback()); | 486 ZoneCallback<R> registerCallback<R>(R callback()); |
501 | 487 |
502 /** | 488 /** |
503 * Registers the given callback in this zone. | 489 * Registers the given callback in this zone. |
504 * | 490 * |
505 * Similar to [registerCallback] but with a unary callback. | 491 * Similar to [registerCallback] but with a unary callback. |
506 */ | 492 */ |
507 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>( | 493 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(R callback(T arg)); |
508 R callback(T arg)); | |
509 | 494 |
510 /** | 495 /** |
511 * Registers the given callback in this zone. | 496 * Registers the given callback in this zone. |
512 * | 497 * |
513 * Similar to [registerCallback] but with a unary callback. | 498 * Similar to [registerCallback] but with a unary callback. |
514 */ | 499 */ |
515 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( | 500 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( |
516 R callback(T1 arg1, T2 arg2)); | 501 R callback(T1 arg1, T2 arg2)); |
517 | 502 |
518 /** | 503 /** |
519 * Equivalent to: | 504 * Equivalent to: |
520 * | 505 * |
521 * ZoneCallback registered = this.registerCallback(action); | 506 * ZoneCallback registered = this.registerCallback(action); |
522 * if (runGuarded) return () => this.runGuarded(registered); | 507 * if (runGuarded) return () => this.runGuarded(registered); |
523 * return () => this.run(registered); | 508 * return () => this.run(registered); |
524 * | 509 * |
525 */ | 510 */ |
526 ZoneCallback<R> bindCallback<R>( | 511 ZoneCallback<R> bindCallback<R>(R action(), {bool runGuarded: true}); |
527 R action(), { bool runGuarded: true }); | |
528 | 512 |
529 /** | 513 /** |
530 * Equivalent to: | 514 * Equivalent to: |
531 * | 515 * |
532 * ZoneCallback registered = this.registerUnaryCallback(action); | 516 * ZoneCallback registered = this.registerUnaryCallback(action); |
533 * if (runGuarded) return (arg) => this.runUnaryGuarded(registered, arg); | 517 * if (runGuarded) return (arg) => this.runUnaryGuarded(registered, arg); |
534 * return (arg) => thin.runUnary(registered, arg); | 518 * return (arg) => thin.runUnary(registered, arg); |
535 */ | 519 */ |
536 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>( | 520 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R action(T argument), |
537 R action(T argument), { bool runGuarded: true }); | 521 {bool runGuarded: true}); |
538 | 522 |
539 /** | 523 /** |
540 * Equivalent to: | 524 * Equivalent to: |
541 * | 525 * |
542 * ZoneCallback registered = registerBinaryCallback(action); | 526 * ZoneCallback registered = registerBinaryCallback(action); |
543 * if (runGuarded) { | 527 * if (runGuarded) { |
544 * return (arg1, arg2) => this.runBinaryGuarded(registered, arg); | 528 * return (arg1, arg2) => this.runBinaryGuarded(registered, arg); |
545 * } | 529 * } |
546 * return (arg1, arg2) => thin.runBinary(registered, arg1, arg2); | 530 * return (arg1, arg2) => thin.runBinary(registered, arg1, arg2); |
547 */ | 531 */ |
548 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( | 532 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( |
549 R action(T1 argument1, T2 argument2), | 533 R action(T1 argument1, T2 argument2), |
550 { bool runGuarded: true }); | 534 {bool runGuarded: true}); |
551 | 535 |
552 /** | 536 /** |
553 * Intercepts errors when added programatically to a `Future` or `Stream`. | 537 * Intercepts errors when added programatically to a `Future` or `Stream`. |
554 * | 538 * |
555 * When calling [Completer.completeError], [Stream.addError], | 539 * When calling [Completer.completeError], [Stream.addError], |
556 * or some [Future] constructors, the current zone is allowed to intercept | 540 * or some [Future] constructors, the current zone is allowed to intercept |
557 * and replace the error. | 541 * and replace the error. |
558 * | 542 * |
559 * Future constructors invoke this function when the error is received | 543 * Future constructors invoke this function when the error is received |
560 * directly, for example with [Future.error], or when the error is caught | 544 * directly, for example with [Future.error], or when the error is caught |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 ZoneDelegate _parentDelegate(_Zone zone) { | 649 ZoneDelegate _parentDelegate(_Zone zone) { |
666 if (zone.parent == null) return null; | 650 if (zone.parent == null) return null; |
667 return zone.parent._delegate; | 651 return zone.parent._delegate; |
668 } | 652 } |
669 | 653 |
670 class _ZoneDelegate implements ZoneDelegate { | 654 class _ZoneDelegate implements ZoneDelegate { |
671 final _Zone _delegationTarget; | 655 final _Zone _delegationTarget; |
672 | 656 |
673 _ZoneDelegate(this._delegationTarget); | 657 _ZoneDelegate(this._delegationTarget); |
674 | 658 |
675 R handleUncaughtError<R>( | 659 R handleUncaughtError<R>(Zone zone, error, StackTrace stackTrace) { |
676 Zone zone, error, StackTrace stackTrace) { | |
677 var implementation = _delegationTarget._handleUncaughtError; | 660 var implementation = _delegationTarget._handleUncaughtError; |
678 _Zone implZone = implementation.zone; | 661 _Zone implZone = implementation.zone; |
679 HandleUncaughtErrorHandler handler = implementation.function; | 662 HandleUncaughtErrorHandler handler = implementation.function; |
680 // TODO(floitsch): make this a generic method call on '<R>' once it's | 663 // TODO(floitsch): make this a generic method call on '<R>' once it's |
681 // supported. Remove the unnecessary cast. | 664 // supported. Remove the unnecessary cast. |
682 return handler( | 665 return handler(implZone, _parentDelegate(implZone), zone, error, stackTrace) |
683 implZone, _parentDelegate(implZone), zone, error, stackTrace) | |
684 as Object/*=R*/; | 666 as Object/*=R*/; |
685 } | 667 } |
686 | 668 |
687 R run<R>(Zone zone, R f()) { | 669 R run<R>(Zone zone, R f()) { |
688 var implementation = _delegationTarget._run; | 670 var implementation = _delegationTarget._run; |
689 _Zone implZone = implementation.zone; | 671 _Zone implZone = implementation.zone; |
690 RunHandler handler = implementation.function; | 672 RunHandler handler = implementation.function; |
691 // TODO(floitsch): make this a generic method call on '<R>' once it's | 673 // TODO(floitsch): make this a generic method call on '<R>' once it's |
692 // supported. Remove the unnecessary cast. | 674 // supported. Remove the unnecessary cast. |
693 return handler(implZone, _parentDelegate(implZone), zone, f) | 675 return handler(implZone, _parentDelegate(implZone), zone, f) |
694 as Object/*=R*/; | 676 as Object/*=R*/; |
695 } | 677 } |
696 | 678 |
697 R runUnary<R, T>(Zone zone, R f(T arg), T arg) { | 679 R runUnary<R, T>(Zone zone, R f(T arg), T arg) { |
698 var implementation = _delegationTarget._runUnary; | 680 var implementation = _delegationTarget._runUnary; |
699 _Zone implZone = implementation.zone; | 681 _Zone implZone = implementation.zone; |
700 RunUnaryHandler handler = implementation.function; | 682 RunUnaryHandler handler = implementation.function; |
701 // TODO(floitsch): make this a generic method call on '<R, T>' once it's | 683 // TODO(floitsch): make this a generic method call on '<R, T>' once it's |
702 // supported. Remove the unnecessary cast. | 684 // supported. Remove the unnecessary cast. |
703 return handler( | 685 return handler(implZone, _parentDelegate(implZone), zone, f, arg) |
704 implZone, _parentDelegate(implZone), zone, f, arg) as Object/*=R*/; | 686 as Object/*=R*/; |
705 } | 687 } |
706 | 688 |
707 R runBinary<R, T1, T2>(Zone zone, | 689 R runBinary<R, T1, T2>(Zone zone, R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { |
708 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { | |
709 var implementation = _delegationTarget._runBinary; | 690 var implementation = _delegationTarget._runBinary; |
710 _Zone implZone = implementation.zone; | 691 _Zone implZone = implementation.zone; |
711 RunBinaryHandler handler = implementation.function; | 692 RunBinaryHandler handler = implementation.function; |
712 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once | 693 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once |
713 // it's supported. Remove the unnecessary cast. | 694 // it's supported. Remove the unnecessary cast. |
714 return handler( | 695 return handler(implZone, _parentDelegate(implZone), zone, f, arg1, arg2) |
715 implZone, _parentDelegate(implZone), zone, f, arg1, arg2) | |
716 as Object/*=R*/; | 696 as Object/*=R*/; |
717 } | 697 } |
718 | 698 |
719 ZoneCallback<R> registerCallback<R>(Zone zone, R f()) { | 699 ZoneCallback<R> registerCallback<R>(Zone zone, R f()) { |
720 var implementation = _delegationTarget._registerCallback; | 700 var implementation = _delegationTarget._registerCallback; |
721 _Zone implZone = implementation.zone; | 701 _Zone implZone = implementation.zone; |
722 RegisterCallbackHandler handler = implementation.function; | 702 RegisterCallbackHandler handler = implementation.function; |
723 // TODO(floitsch): make this a generic method call on '<R>' once it's | 703 // TODO(floitsch): make this a generic method call on '<R>' once it's |
724 // supported. Remove the unnecessary cast. | 704 // supported. Remove the unnecessary cast. |
725 return handler(implZone, _parentDelegate(implZone), zone, f) | 705 return handler(implZone, _parentDelegate(implZone), zone, f) |
726 as Object/*=ZoneCallback<R>*/; | 706 as Object/*=ZoneCallback<R>*/; |
727 } | 707 } |
728 | 708 |
729 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>( | 709 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(Zone zone, R f(T arg)) { |
730 Zone zone, R f(T arg)) { | |
731 var implementation = _delegationTarget._registerUnaryCallback; | 710 var implementation = _delegationTarget._registerUnaryCallback; |
732 _Zone implZone = implementation.zone; | 711 _Zone implZone = implementation.zone; |
733 RegisterUnaryCallbackHandler handler = implementation.function; | 712 RegisterUnaryCallbackHandler handler = implementation.function; |
734 // TODO(floitsch): make this a generic method call on '<R, T>' once it's | 713 // TODO(floitsch): make this a generic method call on '<R, T>' once it's |
735 // supported. Remove the unnecessary cast. | 714 // supported. Remove the unnecessary cast. |
736 return handler(implZone, _parentDelegate(implZone), zone, f) | 715 return handler(implZone, _parentDelegate(implZone), zone, f) |
737 as Object/*=ZoneUnaryCallback<R, T>*/; | 716 as Object/*=ZoneUnaryCallback<R, T>*/; |
738 } | 717 } |
739 | 718 |
740 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( | 719 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( |
741 Zone zone, R f(T1 arg1, T2 arg2)) { | 720 Zone zone, R f(T1 arg1, T2 arg2)) { |
742 var implementation = _delegationTarget._registerBinaryCallback; | 721 var implementation = _delegationTarget._registerBinaryCallback; |
743 _Zone implZone = implementation.zone; | 722 _Zone implZone = implementation.zone; |
744 RegisterBinaryCallbackHandler handler = implementation.function; | 723 RegisterBinaryCallbackHandler handler = implementation.function; |
745 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once | 724 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once |
746 // it's supported. Remove the unnecessary cast. | 725 // it's supported. Remove the unnecessary cast. |
747 return handler(implZone, _parentDelegate(implZone), zone, f) | 726 return handler(implZone, _parentDelegate(implZone), zone, f) |
748 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; | 727 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; |
749 } | 728 } |
750 | 729 |
751 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) { | 730 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) { |
752 var implementation = _delegationTarget._errorCallback; | 731 var implementation = _delegationTarget._errorCallback; |
753 _Zone implZone = implementation.zone; | 732 _Zone implZone = implementation.zone; |
754 if (identical(implZone, _ROOT_ZONE)) return null; | 733 if (identical(implZone, _ROOT_ZONE)) return null; |
755 ErrorCallbackHandler handler = implementation.function; | 734 ErrorCallbackHandler handler = implementation.function; |
756 return handler(implZone, _parentDelegate(implZone), zone, | 735 return handler( |
757 error, stackTrace); | 736 implZone, _parentDelegate(implZone), zone, error, stackTrace); |
758 } | 737 } |
759 | 738 |
760 void scheduleMicrotask(Zone zone, f()) { | 739 void scheduleMicrotask(Zone zone, f()) { |
761 var implementation = _delegationTarget._scheduleMicrotask; | 740 var implementation = _delegationTarget._scheduleMicrotask; |
762 _Zone implZone = implementation.zone; | 741 _Zone implZone = implementation.zone; |
763 ScheduleMicrotaskHandler handler = implementation.function; | 742 ScheduleMicrotaskHandler handler = implementation.function; |
764 handler(implZone, _parentDelegate(implZone), zone, f); | 743 handler(implZone, _parentDelegate(implZone), zone, f); |
765 } | 744 } |
766 | 745 |
767 Timer createTimer(Zone zone, Duration duration, void f()) { | 746 Timer createTimer(Zone zone, Duration duration, void f()) { |
(...skipping 10 matching lines...) Expand all Loading... |
778 return handler(implZone, _parentDelegate(implZone), zone, period, f); | 757 return handler(implZone, _parentDelegate(implZone), zone, period, f); |
779 } | 758 } |
780 | 759 |
781 void print(Zone zone, String line) { | 760 void print(Zone zone, String line) { |
782 var implementation = _delegationTarget._print; | 761 var implementation = _delegationTarget._print; |
783 _Zone implZone = implementation.zone; | 762 _Zone implZone = implementation.zone; |
784 PrintHandler handler = implementation.function; | 763 PrintHandler handler = implementation.function; |
785 handler(implZone, _parentDelegate(implZone), zone, line); | 764 handler(implZone, _parentDelegate(implZone), zone, line); |
786 } | 765 } |
787 | 766 |
788 Zone fork(Zone zone, ZoneSpecification specification, | 767 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues) { |
789 Map zoneValues) { | |
790 var implementation = _delegationTarget._fork; | 768 var implementation = _delegationTarget._fork; |
791 _Zone implZone = implementation.zone; | 769 _Zone implZone = implementation.zone; |
792 ForkHandler handler = implementation.function; | 770 ForkHandler handler = implementation.function; |
793 return handler( | 771 return handler( |
794 implZone, _parentDelegate(implZone), zone, specification, zoneValues); | 772 implZone, _parentDelegate(implZone), zone, specification, zoneValues); |
795 } | 773 } |
796 } | 774 } |
797 | 775 |
798 | |
799 /** | 776 /** |
800 * Base class for Zone implementations. | 777 * Base class for Zone implementations. |
801 */ | 778 */ |
802 abstract class _Zone implements Zone { | 779 abstract class _Zone implements Zone { |
803 const _Zone(); | 780 const _Zone(); |
804 | 781 |
805 _ZoneFunction<RunHandler> get _run; | 782 _ZoneFunction<RunHandler> get _run; |
806 _ZoneFunction<RunUnaryHandler> get _runUnary; | 783 _ZoneFunction<RunUnaryHandler> get _runUnary; |
807 _ZoneFunction<RunBinaryHandler> get _runBinary; | 784 _ZoneFunction<RunBinaryHandler> get _runBinary; |
808 _ZoneFunction<RegisterCallbackHandler> get _registerCallback; | 785 _ZoneFunction<RegisterCallbackHandler> get _registerCallback; |
809 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback; | 786 _ZoneFunction<RegisterUnaryCallbackHandler> get _registerUnaryCallback; |
810 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback; | 787 _ZoneFunction<RegisterBinaryCallbackHandler> get _registerBinaryCallback; |
811 _ZoneFunction<ErrorCallbackHandler> get _errorCallback; | 788 _ZoneFunction<ErrorCallbackHandler> get _errorCallback; |
812 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask; | 789 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask; |
813 _ZoneFunction<CreateTimerHandler> get _createTimer; | 790 _ZoneFunction<CreateTimerHandler> get _createTimer; |
814 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer; | 791 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer; |
815 _ZoneFunction<PrintHandler> get _print; | 792 _ZoneFunction<PrintHandler> get _print; |
816 _ZoneFunction<ForkHandler> get _fork; | 793 _ZoneFunction<ForkHandler> get _fork; |
817 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError; | 794 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError; |
818 _Zone get parent; | 795 _Zone get parent; |
819 ZoneDelegate get _delegate; | 796 ZoneDelegate get _delegate; |
820 Map get _map; | 797 Map get _map; |
821 | 798 |
822 bool inSameErrorZone(Zone otherZone) { | 799 bool inSameErrorZone(Zone otherZone) { |
823 return identical(this, otherZone) || | 800 return identical(this, otherZone) || |
824 identical(errorZone, otherZone.errorZone); | 801 identical(errorZone, otherZone.errorZone); |
825 } | 802 } |
826 } | 803 } |
827 | 804 |
828 class _CustomZone extends _Zone { | 805 class _CustomZone extends _Zone { |
829 // The actual zone and implementation of each of these | 806 // The actual zone and implementation of each of these |
830 // inheritable zone functions. | 807 // inheritable zone functions. |
831 _ZoneFunction<RunHandler> _run; | 808 _ZoneFunction<RunHandler> _run; |
832 _ZoneFunction<RunUnaryHandler> _runUnary; | 809 _ZoneFunction<RunUnaryHandler> _runUnary; |
833 _ZoneFunction<RunBinaryHandler> _runBinary; | 810 _ZoneFunction<RunBinaryHandler> _runBinary; |
834 _ZoneFunction<RegisterCallbackHandler> _registerCallback; | 811 _ZoneFunction<RegisterCallbackHandler> _registerCallback; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
928 } | 905 } |
929 | 906 |
930 R runUnaryGuarded<R, T>(R f(T arg), T arg) { | 907 R runUnaryGuarded<R, T>(R f(T arg), T arg) { |
931 try { | 908 try { |
932 return runUnary(f, arg); | 909 return runUnary(f, arg); |
933 } catch (e, s) { | 910 } catch (e, s) { |
934 return handleUncaughtError(e, s); | 911 return handleUncaughtError(e, s); |
935 } | 912 } |
936 } | 913 } |
937 | 914 |
938 R runBinaryGuarded<R, T1, T2>( | 915 R runBinaryGuarded<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { |
939 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { | |
940 try { | 916 try { |
941 return runBinary(f, arg1, arg2); | 917 return runBinary(f, arg1, arg2); |
942 } catch (e, s) { | 918 } catch (e, s) { |
943 return handleUncaughtError(e, s); | 919 return handleUncaughtError(e, s); |
944 } | 920 } |
945 } | 921 } |
946 | 922 |
947 ZoneCallback<R> bindCallback<R>( | 923 ZoneCallback<R> bindCallback<R>(R f(), {bool runGuarded: true}) { |
948 R f(), { bool runGuarded: true }) { | |
949 var registered = registerCallback(f); | 924 var registered = registerCallback(f); |
950 if (runGuarded) { | 925 if (runGuarded) { |
951 return () => this.runGuarded(registered); | 926 return () => this.runGuarded(registered); |
952 } else { | 927 } else { |
953 return () => this.run(registered); | 928 return () => this.run(registered); |
954 } | 929 } |
955 } | 930 } |
956 | 931 |
957 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>( | 932 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R f(T arg), |
958 R f(T arg), { bool runGuarded: true }) { | 933 {bool runGuarded: true}) { |
959 var registered = registerUnaryCallback(f); | 934 var registered = registerUnaryCallback(f); |
960 if (runGuarded) { | 935 if (runGuarded) { |
961 return (arg) => this.runUnaryGuarded(registered, arg); | 936 return (arg) => this.runUnaryGuarded(registered, arg); |
962 } else { | 937 } else { |
963 return (arg) => this.runUnary(registered, arg); | 938 return (arg) => this.runUnary(registered, arg); |
964 } | 939 } |
965 } | 940 } |
966 | 941 |
967 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( | 942 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( |
968 R f(T1 arg1, T2 arg2), { bool runGuarded: true }) { | 943 R f(T1 arg1, T2 arg2), |
| 944 {bool runGuarded: true}) { |
969 var registered = registerBinaryCallback(f); | 945 var registered = registerBinaryCallback(f); |
970 if (runGuarded) { | 946 if (runGuarded) { |
971 return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2); | 947 return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2); |
972 } else { | 948 } else { |
973 return (arg1, arg2) => this.runBinary(registered, arg1, arg2); | 949 return (arg1, arg2) => this.runBinary(registered, arg1, arg2); |
974 } | 950 } |
975 } | 951 } |
976 | 952 |
977 operator [](Object key) { | 953 operator [](Object key) { |
978 var result = _map[key]; | 954 var result = _map[key]; |
(...skipping 16 matching lines...) Expand all Loading... |
995 | 971 |
996 // Methods that can be customized by the zone specification. | 972 // Methods that can be customized by the zone specification. |
997 | 973 |
998 R handleUncaughtError<R>(error, StackTrace stackTrace) { | 974 R handleUncaughtError<R>(error, StackTrace stackTrace) { |
999 var implementation = this._handleUncaughtError; | 975 var implementation = this._handleUncaughtError; |
1000 assert(implementation != null); | 976 assert(implementation != null); |
1001 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 977 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1002 HandleUncaughtErrorHandler handler = implementation.function; | 978 HandleUncaughtErrorHandler handler = implementation.function; |
1003 // TODO(floitsch): make this a generic method call on '<R>' once it's | 979 // TODO(floitsch): make this a generic method call on '<R>' once it's |
1004 // supported. Remove the unnecessary cast. | 980 // supported. Remove the unnecessary cast. |
1005 return handler( | 981 return handler(implementation.zone, parentDelegate, this, error, stackTrace) |
1006 implementation.zone, parentDelegate, this, error, stackTrace) | |
1007 as Object/*=R*/; | 982 as Object/*=R*/; |
1008 } | 983 } |
1009 | 984 |
1010 Zone fork({ZoneSpecification specification, Map zoneValues}) { | 985 Zone fork({ZoneSpecification specification, Map zoneValues}) { |
1011 var implementation = this._fork; | 986 var implementation = this._fork; |
1012 assert(implementation != null); | 987 assert(implementation != null); |
1013 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 988 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1014 ForkHandler handler = implementation.function; | 989 ForkHandler handler = implementation.function; |
1015 return handler(implementation.zone, parentDelegate, this, | 990 return handler( |
1016 specification, zoneValues); | 991 implementation.zone, parentDelegate, this, specification, zoneValues); |
1017 } | 992 } |
1018 | 993 |
1019 R run<R>(R f()) { | 994 R run<R>(R f()) { |
1020 var implementation = this._run; | 995 var implementation = this._run; |
1021 assert(implementation != null); | 996 assert(implementation != null); |
1022 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 997 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1023 RunHandler handler = implementation.function; | 998 RunHandler handler = implementation.function; |
1024 // TODO(floitsch): make this a generic method call on '<R>' once it's | 999 // TODO(floitsch): make this a generic method call on '<R>' once it's |
1025 // supported. Remove the unnecessary cast. | 1000 // supported. Remove the unnecessary cast. |
1026 return handler(implementation.zone, parentDelegate, this, f) | 1001 return handler(implementation.zone, parentDelegate, this, f) |
1027 as Object/*=R*/; | 1002 as Object/*=R*/; |
1028 } | 1003 } |
1029 | 1004 |
1030 R runUnary<R, T>(R f(T arg), T arg) { | 1005 R runUnary<R, T>(R f(T arg), T arg) { |
1031 var implementation = this._runUnary; | 1006 var implementation = this._runUnary; |
1032 assert(implementation != null); | 1007 assert(implementation != null); |
1033 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1008 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1034 RunUnaryHandler handler = implementation.function; | 1009 RunUnaryHandler handler = implementation.function; |
1035 // TODO(floitsch): make this a generic method call on '<R, T>' once it's | 1010 // TODO(floitsch): make this a generic method call on '<R, T>' once it's |
1036 // supported. Remove the unnecessary cast. | 1011 // supported. Remove the unnecessary cast. |
1037 return handler(implementation.zone, parentDelegate, this, f, arg) | 1012 return handler(implementation.zone, parentDelegate, this, f, arg) |
1038 as Object/*=R*/; | 1013 as Object/*=R*/; |
1039 } | 1014 } |
1040 | 1015 |
1041 R runBinary<R, T1, T2>( | 1016 R runBinary<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { |
1042 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { | |
1043 var implementation = this._runBinary; | 1017 var implementation = this._runBinary; |
1044 assert(implementation != null); | 1018 assert(implementation != null); |
1045 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1019 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1046 RunBinaryHandler handler = implementation.function; | 1020 RunBinaryHandler handler = implementation.function; |
1047 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once | 1021 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once |
1048 // it's supported. Remove the unnecessary cast. | 1022 // it's supported. Remove the unnecessary cast. |
1049 return handler( | 1023 return handler(implementation.zone, parentDelegate, this, f, arg1, arg2) |
1050 implementation.zone, parentDelegate, this, f, arg1, arg2) | |
1051 as Object/*=R*/; | 1024 as Object/*=R*/; |
1052 } | 1025 } |
1053 | 1026 |
1054 ZoneCallback<R> registerCallback<R>(R callback()) { | 1027 ZoneCallback<R> registerCallback<R>(R callback()) { |
1055 var implementation = this._registerCallback; | 1028 var implementation = this._registerCallback; |
1056 assert(implementation != null); | 1029 assert(implementation != null); |
1057 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1030 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1058 RegisterCallbackHandler handler = implementation.function; | 1031 RegisterCallbackHandler handler = implementation.function; |
1059 // TODO(floitsch): make this a generic method call on '<R>' once it's | 1032 // TODO(floitsch): make this a generic method call on '<R>' once it's |
1060 // supported. Remove the unnecessary cast. | 1033 // supported. Remove the unnecessary cast. |
1061 return handler(implementation.zone, parentDelegate, this, callback) | 1034 return handler(implementation.zone, parentDelegate, this, callback) |
1062 as Object/*=ZoneCallback<R>*/; | 1035 as Object/*=ZoneCallback<R>*/; |
1063 } | 1036 } |
1064 | 1037 |
1065 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>( | 1038 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(R callback(T arg)) { |
1066 R callback(T arg)) { | |
1067 var implementation = this._registerUnaryCallback; | 1039 var implementation = this._registerUnaryCallback; |
1068 assert(implementation != null); | 1040 assert(implementation != null); |
1069 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1041 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1070 RegisterUnaryCallbackHandler handler = implementation.function; | 1042 RegisterUnaryCallbackHandler handler = implementation.function; |
1071 // TODO(floitsch): make this a generic method call on '<R, T>' once it's | 1043 // TODO(floitsch): make this a generic method call on '<R, T>' once it's |
1072 // supported. Remove the unnecessary cast. | 1044 // supported. Remove the unnecessary cast. |
1073 return handler(implementation.zone, parentDelegate, this, callback) | 1045 return handler(implementation.zone, parentDelegate, this, callback) |
1074 as Object/*=ZoneUnaryCallback<R, T>*/; | 1046 as Object/*=ZoneUnaryCallback<R, T>*/; |
1075 } | 1047 } |
1076 | 1048 |
1077 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( | 1049 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( |
1078 R callback(T1 arg1, T2 arg2)) { | 1050 R callback(T1 arg1, T2 arg2)) { |
1079 var implementation = this._registerBinaryCallback; | 1051 var implementation = this._registerBinaryCallback; |
1080 assert(implementation != null); | 1052 assert(implementation != null); |
1081 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1053 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1082 RegisterBinaryCallbackHandler handler = implementation.function; | 1054 RegisterBinaryCallbackHandler handler = implementation.function; |
1083 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once | 1055 // TODO(floitsch): make this a generic method call on '<R, T1, T2>' once |
1084 // it's supported. Remove the unnecessary cast. | 1056 // it's supported. Remove the unnecessary cast. |
1085 return handler(implementation.zone, parentDelegate, this, callback) | 1057 return handler(implementation.zone, parentDelegate, this, callback) |
1086 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; | 1058 as Object/*=ZoneBinaryCallback<R, T1, T2>*/; |
1087 } | 1059 } |
1088 | 1060 |
1089 AsyncError errorCallback(Object error, StackTrace stackTrace) { | 1061 AsyncError errorCallback(Object error, StackTrace stackTrace) { |
1090 var implementation = this._errorCallback; | 1062 var implementation = this._errorCallback; |
1091 assert(implementation != null); | 1063 assert(implementation != null); |
1092 final Zone implementationZone = implementation.zone; | 1064 final Zone implementationZone = implementation.zone; |
1093 if (identical(implementationZone, _ROOT_ZONE)) return null; | 1065 if (identical(implementationZone, _ROOT_ZONE)) return null; |
1094 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone); | 1066 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone); |
1095 ErrorCallbackHandler handler = implementation.function; | 1067 ErrorCallbackHandler handler = implementation.function; |
1096 return handler( | 1068 return handler(implementationZone, parentDelegate, this, error, stackTrace); |
1097 implementationZone, parentDelegate, this, error, stackTrace); | |
1098 } | 1069 } |
1099 | 1070 |
1100 void scheduleMicrotask(void f()) { | 1071 void scheduleMicrotask(void f()) { |
1101 var implementation = this._scheduleMicrotask; | 1072 var implementation = this._scheduleMicrotask; |
1102 assert(implementation != null); | 1073 assert(implementation != null); |
1103 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1074 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1104 ScheduleMicrotaskHandler handler = implementation.function; | 1075 ScheduleMicrotaskHandler handler = implementation.function; |
1105 return handler(implementation.zone, parentDelegate, this, f); | 1076 return handler(implementation.zone, parentDelegate, this, f); |
1106 } | 1077 } |
1107 | 1078 |
1108 Timer createTimer(Duration duration, void f()) { | 1079 Timer createTimer(Duration duration, void f()) { |
1109 var implementation = this._createTimer; | 1080 var implementation = this._createTimer; |
1110 assert(implementation != null); | 1081 assert(implementation != null); |
1111 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1082 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1112 CreateTimerHandler handler = implementation.function; | 1083 CreateTimerHandler handler = implementation.function; |
1113 return handler(implementation.zone, parentDelegate, this, duration, f); | 1084 return handler(implementation.zone, parentDelegate, this, duration, f); |
1114 } | 1085 } |
1115 | 1086 |
1116 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { | 1087 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) { |
1117 var implementation = this._createPeriodicTimer; | 1088 var implementation = this._createPeriodicTimer; |
1118 assert(implementation != null); | 1089 assert(implementation != null); |
1119 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1090 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1120 CreatePeriodicTimerHandler handler = implementation.function; | 1091 CreatePeriodicTimerHandler handler = implementation.function; |
1121 return handler( | 1092 return handler(implementation.zone, parentDelegate, this, duration, f); |
1122 implementation.zone, parentDelegate, this, duration, f); | |
1123 } | 1093 } |
1124 | 1094 |
1125 void print(String line) { | 1095 void print(String line) { |
1126 var implementation = this._print; | 1096 var implementation = this._print; |
1127 assert(implementation != null); | 1097 assert(implementation != null); |
1128 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); | 1098 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone); |
1129 PrintHandler handler = implementation.function; | 1099 PrintHandler handler = implementation.function; |
1130 return handler(implementation.zone, parentDelegate, this, line); | 1100 return handler(implementation.zone, parentDelegate, this, line); |
1131 } | 1101 } |
1132 } | 1102 } |
(...skipping 13 matching lines...) Expand all Loading... |
1146 if (Zone._current == zone) return f(); | 1116 if (Zone._current == zone) return f(); |
1147 | 1117 |
1148 Zone old = Zone._enter(zone); | 1118 Zone old = Zone._enter(zone); |
1149 try { | 1119 try { |
1150 return f(); | 1120 return f(); |
1151 } finally { | 1121 } finally { |
1152 Zone._leave(old); | 1122 Zone._leave(old); |
1153 } | 1123 } |
1154 } | 1124 } |
1155 | 1125 |
1156 R _rootRunUnary<R, T>(Zone self, ZoneDelegate parent, Zone zone, | 1126 R _rootRunUnary<R, T>( |
1157 R f(T arg), T arg) { | 1127 Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg) { |
1158 if (Zone._current == zone) return f(arg); | 1128 if (Zone._current == zone) return f(arg); |
1159 | 1129 |
1160 Zone old = Zone._enter(zone); | 1130 Zone old = Zone._enter(zone); |
1161 try { | 1131 try { |
1162 return f(arg); | 1132 return f(arg); |
1163 } finally { | 1133 } finally { |
1164 Zone._leave(old); | 1134 Zone._leave(old); |
1165 } | 1135 } |
1166 } | 1136 } |
1167 | 1137 |
(...skipping 13 matching lines...) Expand all Loading... |
1181 Zone self, ZoneDelegate parent, Zone zone, R f()) { | 1151 Zone self, ZoneDelegate parent, Zone zone, R f()) { |
1182 return f; | 1152 return f; |
1183 } | 1153 } |
1184 | 1154 |
1185 ZoneUnaryCallback<R, T> _rootRegisterUnaryCallback<R, T>( | 1155 ZoneUnaryCallback<R, T> _rootRegisterUnaryCallback<R, T>( |
1186 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)) { | 1156 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)) { |
1187 return f; | 1157 return f; |
1188 } | 1158 } |
1189 | 1159 |
1190 ZoneBinaryCallback<R, T1, T2> _rootRegisterBinaryCallback<R, T1, T2>( | 1160 ZoneBinaryCallback<R, T1, T2> _rootRegisterBinaryCallback<R, T1, T2>( |
1191 Zone self, ZoneDelegate parent, Zone zone, | 1161 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)) { |
1192 R f(T1 arg1, T2 arg2)) { | |
1193 return f; | 1162 return f; |
1194 } | 1163 } |
1195 | 1164 |
1196 AsyncError _rootErrorCallback(Zone self, ZoneDelegate parent, Zone zone, | 1165 AsyncError _rootErrorCallback(Zone self, ZoneDelegate parent, Zone zone, |
1197 Object error, StackTrace stackTrace) => null; | 1166 Object error, StackTrace stackTrace) => |
| 1167 null; |
1198 | 1168 |
1199 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) { | 1169 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) { |
1200 if (!identical(_ROOT_ZONE, zone)) { | 1170 if (!identical(_ROOT_ZONE, zone)) { |
1201 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone); | 1171 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone); |
1202 f = zone.bindCallback(f, runGuarded: hasErrorHandler); | 1172 f = zone.bindCallback(f, runGuarded: hasErrorHandler); |
1203 // Use root zone as event zone if the function is already bound. | 1173 // Use root zone as event zone if the function is already bound. |
1204 zone = _ROOT_ZONE; | 1174 zone = _ROOT_ZONE; |
1205 } | 1175 } |
1206 _scheduleAsyncCallback(f); | 1176 _scheduleAsyncCallback(f); |
1207 } | 1177 } |
1208 | 1178 |
1209 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone, | 1179 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone, |
1210 Duration duration, void callback()) { | 1180 Duration duration, void callback()) { |
1211 if (!identical(_ROOT_ZONE, zone)) { | 1181 if (!identical(_ROOT_ZONE, zone)) { |
1212 callback = zone.bindCallback(callback); | 1182 callback = zone.bindCallback(callback); |
1213 } | 1183 } |
1214 return Timer._createTimer(duration, callback); | 1184 return Timer._createTimer(duration, callback); |
1215 } | 1185 } |
1216 | 1186 |
1217 Timer _rootCreatePeriodicTimer( | 1187 Timer _rootCreatePeriodicTimer(Zone self, ZoneDelegate parent, Zone zone, |
1218 Zone self, ZoneDelegate parent, Zone zone, | |
1219 Duration duration, void callback(Timer timer)) { | 1188 Duration duration, void callback(Timer timer)) { |
1220 if (!identical(_ROOT_ZONE, zone)) { | 1189 if (!identical(_ROOT_ZONE, zone)) { |
1221 // TODO(floitsch): the return type should be 'void'. | 1190 // TODO(floitsch): the return type should be 'void'. |
1222 callback = zone.bindUnaryCallback<dynamic, Timer>(callback); | 1191 callback = zone.bindUnaryCallback<dynamic, Timer>(callback); |
1223 } | 1192 } |
1224 return Timer._createPeriodicTimer(duration, callback); | 1193 return Timer._createPeriodicTimer(duration, callback); |
1225 } | 1194 } |
1226 | 1195 |
1227 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) { | 1196 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) { |
1228 printToConsole(line); | 1197 printToConsole(line); |
1229 } | 1198 } |
1230 | 1199 |
1231 void _printToZone(String line) { | 1200 void _printToZone(String line) { |
1232 Zone.current.print(line); | 1201 Zone.current.print(line); |
1233 } | 1202 } |
1234 | 1203 |
1235 Zone _rootFork(Zone self, ZoneDelegate parent, Zone zone, | 1204 Zone _rootFork(Zone self, ZoneDelegate parent, Zone zone, |
1236 ZoneSpecification specification, | 1205 ZoneSpecification specification, Map zoneValues) { |
1237 Map zoneValues) { | |
1238 // TODO(floitsch): it would be nice if we could get rid of this hack. | 1206 // TODO(floitsch): it would be nice if we could get rid of this hack. |
1239 // Change the static zoneOrDirectPrint function to go through zones | 1207 // Change the static zoneOrDirectPrint function to go through zones |
1240 // from now on. | 1208 // from now on. |
1241 printToZone = _printToZone; | 1209 printToZone = _printToZone; |
1242 | 1210 |
1243 if (specification == null) { | 1211 if (specification == null) { |
1244 specification = const ZoneSpecification(); | 1212 specification = const ZoneSpecification(); |
1245 } else if (specification is! _ZoneSpecification) { | 1213 } else if (specification is! _ZoneSpecification) { |
1246 throw new ArgumentError("ZoneSpecifications must be instantiated" | 1214 throw new ArgumentError("ZoneSpecifications must be instantiated" |
1247 " with the provided constructor."); | 1215 " with the provided constructor."); |
(...skipping 30 matching lines...) Expand all Loading... |
1278 const _ZoneFunction<RegisterBinaryCallbackHandler>( | 1246 const _ZoneFunction<RegisterBinaryCallbackHandler>( |
1279 _ROOT_ZONE, _rootRegisterBinaryCallback); | 1247 _ROOT_ZONE, _rootRegisterBinaryCallback); |
1280 _ZoneFunction<ErrorCallbackHandler> get _errorCallback => | 1248 _ZoneFunction<ErrorCallbackHandler> get _errorCallback => |
1281 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback); | 1249 const _ZoneFunction<ErrorCallbackHandler>(_ROOT_ZONE, _rootErrorCallback); |
1282 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask => | 1250 _ZoneFunction<ScheduleMicrotaskHandler> get _scheduleMicrotask => |
1283 const _ZoneFunction<ScheduleMicrotaskHandler>( | 1251 const _ZoneFunction<ScheduleMicrotaskHandler>( |
1284 _ROOT_ZONE, _rootScheduleMicrotask); | 1252 _ROOT_ZONE, _rootScheduleMicrotask); |
1285 _ZoneFunction<CreateTimerHandler> get _createTimer => | 1253 _ZoneFunction<CreateTimerHandler> get _createTimer => |
1286 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer); | 1254 const _ZoneFunction<CreateTimerHandler>(_ROOT_ZONE, _rootCreateTimer); |
1287 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer => | 1255 _ZoneFunction<CreatePeriodicTimerHandler> get _createPeriodicTimer => |
1288 const _ZoneFunction<CreatePeriodicTimerHandler>(_ROOT_ZONE, _rootCreatePer
iodicTimer); | 1256 const _ZoneFunction<CreatePeriodicTimerHandler>( |
| 1257 _ROOT_ZONE, _rootCreatePeriodicTimer); |
1289 _ZoneFunction<PrintHandler> get _print => | 1258 _ZoneFunction<PrintHandler> get _print => |
1290 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint); | 1259 const _ZoneFunction<PrintHandler>(_ROOT_ZONE, _rootPrint); |
1291 _ZoneFunction<ForkHandler> get _fork => | 1260 _ZoneFunction<ForkHandler> get _fork => |
1292 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork); | 1261 const _ZoneFunction<ForkHandler>(_ROOT_ZONE, _rootFork); |
1293 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError => | 1262 _ZoneFunction<HandleUncaughtErrorHandler> get _handleUncaughtError => |
1294 const _ZoneFunction<HandleUncaughtErrorHandler>( | 1263 const _ZoneFunction<HandleUncaughtErrorHandler>( |
1295 _ROOT_ZONE, _rootHandleUncaughtError); | 1264 _ROOT_ZONE, _rootHandleUncaughtError); |
1296 | 1265 |
1297 // The parent zone. | 1266 // The parent zone. |
1298 _Zone get parent => null; | 1267 _Zone get parent => null; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1336 try { | 1305 try { |
1337 if (identical(_ROOT_ZONE, Zone._current)) { | 1306 if (identical(_ROOT_ZONE, Zone._current)) { |
1338 return f(arg); | 1307 return f(arg); |
1339 } | 1308 } |
1340 return _rootRunUnary<R, T>(null, null, this, f, arg); | 1309 return _rootRunUnary<R, T>(null, null, this, f, arg); |
1341 } catch (e, s) { | 1310 } catch (e, s) { |
1342 return handleUncaughtError<R>(e, s); | 1311 return handleUncaughtError<R>(e, s); |
1343 } | 1312 } |
1344 } | 1313 } |
1345 | 1314 |
1346 R runBinaryGuarded<R, T1, T2>( | 1315 R runBinaryGuarded<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { |
1347 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { | |
1348 try { | 1316 try { |
1349 if (identical(_ROOT_ZONE, Zone._current)) { | 1317 if (identical(_ROOT_ZONE, Zone._current)) { |
1350 return f(arg1, arg2); | 1318 return f(arg1, arg2); |
1351 } | 1319 } |
1352 return _rootRunBinary<R, T1, T2>(null, null, this, f, arg1, arg2); | 1320 return _rootRunBinary<R, T1, T2>(null, null, this, f, arg1, arg2); |
1353 } catch (e, s) { | 1321 } catch (e, s) { |
1354 return handleUncaughtError<R>(e, s); | 1322 return handleUncaughtError<R>(e, s); |
1355 } | 1323 } |
1356 } | 1324 } |
1357 | 1325 |
1358 ZoneCallback<R> bindCallback<R>( | 1326 ZoneCallback<R> bindCallback<R>(R f(), {bool runGuarded: true}) { |
1359 R f(), { bool runGuarded: true }) { | |
1360 if (runGuarded) { | 1327 if (runGuarded) { |
1361 return () => this.runGuarded<R>(f); | 1328 return () => this.runGuarded<R>(f); |
1362 } else { | 1329 } else { |
1363 return () => this.run<R>(f); | 1330 return () => this.run<R>(f); |
1364 } | 1331 } |
1365 } | 1332 } |
1366 | 1333 |
1367 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>( | 1334 ZoneUnaryCallback<R, T> bindUnaryCallback<R, T>(R f(T arg), |
1368 R f(T arg), { bool runGuarded: true }) { | 1335 {bool runGuarded: true}) { |
1369 if (runGuarded) { | 1336 if (runGuarded) { |
1370 return (arg) => this.runUnaryGuarded<R, T>(f, arg); | 1337 return (arg) => this.runUnaryGuarded<R, T>(f, arg); |
1371 } else { | 1338 } else { |
1372 return (arg) => this.runUnary<R, T>(f, arg); | 1339 return (arg) => this.runUnary<R, T>(f, arg); |
1373 } | 1340 } |
1374 } | 1341 } |
1375 | 1342 |
1376 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( | 1343 ZoneBinaryCallback<R, T1, T2> bindBinaryCallback<R, T1, T2>( |
1377 R f(T1 arg1, T2 arg2), { bool runGuarded: true }) { | 1344 R f(T1 arg1, T2 arg2), |
| 1345 {bool runGuarded: true}) { |
1378 if (runGuarded) { | 1346 if (runGuarded) { |
1379 return (arg1, arg2) => | 1347 return (arg1, arg2) => this.runBinaryGuarded<R, T1, T2>(f, arg1, arg2); |
1380 this.runBinaryGuarded<R, T1, T2>(f, arg1, arg2); | |
1381 } else { | 1348 } else { |
1382 return (arg1, arg2) => this.runBinary<R, T1, T2>(f, arg1, arg2); | 1349 return (arg1, arg2) => this.runBinary<R, T1, T2>(f, arg1, arg2); |
1383 } | 1350 } |
1384 } | 1351 } |
1385 | 1352 |
1386 operator [](Object key) => null; | 1353 operator [](Object key) => null; |
1387 | 1354 |
1388 // Methods that can be customized by the zone specification. | 1355 // Methods that can be customized by the zone specification. |
1389 | 1356 |
1390 R handleUncaughtError<R>(error, StackTrace stackTrace) { | 1357 R handleUncaughtError<R>(error, StackTrace stackTrace) { |
1391 return _rootHandleUncaughtError(null, null, this, error, stackTrace); | 1358 return _rootHandleUncaughtError(null, null, this, error, stackTrace); |
1392 } | 1359 } |
1393 | 1360 |
1394 Zone fork({ZoneSpecification specification, Map zoneValues}) { | 1361 Zone fork({ZoneSpecification specification, Map zoneValues}) { |
1395 return _rootFork(null, null, this, specification, zoneValues); | 1362 return _rootFork(null, null, this, specification, zoneValues); |
1396 } | 1363 } |
1397 | 1364 |
1398 R run<R>(R f()) { | 1365 R run<R>(R f()) { |
1399 if (identical(Zone._current, _ROOT_ZONE)) return f(); | 1366 if (identical(Zone._current, _ROOT_ZONE)) return f(); |
1400 return _rootRun(null, null, this, f); | 1367 return _rootRun(null, null, this, f); |
1401 } | 1368 } |
1402 | 1369 |
1403 R runUnary<R, T>(R f(T arg), T arg) { | 1370 R runUnary<R, T>(R f(T arg), T arg) { |
1404 if (identical(Zone._current, _ROOT_ZONE)) return f(arg); | 1371 if (identical(Zone._current, _ROOT_ZONE)) return f(arg); |
1405 return _rootRunUnary(null, null, this, f, arg); | 1372 return _rootRunUnary(null, null, this, f, arg); |
1406 } | 1373 } |
1407 | 1374 |
1408 R runBinary<R, T1, T2>( | 1375 R runBinary<R, T1, T2>(R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { |
1409 R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) { | |
1410 if (identical(Zone._current, _ROOT_ZONE)) return f(arg1, arg2); | 1376 if (identical(Zone._current, _ROOT_ZONE)) return f(arg1, arg2); |
1411 return _rootRunBinary(null, null, this, f, arg1, arg2); | 1377 return _rootRunBinary(null, null, this, f, arg1, arg2); |
1412 } | 1378 } |
1413 | 1379 |
1414 ZoneCallback<R> registerCallback<R>(R f()) => f; | 1380 ZoneCallback<R> registerCallback<R>(R f()) => f; |
1415 | 1381 |
1416 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>( | 1382 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>(R f(T arg)) => f; |
1417 R f(T arg)) => f; | |
1418 | 1383 |
1419 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( | 1384 ZoneBinaryCallback<R, T1, T2> |
1420 R f(T1 arg1, T2 arg2)) => f; | 1385 registerBinaryCallback<R, T1, T2>(R f(T1 arg1, T2 arg2)) => f; |
1421 | 1386 |
1422 AsyncError errorCallback(Object error, StackTrace stackTrace) => null; | 1387 AsyncError errorCallback(Object error, StackTrace stackTrace) => null; |
1423 | 1388 |
1424 void scheduleMicrotask(void f()) { | 1389 void scheduleMicrotask(void f()) { |
1425 _rootScheduleMicrotask(null, null, this, f); | 1390 _rootScheduleMicrotask(null, null, this, f); |
1426 } | 1391 } |
1427 | 1392 |
1428 Timer createTimer(Duration duration, void f()) { | 1393 Timer createTimer(Duration duration, void f()) { |
1429 return Timer._createTimer(duration, f); | 1394 return Timer._createTimer(duration, f); |
1430 } | 1395 } |
(...skipping 28 matching lines...) Expand all Loading... |
1459 * }, onError: (e) { print("unused error handler"); }); | 1424 * }, onError: (e) { print("unused error handler"); }); |
1460 * }, onError: (e) { print("catches error of first error-zone."); }); | 1425 * }, onError: (e) { print("catches error of first error-zone."); }); |
1461 * | 1426 * |
1462 * Example: | 1427 * Example: |
1463 * | 1428 * |
1464 * runZoned(() { | 1429 * runZoned(() { |
1465 * new Future(() { throw "asynchronous error"; }); | 1430 * new Future(() { throw "asynchronous error"; }); |
1466 * }, onError: print); // Will print "asynchronous error". | 1431 * }, onError: print); // Will print "asynchronous error". |
1467 */ | 1432 */ |
1468 R runZoned<R>(R body(), | 1433 R runZoned<R>(R body(), |
1469 { Map zoneValues, | 1434 {Map zoneValues, ZoneSpecification zoneSpecification, Function onError}) { |
1470 ZoneSpecification zoneSpecification, | |
1471 Function onError }) { | |
1472 HandleUncaughtErrorHandler errorHandler; | 1435 HandleUncaughtErrorHandler errorHandler; |
1473 if (onError != null) { | 1436 if (onError != null) { |
1474 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, | 1437 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, error, |
1475 error, StackTrace stackTrace) { | 1438 StackTrace stackTrace) { |
1476 try { | 1439 try { |
1477 if (onError is ZoneBinaryCallback<R, dynamic, StackTrace>) { | 1440 if (onError is ZoneBinaryCallback<R, dynamic, StackTrace>) { |
1478 return self.parent.runBinary(onError, error, stackTrace); | 1441 return self.parent.runBinary(onError, error, stackTrace); |
1479 } | 1442 } |
1480 return self.parent.runUnary(onError, error); | 1443 return self.parent.runUnary(onError, error); |
1481 } catch(e, s) { | 1444 } catch (e, s) { |
1482 if (identical(e, error)) { | 1445 if (identical(e, error)) { |
1483 return parent.handleUncaughtError(zone, error, stackTrace); | 1446 return parent.handleUncaughtError(zone, error, stackTrace); |
1484 } else { | 1447 } else { |
1485 return parent.handleUncaughtError(zone, e, s); | 1448 return parent.handleUncaughtError(zone, e, s); |
1486 } | 1449 } |
1487 } | 1450 } |
1488 }; | 1451 }; |
1489 } | 1452 } |
1490 if (zoneSpecification == null) { | 1453 if (zoneSpecification == null) { |
1491 zoneSpecification = | 1454 zoneSpecification = |
1492 new ZoneSpecification(handleUncaughtError: errorHandler); | 1455 new ZoneSpecification(handleUncaughtError: errorHandler); |
1493 } else if (errorHandler != null) { | 1456 } else if (errorHandler != null) { |
1494 zoneSpecification = | 1457 zoneSpecification = new ZoneSpecification.from(zoneSpecification, |
1495 new ZoneSpecification.from(zoneSpecification, | 1458 handleUncaughtError: errorHandler); |
1496 handleUncaughtError: errorHandler); | |
1497 } | 1459 } |
1498 Zone zone = Zone.current.fork(specification: zoneSpecification, | 1460 Zone zone = Zone.current |
1499 zoneValues: zoneValues); | 1461 .fork(specification: zoneSpecification, zoneValues: zoneValues); |
1500 if (onError != null) { | 1462 if (onError != null) { |
1501 return zone.runGuarded(body); | 1463 return zone.runGuarded(body); |
1502 } else { | 1464 } else { |
1503 return zone.run(body); | 1465 return zone.run(body); |
1504 } | 1466 } |
1505 } | 1467 } |
OLD | NEW |