| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 15:55:08 -07:00
										 |  |  | import {isPromise} from '../src/util/lang'; | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 16:54:46 -08:00
										 |  |  | import {Inject, Injectable, InjectionToken, Optional} from './di'; | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * A function that will be executed when an application is initialized. | 
					
						
							|  |  |  |  * @experimental | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-03 16:54:46 -08:00
										 |  |  | export const APP_INITIALIZER = new InjectionToken<Array<() => void>>('Application Initializer'); | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * A class that reflects the state of running {@link APP_INITIALIZER}s. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @experimental | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | @Injectable() | 
					
						
							| 
									
										
										
										
											2016-08-02 07:54:14 -07:00
										 |  |  | export class ApplicationInitStatus { | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  |   private resolve: Function; | 
					
						
							|  |  |  |   private reject: Function; | 
					
						
							|  |  |  |   private initialized = false; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:06:33 -07:00
										 |  |  |   public readonly donePromise: Promise<any>; | 
					
						
							|  |  |  |   public readonly done = false; | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  |   constructor(@Inject(APP_INITIALIZER) @Optional() private appInits: (() => any)[]) { | 
					
						
							| 
									
										
										
										
											2017-09-08 18:06:33 -07:00
										 |  |  |     this.donePromise = new Promise((res, rej) => { | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  |       this.resolve = res; | 
					
						
							|  |  |  |       this.reject = rej; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** @internal */ | 
					
						
							|  |  |  |   runInitializers() { | 
					
						
							|  |  |  |     if (this.initialized) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  |     const asyncInitPromises: Promise<any>[] = []; | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 16:13:16 -07:00
										 |  |  |     const complete = () => { | 
					
						
							| 
									
										
										
										
											2017-09-08 18:06:33 -07:00
										 |  |  |       (this as{done: boolean}).done = true; | 
					
						
							| 
									
										
										
										
											2017-07-27 16:13:16 -07:00
										 |  |  |       this.resolve(); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (this.appInits) { | 
					
						
							|  |  |  |       for (let i = 0; i < this.appInits.length; i++) { | 
					
						
							|  |  |  |         const initResult = this.appInits[i](); | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  |         if (isPromise(initResult)) { | 
					
						
							|  |  |  |           asyncInitPromises.push(initResult); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Promise.all(asyncInitPromises).then(() => { complete(); }).catch(e => { this.reject(e); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  |     if (asyncInitPromises.length === 0) { | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  |       complete(); | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-16 15:14:55 -07:00
										 |  |  |     this.initialized = true; | 
					
						
							| 
									
										
										
										
											2016-08-02 07:38:14 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } |