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

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

Issue 18325006: Add isActive field on Timer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | Annotate | Revision Log
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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 429
430 typedef void _TimerCallback(); 430 typedef void _TimerCallback();
431 431
432 /** 432 /**
433 * A [Timer] class that takes zones into account. 433 * A [Timer] class that takes zones into account.
434 */ 434 */
435 class _ZoneTimer implements Timer { 435 class _ZoneTimer implements Timer {
436 final _Zone _zone; 436 final _Zone _zone;
437 final _TimerCallback _callback; 437 final _TimerCallback _callback;
438 Timer _timer; 438 Timer _timer;
439 bool _isDone = false; 439 bool _isDone = false;
floitsch 2013/07/01 12:49:43 Remove _isDone and use _timer._isActive instead.
zarah 2013/07/01 16:15:35 Done.
440 440
441 _ZoneTimer(this._zone, Duration duration, this._callback) { 441 _ZoneTimer(this._zone, Duration duration, this._callback) {
442 _zone.expectCallback(); 442 _zone.expectCallback();
443 _timer = _createTimer(duration, this._run); 443 _timer = _createTimer(duration, this._run);
444 } 444 }
445 445
446 void _run() { 446 void _run() {
447 _isDone = true; 447 _isDone = true;
448 _zone.executeCallbackGuarded(_callback); 448 _zone.executeCallbackGuarded(_callback);
449 } 449 }
450 450
451 void cancel() { 451 void cancel() {
452 if (!_isDone) _zone.cancelCallbackExpectation(); 452 if (!_isDone) _zone.cancelCallbackExpectation();
453 _isDone = true; 453 _isDone = true;
454 _timer.cancel(); 454 _timer.cancel();
455 } 455 }
456
457 bool get isActive => !_isDone;
456 } 458 }
457 459
458 typedef void _PeriodicTimerCallback(Timer timer); 460 typedef void _PeriodicTimerCallback(Timer timer);
459 461
460 /** 462 /**
461 * A [Timer] class for periodic callbacks that takes zones into account. 463 * A [Timer] class for periodic callbacks that takes zones into account.
462 */ 464 */
463 class _PeriodicZoneTimer implements Timer { 465 class _PeriodicZoneTimer implements Timer {
464 final _Zone _zone; 466 final _Zone _zone;
465 final _PeriodicTimerCallback _callback; 467 final _PeriodicTimerCallback _callback;
466 Timer _timer; 468 Timer _timer;
467 bool _isDone = false; 469 bool _isDone = false;
468 470
469 _PeriodicZoneTimer(this._zone, Duration duration, this._callback) { 471 _PeriodicZoneTimer(this._zone, Duration duration, this._callback) {
470 _zone.expectCallback(); 472 _zone.expectCallback();
471 _timer = _createPeriodicTimer(duration, this._run); 473 _timer = _createPeriodicTimer(duration, this._run);
472 } 474 }
473 475
474 void _run(Timer timer) { 476 void _run(Timer timer) {
475 assert(identical(_timer, timer)); 477 assert(identical(_timer, timer));
476 _zone.executePeriodicCallbackGuarded(() { _callback(this); }); 478 _zone.executePeriodicCallbackGuarded(() { _callback(this); });
477 } 479 }
478 480
479 void cancel() { 481 void cancel() {
480 if (!_isDone) _zone.cancelCallbackExpectation(); 482 if (!_isDone) _zone.cancelCallbackExpectation();
481 _isDone = true; 483 _isDone = true;
482 _timer.cancel(); 484 _timer.cancel();
483 } 485 }
486
487 bool get isActive => !_isDone;
floitsch 2013/07/01 12:49:43 ditto.
zarah 2013/07/01 16:15:35 Done.
484 } 488 }
485 489
486 /** 490 /**
487 * Runs [body] in its own zone. 491 * Runs [body] in its own zone.
488 * 492 *
489 * If [onError] is non-null the zone is considered an error zone. All uncaught 493 * If [onError] is non-null the zone is considered an error zone. All uncaught
490 * errors, synchronous or asynchronous, in the zone are caught and handled 494 * errors, synchronous or asynchronous, in the zone are caught and handled
491 * by the callback. 495 * by the callback.
492 * 496 *
493 * The [onDone] handler (if non-null) is invoked when the zone has no more 497 * The [onDone] handler (if non-null) is invoked when the zone has no more
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 if (onError == null && onDone == null) return body(); 554 if (onError == null && onDone == null) return body();
551 if (onError == null) { 555 if (onError == null) {
552 _WaitForCompletionZone zone = 556 _WaitForCompletionZone zone =
553 new _WaitForCompletionZone(_Zone._current, onDone); 557 new _WaitForCompletionZone(_Zone._current, onDone);
554 return zone.runWaitForCompletion(body); 558 return zone.runWaitForCompletion(body);
555 } 559 }
556 if (onDone == null) onDone = _nullDoneHandler; 560 if (onDone == null) onDone = _nullDoneHandler;
557 _CatchErrorsZone zone = new _CatchErrorsZone(_Zone._current, onError, onDone); 561 _CatchErrorsZone zone = new _CatchErrorsZone(_Zone._current, onError, onDone);
558 return zone.runWaitForCompletion(body); 562 return zone.runWaitForCompletion(body);
559 } 563 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698