| 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 /** | 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 Loading... |
| 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; | |
| 440 | 439 |
| 441 _ZoneTimer(this._zone, Duration duration, this._callback) { | 440 _ZoneTimer(this._zone, Duration duration, this._callback) { |
| 442 _zone.expectCallback(); | 441 _zone.expectCallback(); |
| 443 _timer = _createTimer(duration, this._run); | 442 _timer = _createTimer(duration, this._run); |
| 444 } | 443 } |
| 445 | 444 |
| 446 void _run() { | 445 void _run() { |
| 447 _isDone = true; | |
| 448 _zone.executeCallbackGuarded(_callback); | 446 _zone.executeCallbackGuarded(_callback); |
| 449 } | 447 } |
| 450 | 448 |
| 451 void cancel() { | 449 void cancel() { |
| 452 if (!_isDone) _zone.cancelCallbackExpectation(); | 450 if (_timer.isActive) _zone.cancelCallbackExpectation(); |
| 453 _isDone = true; | |
| 454 _timer.cancel(); | 451 _timer.cancel(); |
| 455 } | 452 } |
| 453 |
| 454 bool get isActive => _timer.isActive; |
| 456 } | 455 } |
| 457 | 456 |
| 458 typedef void _PeriodicTimerCallback(Timer timer); | 457 typedef void _PeriodicTimerCallback(Timer timer); |
| 459 | 458 |
| 460 /** | 459 /** |
| 461 * A [Timer] class for periodic callbacks that takes zones into account. | 460 * A [Timer] class for periodic callbacks that takes zones into account. |
| 462 */ | 461 */ |
| 463 class _PeriodicZoneTimer implements Timer { | 462 class _PeriodicZoneTimer implements Timer { |
| 464 final _Zone _zone; | 463 final _Zone _zone; |
| 465 final _PeriodicTimerCallback _callback; | 464 final _PeriodicTimerCallback _callback; |
| 466 Timer _timer; | 465 Timer _timer; |
| 467 bool _isDone = false; | |
| 468 | 466 |
| 469 _PeriodicZoneTimer(this._zone, Duration duration, this._callback) { | 467 _PeriodicZoneTimer(this._zone, Duration duration, this._callback) { |
| 470 _zone.expectCallback(); | 468 _zone.expectCallback(); |
| 471 _timer = _createPeriodicTimer(duration, this._run); | 469 _timer = _createPeriodicTimer(duration, this._run); |
| 472 } | 470 } |
| 473 | 471 |
| 474 void _run(Timer timer) { | 472 void _run(Timer timer) { |
| 475 assert(identical(_timer, timer)); | 473 assert(identical(_timer, timer)); |
| 476 _zone.executePeriodicCallbackGuarded(() { _callback(this); }); | 474 _zone.executePeriodicCallbackGuarded(() { _callback(this); }); |
| 477 } | 475 } |
| 478 | 476 |
| 479 void cancel() { | 477 void cancel() { |
| 480 if (!_isDone) _zone.cancelCallbackExpectation(); | 478 if (_timer.isActive) _zone.cancelCallbackExpectation(); |
| 481 _isDone = true; | |
| 482 _timer.cancel(); | 479 _timer.cancel(); |
| 483 } | 480 } |
| 481 |
| 482 bool get isActive => _timer.isActive; |
| 484 } | 483 } |
| 485 | 484 |
| 486 /** | 485 /** |
| 487 * Runs [body] in its own zone. | 486 * Runs [body] in its own zone. |
| 488 * | 487 * |
| 489 * If [onError] is non-null the zone is considered an error zone. All uncaught | 488 * 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 | 489 * errors, synchronous or asynchronous, in the zone are caught and handled |
| 491 * by the callback. | 490 * by the callback. |
| 492 * | 491 * |
| 493 * The [onDone] handler (if non-null) is invoked when the zone has no more | 492 * 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 Loading... |
| 550 if (onError == null && onDone == null) return body(); | 549 if (onError == null && onDone == null) return body(); |
| 551 if (onError == null) { | 550 if (onError == null) { |
| 552 _WaitForCompletionZone zone = | 551 _WaitForCompletionZone zone = |
| 553 new _WaitForCompletionZone(_Zone._current, onDone); | 552 new _WaitForCompletionZone(_Zone._current, onDone); |
| 554 return zone.runWaitForCompletion(body); | 553 return zone.runWaitForCompletion(body); |
| 555 } | 554 } |
| 556 if (onDone == null) onDone = _nullDoneHandler; | 555 if (onDone == null) onDone = _nullDoneHandler; |
| 557 _CatchErrorsZone zone = new _CatchErrorsZone(_Zone._current, onError, onDone); | 556 _CatchErrorsZone zone = new _CatchErrorsZone(_Zone._current, onError, onDone); |
| 558 return zone.runWaitForCompletion(body); | 557 return zone.runWaitForCompletion(body); |
| 559 } | 558 } |
| OLD | NEW |