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

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

Issue 23926011: Rewrite Futures. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix bad asserts. Created 7 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « sdk/lib/async/stream_pipe.dart ('k') | tests/lib/async/future_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 /** 7 /**
8 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous 8 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous
9 * callbacks are executed in the zone they have been queued in. For example, 9 * callbacks are executed in the zone they have been queued in. For example,
10 * the callback of a `future.then` is executed in the same zone as the one where 10 * the callback of a `future.then` is executed in the same zone as the one where
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 void executePeriodicCallbackGuarded(void f()); 80 void executePeriodicCallbackGuarded(void f());
81 81
82 /** 82 /**
83 * Executes [f] in `this` zone. 83 * Executes [f] in `this` zone.
84 * 84 *
85 * The behavior of this method should be the same as 85 * The behavior of this method should be the same as
86 * [executePeriodicCallback] except that it can have a return value. 86 * [executePeriodicCallback] except that it can have a return value.
87 * 87 *
88 * Returns the result of the invocation. 88 * Returns the result of the invocation.
89 */ 89 */
90 runFromChildZone(f()); 90 dynamic runFromChildZone(f());
91 91
92 /** 92 /**
93 * Same as [runFromChildZone] but catches uncaught errors and gives them to 93 * Same as [runFromChildZone] but catches uncaught errors and gives them to
94 * [handleUncaughtError]. 94 * [handleUncaughtError].
95 */ 95 */
96 runFromChildZoneGuarded(f()); 96 dynamic runFromChildZoneGuarded(f());
97 97
98 /** 98 /**
99 * Runs [f] asynchronously in [zone]. 99 * Runs [f] asynchronously in [zone].
100 */ 100 */
101 void runAsync(void f(), _Zone zone); 101 void runAsync(void f(), _Zone zone);
102 102
103 /** 103 /**
104 * Creates a Timer where the callback is executed in this zone. 104 * Creates a Timer where the callback is executed in this zone.
105 */ 105 */
106 Timer createTimer(Duration duration, void callback()); 106 Timer createTimer(Duration duration, void callback());
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 } 174 }
175 175
176 /** 176 /**
177 * Cleans up this zone when it is done. 177 * Cleans up this zone when it is done.
178 * 178 *
179 * This releases internal memore structures that are no longer necessary. 179 * This releases internal memore structures that are no longer necessary.
180 * 180 *
181 * A zone is done when its dynamic extent has finished executing and 181 * A zone is done when its dynamic extent has finished executing and
182 * there are no outstanding asynchronous callbacks. 182 * there are no outstanding asynchronous callbacks.
183 */ 183 */
184 _dispose() { 184 void _dispose() {
185 if (_parentZone != null) { 185 if (_parentZone != null) {
186 _parentZone._removeChild(this); 186 _parentZone._removeChild(this);
187 } 187 }
188 } 188 }
189 189
190 /** 190 /**
191 * Checks if the zone is done and doesn't have any outstanding callbacks 191 * Checks if the zone is done and doesn't have any outstanding callbacks
192 * anymore. 192 * anymore.
193 * 193 *
194 * This method is called when an operation has decremented the 194 * This method is called when an operation has decremented the
(...skipping 16 matching lines...) Expand all
211 } 211 }
212 212
213 void executePeriodicCallback(void f()) { 213 void executePeriodicCallback(void f()) {
214 this._runUnguarded(f); 214 this._runUnguarded(f);
215 } 215 }
216 216
217 void executePeriodicCallbackGuarded(void f()) { 217 void executePeriodicCallbackGuarded(void f()) {
218 this._runGuarded(f); 218 this._runGuarded(f);
219 } 219 }
220 220
221 runFromChildZone(f()) => this._runUnguarded(f); 221 dynamic runFromChildZone(f()) => this._runUnguarded(f);
222 runFromChildZoneGuarded(f()) => this._runGuarded(f); 222 dynamic runFromChildZoneGuarded(f()) => this._runGuarded(f);
223 223
224 _runInZone(f(), bool handleUncaught) { 224 dynamic _runInZone(f(), bool handleUncaught) {
225 if (identical(_Zone._current, this) 225 if (identical(_Zone._current, this)
226 && !handleUncaught 226 && !handleUncaught
227 && _isExecutingCallback) { 227 && _isExecutingCallback) {
228 // No need to go through a try/catch. 228 // No need to go through a try/catch.
229 return f(); 229 return f();
230 } 230 }
231 231
232 _Zone oldZone = _Zone._current; 232 _Zone oldZone = _Zone._current;
233 _Zone._current = this; 233 _Zone._current = this;
234 // While we are executing the function we don't want to have other 234 // While we are executing the function we don't want to have other
(...skipping 19 matching lines...) Expand all
254 _Zone._current = oldZone; 254 _Zone._current = oldZone;
255 _checkIfDone(); 255 _checkIfDone();
256 } 256 }
257 } 257 }
258 258
259 /** 259 /**
260 * Runs the function and catches uncaught errors. 260 * Runs the function and catches uncaught errors.
261 * 261 *
262 * Uncaught errors are given to [handleUncaughtError]. 262 * Uncaught errors are given to [handleUncaughtError].
263 */ 263 */
264 _runGuarded(void f()) { 264 dynamic _runGuarded(void f()) {
265 return _runInZone(f, true); 265 return _runInZone(f, true);
266 } 266 }
267 267
268 /** 268 /**
269 * Runs the function but doesn't catch uncaught errors. 269 * Runs the function but doesn't catch uncaught errors.
270 */ 270 */
271 _runUnguarded(void f()) { 271 dynamic _runUnguarded(void f()) {
272 return _runInZone(f, false); 272 return _runInZone(f, false);
273 } 273 }
274 274
275 runAsync(void f(), _Zone zone) => _parentZone.runAsync(f, zone); 275 void runAsync(void f(), _Zone zone) => _parentZone.runAsync(f, zone);
276 276
277 // TODO(floitsch): the zone should just forward to the parent zone. The 277 // TODO(floitsch): the zone should just forward to the parent zone. The
278 // default zone should then create the _ZoneTimer. 278 // default zone should then create the _ZoneTimer.
279 Timer createTimer(Duration duration, void callback()) { 279 Timer createTimer(Duration duration, void callback()) {
280 return new _ZoneTimer(this, duration, callback); 280 return new _ZoneTimer(this, duration, callback);
281 } 281 }
282 282
283 // TODO(floitsch): the zone should just forward to the parent zone. The 283 // TODO(floitsch): the zone should just forward to the parent zone. The
284 // default zone should then create the _ZoneTimer. 284 // default zone should then create the _ZoneTimer.
285 Timer createPeriodicTimer(Duration duration, void callback(Timer timer)) { 285 Timer createPeriodicTimer(Duration duration, void callback(Timer timer)) {
(...skipping 12 matching lines...) Expand all
298 } 298 }
299 299
300 /** 300 /**
301 * The default-zone that conceptually surrounds the `main` function. 301 * The default-zone that conceptually surrounds the `main` function.
302 */ 302 */
303 class _DefaultZone extends _ZoneBase { 303 class _DefaultZone extends _ZoneBase {
304 _DefaultZone() : super._defaultZone(); 304 _DefaultZone() : super._defaultZone();
305 305
306 _Zone get _errorZone => this; 306 _Zone get _errorZone => this;
307 307
308 handleUncaughtError(error) { 308 void handleUncaughtError(error) {
309 _scheduleAsyncCallback(() { 309 _scheduleAsyncCallback(() {
310 print("Uncaught Error: ${error}"); 310 print("Uncaught Error: ${error}");
311 var trace = getAttachedStackTrace(error); 311 var trace = getAttachedStackTrace(error);
312 _attachStackTrace(error, null); 312 _attachStackTrace(error, null);
313 if (trace != null) { 313 if (trace != null) {
314 print("Stack Trace:\n$trace\n"); 314 print("Stack Trace:\n$trace\n");
315 } 315 }
316 throw error; 316 throw error;
317 }); 317 });
318 } 318 }
(...skipping 15 matching lines...) Expand all
334 334
335 /** 335 /**
336 * A zone that executes a callback when the zone is dead. 336 * A zone that executes a callback when the zone is dead.
337 */ 337 */
338 class _WaitForCompletionZone extends _ZoneBase { 338 class _WaitForCompletionZone extends _ZoneBase {
339 final _CompletionCallback _onDone; 339 final _CompletionCallback _onDone;
340 340
341 _WaitForCompletionZone(_Zone parentZone, this._onDone) : super(parentZone); 341 _WaitForCompletionZone(_Zone parentZone, this._onDone) : super(parentZone);
342 342
343 /** 343 /**
344 * Runs the given function asynchronously. Executes the [_onDone] callback 344 * Runs the given function.
345 * when the zone is done. 345 *
346 * Executes the [_onDone] callback when the zone is done.
346 */ 347 */
347 runWaitForCompletion(void f()) { 348 dynamic runWaitForCompletion(void f()) {
348 return this._runUnguarded(f); 349 return this._runUnguarded(f);
349 } 350 }
350 351
351 _dispose() { 352 void _dispose() {
352 super._dispose(); 353 super._dispose();
353 _onDone(); 354 _onDone();
354 } 355 }
355 356
356 String toString() => "WaitForCompletion ${super.toString()}"; 357 String toString() => "WaitForCompletion ${super.toString()}";
357 } 358 }
358 359
359 typedef void _HandleErrorCallback(error); 360 typedef void _HandleErrorCallback(error);
360 361
361 /** 362 /**
362 * A zone that collects all uncaught errors and provides them in a stream. 363 * A zone that collects all uncaught errors and provides them in a stream.
363 * The stream is closed when the zone is done. 364 * The stream is closed when the zone is done.
364 */ 365 */
365 class _CatchErrorsZone extends _WaitForCompletionZone { 366 class _CatchErrorsZone extends _WaitForCompletionZone {
366 final _HandleErrorCallback _handleError; 367 final _HandleErrorCallback _handleError;
367 368
368 _CatchErrorsZone(_Zone parentZone, this._handleError, void onDone()) 369 _CatchErrorsZone(_Zone parentZone, this._handleError, void onDone())
369 : super(parentZone, onDone); 370 : super(parentZone, onDone);
370 371
371 _Zone get _errorZone => this; 372 _Zone get _errorZone => this;
372 373
373 handleUncaughtError(error) { 374 void handleUncaughtError(error) {
374 try { 375 try {
375 _handleError(error); 376 _handleError(error);
376 } catch(e, s) { 377 } catch(e, s) {
377 if (identical(e, s)) { 378 if (identical(e, s)) {
378 _parentZone.handleUncaughtError(error); 379 _parentZone.handleUncaughtError(error);
379 } else { 380 } else {
380 _parentZone.handleUncaughtError(_asyncError(e, s)); 381 _parentZone.handleUncaughtError(_asyncError(e, s));
381 } 382 }
382 } 383 }
383 } 384 }
384 385
385 /** 386 /**
386 * Runs the given function asynchronously. Executes the [_onDone] callback 387 * Runs the given function asynchronously. Executes the [_onDone] callback
387 * when the zone is done. 388 * when the zone is done.
388 */ 389 */
389 runWaitForCompletion(void f()) { 390 dynamic runWaitForCompletion(void f()) {
390 return this._runGuarded(f); 391 return this._runGuarded(f);
391 } 392 }
392 393
393 String toString() => "CatchErrors ${super.toString()}"; 394 String toString() => "CatchErrors ${super.toString()}";
394 } 395 }
395 396
396 typedef void _RunAsyncInterceptor(void callback()); 397 typedef void _RunAsyncInterceptor(void callback());
397 398
398 class _RunAsyncZone extends _ZoneBase { 399 class _RunAsyncZone extends _ZoneBase {
399 final _RunAsyncInterceptor _runAsyncInterceptor; 400 final _RunAsyncInterceptor _runAsyncInterceptor;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 if (onError == null && onDone == null) return body(); 535 if (onError == null && onDone == null) return body();
535 if (onError == null) { 536 if (onError == null) {
536 _WaitForCompletionZone zone = 537 _WaitForCompletionZone zone =
537 new _WaitForCompletionZone(_Zone._current, onDone); 538 new _WaitForCompletionZone(_Zone._current, onDone);
538 return zone.runWaitForCompletion(body); 539 return zone.runWaitForCompletion(body);
539 } 540 }
540 if (onDone == null) onDone = _nullDoneHandler; 541 if (onDone == null) onDone = _nullDoneHandler;
541 _CatchErrorsZone zone = new _CatchErrorsZone(_Zone._current, onError, onDone); 542 _CatchErrorsZone zone = new _CatchErrorsZone(_Zone._current, onError, onDone);
542 return zone.runWaitForCompletion(body); 543 return zone.runWaitForCompletion(body);
543 } 544 }
OLDNEW
« no previous file with comments | « sdk/lib/async/stream_pipe.dart ('k') | tests/lib/async/future_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698