2016-08-02 07:38:14 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-08-02 07:38:14 -07:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-24 20:29:09 +01:00
|
|
|
import {Observable} from 'rxjs';
|
2018-04-26 14:08:13 -07:00
|
|
|
import {Inject, Injectable, InjectionToken, Optional} from './di';
|
2019-10-17 17:48:35 +08:00
|
|
|
import {isObservable, isPromise} from './util/lang';
|
2019-12-07 14:20:26 +03:00
|
|
|
import {noop} from './util/noop';
|
2016-08-02 07:38:14 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-11 14:57:23 -07:00
|
|
|
* A [DI token](guide/glossary#di-token "DI token definition") that you can use to provide
|
|
|
|
|
* one or more initialization functions.
|
|
|
|
|
*
|
2020-04-12 14:04:47 +03:00
|
|
|
* The provided functions are injected at application startup and executed during
|
2019-10-17 17:48:35 +08:00
|
|
|
* app initialization. If any of these functions returns a Promise or an Observable, initialization
|
|
|
|
|
* does not complete until the Promise is resolved or the Observable is completed.
|
2020-01-13 13:19:53 -08:00
|
|
|
*
|
|
|
|
|
* You can, for example, create a factory function that loads language data
|
|
|
|
|
* or an external configuration, and provide that function to the `APP_INITIALIZER` token.
|
2020-05-11 14:57:23 -07:00
|
|
|
* The function is executed during the application bootstrap process,
|
2020-01-13 13:19:53 -08:00
|
|
|
* and the needed data is available on startup.
|
2018-10-19 16:27:04 +01:00
|
|
|
*
|
2020-05-11 14:57:23 -07:00
|
|
|
* @see `ApplicationInitStatus`
|
|
|
|
|
*
|
2021-03-05 23:03:16 +01:00
|
|
|
* @usageNotes
|
|
|
|
|
*
|
|
|
|
|
* The following example illustrates how to configure a multi-provider using `APP_INITIALIZER` token
|
|
|
|
|
* and a function returning a promise.
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* function initializeApp(): Promise<any> {
|
|
|
|
|
* return new Promise((resolve, reject) => {
|
|
|
|
|
* // Do some asynchronous stuff
|
|
|
|
|
* resolve();
|
|
|
|
|
* });
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @NgModule({
|
|
|
|
|
* imports: [BrowserModule],
|
|
|
|
|
* declarations: [AppComponent],
|
|
|
|
|
* bootstrap: [AppComponent],
|
|
|
|
|
* providers: [{
|
|
|
|
|
* provide: APP_INITIALIZER,
|
|
|
|
|
* useFactory: () => initializeApp,
|
|
|
|
|
* multi: true
|
|
|
|
|
* }]
|
|
|
|
|
* })
|
|
|
|
|
* export class AppModule {}
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* It's also possible to configure a multi-provider using `APP_INITIALIZER` token and a function
|
|
|
|
|
* returning an observable, see an example below. Note: the `HttpClient` in this example is used for
|
|
|
|
|
* demo purposes to illustrate how the factory function can work with other providers available
|
|
|
|
|
* through DI.
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* function initializeApp(httpClient: HttpClient): Observable<any> {
|
|
|
|
|
* return httpClient.get("https://someUrl.com/api/user")
|
|
|
|
|
* .pipe(
|
|
|
|
|
* tap(user => { ... })
|
|
|
|
|
* )
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @NgModule({
|
|
|
|
|
* imports: [BrowserModule, HttpClientModule],
|
|
|
|
|
* declarations: [AppComponent],
|
|
|
|
|
* bootstrap: [AppComponent],
|
|
|
|
|
* providers: [{
|
|
|
|
|
* provide: APP_INITIALIZER,
|
|
|
|
|
* useFactory: initializeApp,
|
|
|
|
|
* deps: [HttpClient],
|
|
|
|
|
* multi: true
|
|
|
|
|
* }]
|
|
|
|
|
* })
|
|
|
|
|
* export class AppModule {}
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2018-10-19 16:27:04 +01:00
|
|
|
* @publicApi
|
2016-08-02 07:38:14 -07:00
|
|
|
*/
|
2021-02-24 20:29:09 +01:00
|
|
|
export const APP_INITIALIZER =
|
|
|
|
|
new InjectionToken<ReadonlyArray<() => Observable<unknown>| Promise<unknown>| void>>(
|
|
|
|
|
'Application Initializer');
|
2016-08-02 07:38:14 -07:00
|
|
|
|
|
|
|
|
/**
|
2020-05-11 14:57:23 -07:00
|
|
|
* A class that reflects the state of running {@link APP_INITIALIZER} functions.
|
2018-10-19 16:27:04 +01:00
|
|
|
*
|
|
|
|
|
* @publicApi
|
2016-08-02 07:38:14 -07:00
|
|
|
*/
|
2018-04-26 14:08:13 -07:00
|
|
|
@Injectable()
|
2016-08-02 07:54:14 -07:00
|
|
|
export class ApplicationInitStatus {
|
2019-12-07 14:20:26 +03:00
|
|
|
private resolve = noop;
|
|
|
|
|
private reject = noop;
|
2017-05-16 15:14:55 -07:00
|
|
|
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
|
|
|
|
2021-02-24 20:29:09 +01:00
|
|
|
constructor(@Inject(APP_INITIALIZER) @Optional() private readonly appInits:
|
|
|
|
|
ReadonlyArray<() => Observable<unknown>| Promise<unknown>| void>) {
|
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 = () => {
|
2020-04-13 16:40:21 -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);
|
2019-10-17 17:48:35 +08:00
|
|
|
} else if (isObservable(initResult)) {
|
|
|
|
|
const observableAsPromise = new Promise<void>((resolve, reject) => {
|
|
|
|
|
initResult.subscribe({complete: resolve, error: reject});
|
|
|
|
|
});
|
|
|
|
|
asyncInitPromises.push(observableAsPromise);
|
2016-08-02 07:38:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 15:14:55 -07:00
|
|
|
|
2020-04-13 16:40:21 -07:00
|
|
|
Promise.all(asyncInitPromises)
|
|
|
|
|
.then(() => {
|
|
|
|
|
complete();
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
this.reject(e);
|
|
|
|
|
});
|
2017-05-16 15:14:55 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|