@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -90,7 +90,7 @@ class PublicTestability implements _JsObjectProxyable {
|
||||
'findBindings': (bindingString, [exactMatch, allowNonElementNodes]) =>
|
||||
findBindings(bindingString, exactMatch, allowNonElementNodes),
|
||||
'isStable': () => isStable(),
|
||||
'whenStable': (callback) => whenStable(() => callback.apply([]))
|
||||
'whenStable': (callback) => whenStable((didWork) => callback.apply([didWork]))
|
||||
})..['_dart_'] = this;
|
||||
}
|
||||
}
|
||||
@ -116,16 +116,38 @@ class BrowserGetTestability implements GetTestability {
|
||||
}
|
||||
throw 'Could not find testability for element.';
|
||||
});
|
||||
js.context['getAllAngularTestabilities'] = _jsify(() {
|
||||
var getAllAngularTestabilities = () {
|
||||
var registry = js.context['ngTestabilityRegistries'];
|
||||
var result = [];
|
||||
for (int i = 0; i < registry.length; i++) {
|
||||
var testabilities =
|
||||
registry[i].callMethod('getAllAngularTestabilities');
|
||||
registry[i].callMethod('getAllAngularTestabilities');
|
||||
if (testabilities != null) result.addAll(testabilities);
|
||||
}
|
||||
return _jsify(result);
|
||||
};
|
||||
js.context['getAllAngularTestabilities'] =
|
||||
_jsify(getAllAngularTestabilities);
|
||||
|
||||
var whenAllStable = _jsify((callback) {
|
||||
var testabilities = getAllAngularTestabilities();
|
||||
var count = testabilities.length;
|
||||
var didWork = false;
|
||||
var decrement = _jsify((bool didWork_) {
|
||||
didWork = didWork || didWork_;
|
||||
count--;
|
||||
if (count == 0) {
|
||||
callback.apply([didWork]);
|
||||
}
|
||||
});
|
||||
testabilities.forEach((testability) {
|
||||
testability.callMethod('whenStable', [decrement]);
|
||||
});
|
||||
});
|
||||
if (js.context['frameworkStabilizers'] == null) {
|
||||
js.context['frameworkStabilizers'] = new js.JsArray();
|
||||
}
|
||||
js.context['frameworkStabilizers'].add(whenAllStable);
|
||||
}
|
||||
jsRegistry.add(this._createRegistry(registry));
|
||||
}
|
||||
@ -163,4 +185,4 @@ class BrowserGetTestability implements GetTestability {
|
||||
});
|
||||
return object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,25 @@ export class BrowserGetTestability implements GetTestability {
|
||||
var testabilities = registry.getAllTestabilities();
|
||||
return testabilities.map((testability) => { return new PublicTestability(testability); });
|
||||
};
|
||||
|
||||
var whenAllStable = (callback) => {
|
||||
var testabilities = global.getAllAngularTestabilities();
|
||||
var count = testabilities.length;
|
||||
var didWork = false;
|
||||
var decrement = function(didWork_) {
|
||||
didWork = didWork || didWork_;
|
||||
count--;
|
||||
if (count == 0) {
|
||||
callback(didWork);
|
||||
}
|
||||
};
|
||||
testabilities.forEach(function(testability) { testability.whenStable(decrement); });
|
||||
};
|
||||
|
||||
if (!global.frameworkStabilizers) {
|
||||
global.frameworkStabilizers = ListWrapper.createGrowableSize(0);
|
||||
}
|
||||
global.frameworkStabilizers.push(whenAllStable);
|
||||
}
|
||||
|
||||
findTestabilityInTree(registry: TestabilityRegistry, elem: any,
|
||||
|
Reference in New Issue
Block a user