@@ -4,7 +4,9 @@ import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di';
44import {
55 APP_COMPONENT_REF_PROMISE ,
66 APP_COMPONENT ,
7- APP_ID_RANDOM_PROVIDER
7+ APP_ID_RANDOM_PROVIDER ,
8+ PLATFORM_INITIALIZER ,
9+ APP_INITIALIZER
810} from './application_tokens' ;
911import {
1012 Promise ,
@@ -30,7 +32,6 @@ import {wtfLeave, wtfCreateScope, WtfScopeFn} from './profile/profile';
3032import { ChangeDetectorRef } from 'angular2/src/core/change_detection/change_detector_ref' ;
3133import { lockDevMode } from 'angular2/src/facade/lang' ;
3234
33-
3435/**
3536 * Construct providers specific to an individual root component.
3637 */
@@ -103,15 +104,32 @@ export function platform(providers?: Array<Type | Provider | any[]>): PlatformRe
103104 }
104105}
105106
107+ /**
108+ * Dispose the existing platform.
109+ */
110+ export function disposePlatform ( ) : void {
111+ if ( isPresent ( _platform ) ) {
112+ _platform . dispose ( ) ;
113+ _platform = null ;
114+ }
115+ }
116+
106117function _createPlatform ( providers ?: Array < Type | Provider | any [ ] > ) : PlatformRef {
107118 _platformProviders = providers ;
108- _platform = new PlatformRef_ ( Injector . resolveAndCreate ( providers ) , ( ) => {
119+ let injector = Injector . resolveAndCreate ( providers ) ;
120+ _platform = new PlatformRef_ ( injector , ( ) => {
109121 _platform = null ;
110122 _platformProviders = null ;
111123 } ) ;
124+ _runPlatformInitializers ( injector ) ;
112125 return _platform ;
113126}
114127
128+ function _runPlatformInitializers ( injector : Injector ) : void {
129+ let inits : Function [ ] = injector . getOptional ( PLATFORM_INITIALIZER ) ;
130+ if ( isPresent ( inits ) ) inits . forEach ( init => init ( ) ) ;
131+ }
132+
115133/**
116134 * The Angular platform is the entry point for Angular on a web page. Each page
117135 * has exactly one platform, and services (such as reflection) which are common
@@ -236,11 +254,12 @@ export class PlatformRef_ extends PlatformRef {
236254 } ) ;
237255 app = new ApplicationRef_ ( this , zone , injector ) ;
238256 this . _applications . push ( app ) ;
257+ _runAppInitializers ( injector ) ;
239258 return app ;
240259 }
241260
242261 dispose ( ) : void {
243- this . _applications . forEach ( ( app ) => app . dispose ( ) ) ;
262+ ListWrapper . clone ( this . _applications ) . forEach ( ( app ) => app . dispose ( ) ) ;
244263 this . _disposeListeners . forEach ( ( dispose ) => dispose ( ) ) ;
245264 this . _dispose ( ) ;
246265 }
@@ -249,6 +268,11 @@ export class PlatformRef_ extends PlatformRef {
249268 _applicationDisposed ( app : ApplicationRef ) : void { ListWrapper . remove ( this . _applications , app ) ; }
250269}
251270
271+ function _runAppInitializers ( injector : Injector ) : void {
272+ let inits : Function [ ] = injector . getOptional ( APP_INITIALIZER ) ;
273+ if ( isPresent ( inits ) ) inits . forEach ( init => init ( ) ) ;
274+ }
275+
252276/**
253277 * A reference to an Angular application running on a page.
254278 *
@@ -439,7 +463,7 @@ export class ApplicationRef_ extends ApplicationRef {
439463
440464 dispose ( ) : void {
441465 // TODO(alxhub): Dispose of the NgZone.
442- this . _rootComponents . forEach ( ( ref ) => ref . dispose ( ) ) ;
466+ ListWrapper . clone ( this . _rootComponents ) . forEach ( ( ref ) => ref . dispose ( ) ) ;
443467 this . _disposeListeners . forEach ( ( dispose ) => dispose ( ) ) ;
444468 this . _platform . _applicationDisposed ( this ) ;
445469 }
0 commit comments