angular2
Version:
Angular 2 - a web framework for modern web apps
145 lines (144 loc) • 5.58 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable } from 'angular2/src/core/di';
import { Map, MapWrapper } from 'angular2/src/facade/collection';
import { CONST, CONST_EXPR, scheduleMicroTask } from 'angular2/src/facade/lang';
import { BaseException } from 'angular2/src/facade/exceptions';
import { NgZone } from '../zone/ng_zone';
import { ObservableWrapper } from 'angular2/src/facade/async';
/**
* The Testability service provides testing hooks that can be accessed from
* the browser and by services such as Protractor. Each bootstrapped Angular
* application on the page will have an instance of Testability.
*/
export let Testability = class Testability {
constructor(_ngZone) {
this._ngZone = _ngZone;
/** @internal */
this._pendingCount = 0;
/** @internal */
this._isZoneStable = true;
/**
* 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
*/
this._didWork = false;
/** @internal */
this._callbacks = [];
this._watchAngularEvents();
}
/** @internal */
_watchAngularEvents() {
ObservableWrapper.subscribe(this._ngZone.onUnstable, (_) => {
this._didWork = true;
this._isZoneStable = false;
});
this._ngZone.runOutsideAngular(() => {
ObservableWrapper.subscribe(this._ngZone.onStable, (_) => {
NgZone.assertNotInAngularZone();
scheduleMicroTask(() => {
this._isZoneStable = true;
this._runCallbacksIfReady();
});
});
});
}
increasePendingRequestCount() {
this._pendingCount += 1;
this._didWork = true;
return this._pendingCount;
}
decreasePendingRequestCount() {
this._pendingCount -= 1;
if (this._pendingCount < 0) {
throw new BaseException('pending async requests below zero');
}
this._runCallbacksIfReady();
return this._pendingCount;
}
isStable() {
return this._isZoneStable && this._pendingCount == 0 && !this._ngZone.hasPendingMacrotasks;
}
/** @internal */
_runCallbacksIfReady() {
if (this.isStable()) {
// Schedules the call backs in a new frame so that it is always async.
scheduleMicroTask(() => {
while (this._callbacks.length !== 0) {
(this._callbacks.pop())(this._didWork);
}
this._didWork = false;
});
}
else {
// Not Ready
this._didWork = true;
}
}
whenStable(callback) {
this._callbacks.push(callback);
this._runCallbacksIfReady();
}
getPendingRequestCount() { return this._pendingCount; }
findBindings(using, provider, exactMatch) {
// TODO(juliemr): implement.
return [];
}
findProviders(using, provider, exactMatch) {
// TODO(juliemr): implement.
return [];
}
};
Testability = __decorate([
Injectable(),
__metadata('design:paramtypes', [NgZone])
], Testability);
/**
* A global registry of {@link Testability} instances for specific elements.
*/
export let TestabilityRegistry = class TestabilityRegistry {
constructor() {
/** @internal */
this._applications = new Map();
_testabilityGetter.addToWindow(this);
}
registerApplication(token, testability) {
this._applications.set(token, testability);
}
getTestability(elem) { return this._applications.get(elem); }
getAllTestabilities() { return MapWrapper.values(this._applications); }
getAllRootElements() { return MapWrapper.keys(this._applications); }
findTestabilityInTree(elem, findInAncestors = true) {
return _testabilityGetter.findTestabilityInTree(this, elem, findInAncestors);
}
};
TestabilityRegistry = __decorate([
Injectable(),
__metadata('design:paramtypes', [])
], TestabilityRegistry);
let _NoopGetTestability = class _NoopGetTestability {
addToWindow(registry) { }
findTestabilityInTree(registry, elem, findInAncestors) {
return null;
}
};
_NoopGetTestability = __decorate([
CONST(),
__metadata('design:paramtypes', [])
], _NoopGetTestability);
/**
* Set the {@link GetTestability} implementation used by the Angular testing framework.
*/
export function setTestabilityGetter(getter) {
_testabilityGetter = getter;
}
var _testabilityGetter = CONST_EXPR(new _NoopGetTestability());