This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core, testability): PendingAsync service
A new PendingAsync service that is used to track pending async operations in Angular. VmTurnZone ties in to this service to track timers as async tasks and correctly handles canceled timers. Http registers async tasks for network requests. The Testability API uses the new API to provide a much more useful whenStable implementation.
- Loading branch information
Showing
12 changed files
with
264 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
library angular.core.pending_async; | ||
|
||
import 'dart:async'; | ||
import 'package:di/annotations.dart'; | ||
|
||
typedef void WhenStableCallback(); | ||
|
||
/** | ||
* Tracks pending operations and notifies when they are all complete. | ||
*/ | ||
@Injectable() | ||
class PendingAsync { | ||
/// a count of the number of pending async operations. | ||
int _numPending = 0; | ||
List<WhenStableCallback> _callbacks; | ||
|
||
/** | ||
* A count of the number of tracked pending async operations. | ||
*/ | ||
int get numPending => _numPending; | ||
|
||
/** | ||
* Register a callback to be called synchronously when the number of tracked | ||
* pending async operations reaches a count of zero from a non-zero count. | ||
*/ | ||
void whenStable(WhenStableCallback cb) { | ||
if (_numPending == 0) { | ||
cb(); | ||
return; | ||
} | ||
if (_callbacks == null) { | ||
_callbacks = <WhenStableCallback>[cb]; | ||
} else { | ||
_callbacks.add(cb); | ||
} | ||
} | ||
|
||
/** | ||
* Increase the counter of the number of tracked pending operations. Returns | ||
* the new count of the number of tracked pending operations. | ||
*/ | ||
int increaseCount([int delta = 1]) { | ||
if (delta == 0) { | ||
return _numPending; | ||
} | ||
_numPending += delta; | ||
if (_numPending < 0) { | ||
throw "Attempting to reduce pending async count below zero."; | ||
} else if (_numPending == 0) { | ||
_runAllCallbacks(); | ||
} | ||
return _numPending; | ||
} | ||
|
||
/** | ||
* Decrease the counter of the number of tracked pending operations. Returns | ||
* the new count of the number of tracked pending operations. | ||
*/ | ||
int decreaseCount([int delta = 1]) => increaseCount(-delta); | ||
|
||
void _runAllCallbacks() { | ||
while (_callbacks != null) { | ||
var callbacks = _callbacks; | ||
_callbacks = null; | ||
callbacks.forEach((fn) { fn(); }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.