/node_modules/@angular/core/esm/src/application_ref.js
JavaScript | 351 lines | 266 code | 0 blank | 85 comment | 24 complexity | bcbb40ff44a8d43c91f8b22cd3fb3990 MD5 | raw file
- import { ObservableWrapper, PromiseWrapper } from '../src/facade/async';
- import { ListWrapper } from '../src/facade/collection';
- import { BaseException, ExceptionHandler, unimplemented } from '../src/facade/exceptions';
- import { IS_DART, assertionsEnabled, isBlank, isPresent, isPromise, lockMode } from '../src/facade/lang';
- import { APP_INITIALIZER, PLATFORM_INITIALIZER } from './application_tokens';
- import { Console } from './console';
- import { Injectable, Injector } from './di';
- import { ComponentResolver } from './linker/component_resolver';
- import { wtfCreateScope, wtfLeave } from './profile/profile';
- import { Testability, TestabilityRegistry } from './testability/testability';
- import { NgZone } from './zone/ng_zone';
- /**
- * Create an Angular zone.
- * @experimental
- */
- export function createNgZone() {
- return new NgZone({ enableLongStackTrace: assertionsEnabled() });
- }
- var _platform;
- var _inPlatformCreate = false;
- /**
- * Creates a platform.
- * Platforms have to be eagerly created via this function.
- * @experimental
- */
- export function createPlatform(injector) {
- if (_inPlatformCreate) {
- throw new BaseException('Already creating a platform...');
- }
- if (isPresent(_platform) && !_platform.disposed) {
- throw new BaseException('There can be only one platform. Destroy the previous one to create a new one.');
- }
- lockMode();
- _inPlatformCreate = true;
- try {
- _platform = injector.get(PlatformRef);
- }
- finally {
- _inPlatformCreate = false;
- }
- return _platform;
- }
- /**
- * Checks that there currently is a platform
- * which contains the given token as a provider.
- * @experimental
- */
- export function assertPlatform(requiredToken) {
- var platform = getPlatform();
- if (isBlank(platform)) {
- throw new BaseException('No platform exists!');
- }
- if (isPresent(platform) && isBlank(platform.injector.get(requiredToken, null))) {
- throw new BaseException('A platform with a different configuration has been created. Please destroy it first.');
- }
- return platform;
- }
- /**
- * Dispose the existing platform.
- * @experimental
- */
- export function disposePlatform() {
- if (isPresent(_platform) && !_platform.disposed) {
- _platform.dispose();
- }
- }
- /**
- * Returns the current platform.
- * @experimental
- */
- export function getPlatform() {
- return isPresent(_platform) && !_platform.disposed ? _platform : null;
- }
- /**
- * Shortcut for ApplicationRef.bootstrap.
- * Requires a platform to be created first.
- * @experimental
- */
- export function coreBootstrap(componentFactory, injector) {
- var appRef = injector.get(ApplicationRef);
- return appRef.bootstrap(componentFactory);
- }
- /**
- * Resolves the componentFactory for the given component,
- * waits for asynchronous initializers and bootstraps the component.
- * Requires a platform to be created first.
- * @experimental
- */
- export function coreLoadAndBootstrap(componentType, injector) {
- var appRef = injector.get(ApplicationRef);
- return appRef.run(() => {
- var componentResolver = injector.get(ComponentResolver);
- return PromiseWrapper
- .all([componentResolver.resolveComponent(componentType), appRef.waitForAsyncInitializers()])
- .then((arr) => appRef.bootstrap(arr[0]));
- });
- }
- /**
- * The Angular platform is the entry point for Angular on a web page. Each page
- * has exactly one platform, and services (such as reflection) which are common
- * to every Angular application running on the page are bound in its scope.
- *
- * A page's platform is initialized implicitly when {@link bootstrap}() is called, or
- * explicitly by calling {@link createPlatform}().
- * @stable
- */
- export class PlatformRef {
- /**
- * Retrieve the platform {@link Injector}, which is the parent injector for
- * every Angular application on the page and provides singleton providers.
- */
- get injector() { throw unimplemented(); }
- ;
- get disposed() { throw unimplemented(); }
- }
- export class PlatformRef_ extends PlatformRef {
- constructor(_injector) {
- super();
- this._injector = _injector;
- /** @internal */
- this._applications = [];
- /** @internal */
- this._disposeListeners = [];
- this._disposed = false;
- if (!_inPlatformCreate) {
- throw new BaseException('Platforms have to be created via `createPlatform`!');
- }
- let inits = _injector.get(PLATFORM_INITIALIZER, null);
- if (isPresent(inits))
- inits.forEach(init => init());
- }
- registerDisposeListener(dispose) { this._disposeListeners.push(dispose); }
- get injector() { return this._injector; }
- get disposed() { return this._disposed; }
- addApplication(appRef) { this._applications.push(appRef); }
- dispose() {
- ListWrapper.clone(this._applications).forEach((app) => app.dispose());
- this._disposeListeners.forEach((dispose) => dispose());
- this._disposed = true;
- }
- /** @internal */
- _applicationDisposed(app) { ListWrapper.remove(this._applications, app); }
- }
- /** @nocollapse */
- PlatformRef_.decorators = [
- { type: Injectable },
- ];
- /** @nocollapse */
- PlatformRef_.ctorParameters = [
- { type: Injector, },
- ];
- /**
- * A reference to an Angular application running on a page.
- *
- * For more about Angular applications, see the documentation for {@link bootstrap}.
- * @stable
- */
- export class ApplicationRef {
- /**
- * Retrieve the application {@link Injector}.
- */
- get injector() { return unimplemented(); }
- ;
- /**
- * Retrieve the application {@link NgZone}.
- */
- get zone() { return unimplemented(); }
- ;
- /**
- * Get a list of component types registered to this application.
- */
- get componentTypes() { return unimplemented(); }
- ;
- }
- export class ApplicationRef_ extends ApplicationRef {
- constructor(_platform, _zone, _injector) {
- super();
- this._platform = _platform;
- this._zone = _zone;
- this._injector = _injector;
- /** @internal */
- this._bootstrapListeners = [];
- /** @internal */
- this._disposeListeners = [];
- /** @internal */
- this._rootComponents = [];
- /** @internal */
- this._rootComponentTypes = [];
- /** @internal */
- this._changeDetectorRefs = [];
- /** @internal */
- this._runningTick = false;
- /** @internal */
- this._enforceNoNewChanges = false;
- var zone = _injector.get(NgZone);
- this._enforceNoNewChanges = assertionsEnabled();
- zone.run(() => { this._exceptionHandler = _injector.get(ExceptionHandler); });
- this._asyncInitDonePromise = this.run(() => {
- let inits = _injector.get(APP_INITIALIZER, null);
- var asyncInitResults = [];
- var asyncInitDonePromise;
- if (isPresent(inits)) {
- for (var i = 0; i < inits.length; i++) {
- var initResult = inits[i]();
- if (isPromise(initResult)) {
- asyncInitResults.push(initResult);
- }
- }
- }
- if (asyncInitResults.length > 0) {
- asyncInitDonePromise =
- PromiseWrapper.all(asyncInitResults).then((_) => this._asyncInitDone = true);
- this._asyncInitDone = false;
- }
- else {
- this._asyncInitDone = true;
- asyncInitDonePromise = PromiseWrapper.resolve(true);
- }
- return asyncInitDonePromise;
- });
- ObservableWrapper.subscribe(zone.onError, (error) => {
- this._exceptionHandler.call(error.error, error.stackTrace);
- });
- ObservableWrapper.subscribe(this._zone.onMicrotaskEmpty, (_) => { this._zone.run(() => { this.tick(); }); });
- }
- registerBootstrapListener(listener) {
- this._bootstrapListeners.push(listener);
- }
- registerDisposeListener(dispose) { this._disposeListeners.push(dispose); }
- registerChangeDetector(changeDetector) {
- this._changeDetectorRefs.push(changeDetector);
- }
- unregisterChangeDetector(changeDetector) {
- ListWrapper.remove(this._changeDetectorRefs, changeDetector);
- }
- waitForAsyncInitializers() { return this._asyncInitDonePromise; }
- run(callback) {
- var zone = this.injector.get(NgZone);
- var result;
- // Note: Don't use zone.runGuarded as we want to know about
- // the thrown exception!
- // Note: the completer needs to be created outside
- // of `zone.run` as Dart swallows rejected promises
- // via the onError callback of the promise.
- var completer = PromiseWrapper.completer();
- zone.run(() => {
- try {
- result = callback();
- if (isPromise(result)) {
- PromiseWrapper.then(result, (ref) => { completer.resolve(ref); }, (err, stackTrace) => {
- completer.reject(err, stackTrace);
- this._exceptionHandler.call(err, stackTrace);
- });
- }
- }
- catch (e) {
- this._exceptionHandler.call(e, e.stack);
- throw e;
- }
- });
- return isPromise(result) ? completer.promise : result;
- }
- bootstrap(componentFactory) {
- if (!this._asyncInitDone) {
- throw new BaseException('Cannot bootstrap as there are still asynchronous initializers running. Wait for them using waitForAsyncInitializers().');
- }
- return this.run(() => {
- this._rootComponentTypes.push(componentFactory.componentType);
- var compRef = componentFactory.create(this._injector, [], componentFactory.selector);
- compRef.onDestroy(() => { this._unloadComponent(compRef); });
- var testability = compRef.injector.get(Testability, null);
- if (isPresent(testability)) {
- compRef.injector.get(TestabilityRegistry)
- .registerApplication(compRef.location.nativeElement, testability);
- }
- this._loadComponent(compRef);
- let c = this._injector.get(Console);
- if (assertionsEnabled()) {
- let prodDescription = IS_DART ? 'Production mode is disabled in Dart.' :
- 'Call enableProdMode() to enable the production mode.';
- c.log(`Angular 2 is running in the development mode. ${prodDescription}`);
- }
- return compRef;
- });
- }
- /** @internal */
- _loadComponent(componentRef) {
- this._changeDetectorRefs.push(componentRef.changeDetectorRef);
- this.tick();
- this._rootComponents.push(componentRef);
- this._bootstrapListeners.forEach((listener) => listener(componentRef));
- }
- /** @internal */
- _unloadComponent(componentRef) {
- if (!ListWrapper.contains(this._rootComponents, componentRef)) {
- return;
- }
- this.unregisterChangeDetector(componentRef.changeDetectorRef);
- ListWrapper.remove(this._rootComponents, componentRef);
- }
- get injector() { return this._injector; }
- get zone() { return this._zone; }
- tick() {
- if (this._runningTick) {
- throw new BaseException('ApplicationRef.tick is called recursively');
- }
- var s = ApplicationRef_._tickScope();
- try {
- this._runningTick = true;
- this._changeDetectorRefs.forEach((detector) => detector.detectChanges());
- if (this._enforceNoNewChanges) {
- this._changeDetectorRefs.forEach((detector) => detector.checkNoChanges());
- }
- }
- finally {
- this._runningTick = false;
- wtfLeave(s);
- }
- }
- dispose() {
- // TODO(alxhub): Dispose of the NgZone.
- ListWrapper.clone(this._rootComponents).forEach((ref) => ref.destroy());
- this._disposeListeners.forEach((dispose) => dispose());
- this._platform._applicationDisposed(this);
- }
- get componentTypes() { return this._rootComponentTypes; }
- }
- /** @internal */
- ApplicationRef_._tickScope = wtfCreateScope('ApplicationRef#tick()');
- /** @nocollapse */
- ApplicationRef_.decorators = [
- { type: Injectable },
- ];
- /** @nocollapse */
- ApplicationRef_.ctorParameters = [
- { type: PlatformRef_, },
- { type: NgZone, },
- { type: Injector, },
- ];
- export const PLATFORM_CORE_PROVIDERS =
- /*@ts2dart_const*/ [
- PlatformRef_,
- /*@ts2dart_const*/ (
- /* @ts2dart_Provider */ { provide: PlatformRef, useExisting: PlatformRef_ })
- ];
- export const APPLICATION_CORE_PROVIDERS = [
- /* @ts2dart_Provider */ { provide: NgZone, useFactory: createNgZone, deps: [] },
- ApplicationRef_,
- /* @ts2dart_Provider */ { provide: ApplicationRef, useExisting: ApplicationRef_ },
- ];
- //# sourceMappingURL=application_ref.js.map