feat(bootstrap): add platform and app initializers
Often some init logic needs to run when a platform or an application is boostrapped. For example, boostraping a platform requires initializing the dom adapter. Now, it can be done as follows: new Provider(PLATFORM_INITIALIZER, {useValue: initDomAdapter, multi: true}), All platform initializers will be run after the platform injector has been created. Similarly, all application initializers will be run after the app injector has been created. Closes #5355
This commit is contained in:
@ -4,7 +4,9 @@ import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di';
|
||||
import {
|
||||
APP_COMPONENT_REF_PROMISE,
|
||||
APP_COMPONENT,
|
||||
APP_ID_RANDOM_PROVIDER
|
||||
APP_ID_RANDOM_PROVIDER,
|
||||
PLATFORM_INITIALIZER,
|
||||
APP_INITIALIZER
|
||||
} from './application_tokens';
|
||||
import {
|
||||
Promise,
|
||||
@ -30,7 +32,6 @@ import {wtfLeave, wtfCreateScope, WtfScopeFn} from './profile/profile';
|
||||
import {ChangeDetectorRef} from 'angular2/src/core/change_detection/change_detector_ref';
|
||||
import {lockDevMode} from 'angular2/src/facade/lang';
|
||||
|
||||
|
||||
/**
|
||||
* Construct providers specific to an individual root component.
|
||||
*/
|
||||
@ -103,15 +104,32 @@ export function platform(providers?: Array<Type | Provider | any[]>): PlatformRe
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispose the existing platform.
|
||||
*/
|
||||
export function disposePlatform(): void {
|
||||
if (isPresent(_platform)) {
|
||||
_platform.dispose();
|
||||
_platform = null;
|
||||
}
|
||||
}
|
||||
|
||||
function _createPlatform(providers?: Array<Type | Provider | any[]>): PlatformRef {
|
||||
_platformProviders = providers;
|
||||
_platform = new PlatformRef_(Injector.resolveAndCreate(providers), () => {
|
||||
let injector = Injector.resolveAndCreate(providers);
|
||||
_platform = new PlatformRef_(injector, () => {
|
||||
_platform = null;
|
||||
_platformProviders = null;
|
||||
});
|
||||
_runPlatformInitializers(injector);
|
||||
return _platform;
|
||||
}
|
||||
|
||||
function _runPlatformInitializers(injector: Injector): void {
|
||||
let inits: Function[] = injector.getOptional(PLATFORM_INITIALIZER);
|
||||
if (isPresent(inits)) inits.forEach(init => init());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -236,11 +254,12 @@ export class PlatformRef_ extends PlatformRef {
|
||||
});
|
||||
app = new ApplicationRef_(this, zone, injector);
|
||||
this._applications.push(app);
|
||||
_runAppInitializers(injector);
|
||||
return app;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this._applications.forEach((app) => app.dispose());
|
||||
ListWrapper.clone(this._applications).forEach((app) => app.dispose());
|
||||
this._disposeListeners.forEach((dispose) => dispose());
|
||||
this._dispose();
|
||||
}
|
||||
@ -249,6 +268,11 @@ export class PlatformRef_ extends PlatformRef {
|
||||
_applicationDisposed(app: ApplicationRef): void { ListWrapper.remove(this._applications, app); }
|
||||
}
|
||||
|
||||
function _runAppInitializers(injector: Injector): void {
|
||||
let inits: Function[] = injector.getOptional(APP_INITIALIZER);
|
||||
if (isPresent(inits)) inits.forEach(init => init());
|
||||
}
|
||||
|
||||
/**
|
||||
* A reference to an Angular application running on a page.
|
||||
*
|
||||
@ -439,7 +463,7 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
|
||||
dispose(): void {
|
||||
// TODO(alxhub): Dispose of the NgZone.
|
||||
this._rootComponents.forEach((ref) => ref.dispose());
|
||||
ListWrapper.clone(this._rootComponents).forEach((ref) => ref.dispose());
|
||||
this._disposeListeners.forEach((dispose) => dispose());
|
||||
this._platform._applicationDisposed(this);
|
||||
}
|
||||
|
@ -48,3 +48,14 @@ export const APP_ID_RANDOM_PROVIDER: Provider =
|
||||
function _randomChar(): string {
|
||||
return StringWrapper.fromCharCode(97 + Math.floor(Math.random() * 25));
|
||||
}
|
||||
|
||||
/**
|
||||
* A function that will be executed when a platform is initialized.
|
||||
*/
|
||||
export const PLATFORM_INITIALIZER: OpaqueToken =
|
||||
CONST_EXPR(new OpaqueToken("Platform Initializer"));
|
||||
|
||||
/**
|
||||
* A function that will be executed when an application is initialized.
|
||||
*/
|
||||
export const APP_INITIALIZER: OpaqueToken = CONST_EXPR(new OpaqueToken("Application Initializer"));
|
@ -11,13 +11,13 @@ import {
|
||||
reflector,
|
||||
APPLICATION_COMMON_PROVIDERS,
|
||||
PLATFORM_COMMON_PROVIDERS,
|
||||
EVENT_MANAGER_PLUGINS
|
||||
EVENT_MANAGER_PLUGINS,
|
||||
PLATFORM_INITIALIZER
|
||||
} from "angular2/core";
|
||||
import {COMMON_DIRECTIVES, COMMON_PIPES, FORM_PROVIDERS} from "angular2/common";
|
||||
import {Renderer} from 'angular2/render';
|
||||
import {Testability} from 'angular2/src/core/testability/testability';
|
||||
|
||||
// TODO change these imports once dom_adapter is moved out of core
|
||||
import {DOM} from 'angular2/src/core/dom/dom_adapter';
|
||||
import {DomEventsPlugin} from 'angular2/src/platform/dom/events/dom_events';
|
||||
import {KeyEventsPlugin} from 'angular2/src/platform/dom/events/key_events';
|
||||
@ -42,8 +42,10 @@ export {
|
||||
export {By} from 'angular2/src/platform/browser/debug/by';
|
||||
export {BrowserDomAdapter} from './browser/browser_adapter';
|
||||
|
||||
export const BROWSER_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
|
||||
CONST_EXPR([PLATFORM_COMMON_PROVIDERS]);
|
||||
export const BROWSER_PROVIDERS: Array<any /*Type | Provider | any[]*/> = CONST_EXPR([
|
||||
PLATFORM_COMMON_PROVIDERS,
|
||||
new Provider(PLATFORM_INITIALIZER, {useValue: initDomAdapter, multi: true}),
|
||||
]);
|
||||
|
||||
function _exceptionHandler(): ExceptionHandler {
|
||||
return new ExceptionHandler(DOM, false);
|
||||
@ -73,7 +75,6 @@ export const BROWSER_APP_COMMON_PROVIDERS: Array<any /*Type | Provider | any[]*/
|
||||
]);
|
||||
|
||||
export function initDomAdapter() {
|
||||
// TODO: refactor into a generic init function
|
||||
BrowserDomAdapter.makeCurrent();
|
||||
wtfInit();
|
||||
BrowserGetTestability.init();
|
||||
|
Reference in New Issue
Block a user