feat(testability): Expose function frameworkStabilizers

Closes #5485
This commit is contained in:
Hank Duan
2016-01-05 12:56:24 -08:00
parent 95248f46a1
commit 69ae3634c7
6 changed files with 159 additions and 8 deletions
@@ -15,6 +15,13 @@ import {PromiseWrapper, ObservableWrapper} from 'angular2/src/facade/async';
export class Testability {
/** @internal */
_pendingCount: number = 0;
/**
* Whether any work was done since the last 'whenStable' callback. This is
* useful to detect if this could have potentially destabilized another
* component while it is stabilizing.
* @internal
*/
_didWork: boolean = false;
/** @internal */
_callbacks: Function[] = [];
/** @internal */
@@ -23,8 +30,10 @@ export class Testability {
/** @internal */
_watchAngularEvents(_ngZone: NgZone): void {
ObservableWrapper.subscribe(_ngZone.onTurnStart,
(_) => { this._isAngularEventPending = true; });
ObservableWrapper.subscribe(_ngZone.onTurnStart, (_) => {
this._didWork = true;
this._isAngularEventPending = true;
});
_ngZone.runOutsideAngular(() => {
ObservableWrapper.subscribe(_ngZone.onEventDone, (_) => {
@@ -38,6 +47,7 @@ export class Testability {
increasePendingRequestCount(): number {
this._pendingCount += 1;
this._didWork = true;
return this._pendingCount;
}
@@ -55,14 +65,16 @@ export class Testability {
/** @internal */
_runCallbacksIfReady(): void {
if (!this.isStable()) {
this._didWork = true;
return; // Not ready
}
// Schedules the call backs in a new frame so that it is always async.
PromiseWrapper.resolve(null).then((_) => {
while (this._callbacks.length !== 0) {
(this._callbacks.pop())();
(this._callbacks.pop())(this._didWork);
}
this._didWork = false;
});
}