2016-08-02 14:38:14 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-08-02 14:38:14 +00: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 19:29:09 +00:00
|
|
|
import {Observable} from 'rxjs';
|
2021-07-09 01:04:30 +00:00
|
|
|
|
2023-02-26 09:53:59 +00:00
|
|
|
import {inject, Injectable, InjectionToken} from './di';
|
2023-04-15 14:02:38 +00:00
|
|
|
import {RuntimeError, RuntimeErrorCode} from './errors';
|
2023-03-02 18:30:31 +00:00
|
|
|
import {isPromise, isSubscribable} from './util/lang';
|
2016-08-02 14:38:14 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-05-11 21:57:23 +00: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 11:04:47 +00:00
|
|
|
* The provided functions are injected at application startup and executed during
|
2019-10-17 09:48:35 +00: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 21:19:53 +00: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 21:57:23 +00:00
|
|
|
* The function is executed during the application bootstrap process,
|
2020-01-13 21:19:53 +00:00
|
|
|
* and the needed data is available on startup.
|
2018-10-19 15:27:04 +00:00
|
|
|
*
|
2023-05-02 19:14:30 +00:00
|
|
|
* @see {@link ApplicationInitStatus}
|
2020-05-11 21:57:23 +00:00
|
|
|
*
|
2021-03-05 22:03:16 +00: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.
|
|
|
|
|
*
|
|
|
|
|
* ```
|
2021-07-02 08:13:03 +00:00
|
|
|
* function initializeAppFactory(httpClient: HttpClient): () => Observable<any> {
|
|
|
|
|
* return () => httpClient.get("https://someUrl.com/api/user")
|
2021-03-05 22:03:16 +00:00
|
|
|
* .pipe(
|
|
|
|
|
* tap(user => { ... })
|
2021-07-02 08:13:03 +00:00
|
|
|
* );
|
2021-03-05 22:03:16 +00:00
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @NgModule({
|
|
|
|
|
* imports: [BrowserModule, HttpClientModule],
|
|
|
|
|
* declarations: [AppComponent],
|
|
|
|
|
* bootstrap: [AppComponent],
|
|
|
|
|
* providers: [{
|
|
|
|
|
* provide: APP_INITIALIZER,
|
2021-07-02 08:13:03 +00:00
|
|
|
* useFactory: initializeAppFactory,
|
2021-03-05 22:03:16 +00:00
|
|
|
* deps: [HttpClient],
|
|
|
|
|
* multi: true
|
|
|
|
|
* }]
|
|
|
|
|
* })
|
|
|
|
|
* export class AppModule {}
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2018-10-19 15:27:04 +00:00
|
|
|
* @publicApi
|
2016-08-02 14:38:14 +00:00
|
|
|
*/
|
2021-02-24 19:29:09 +00:00
|
|
|
export const APP_INITIALIZER =
|
|
|
|
|
new InjectionToken<ReadonlyArray<() => Observable<unknown>| Promise<unknown>| void>>(
|
|
|
|
|
'Application Initializer');
|
2016-08-02 14:38:14 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-05-11 21:57:23 +00:00
|
|
|
* A class that reflects the state of running {@link APP_INITIALIZER} functions.
|
2018-10-19 15:27:04 +00:00
|
|
|
*
|
|
|
|
|
* @publicApi
|
2016-08-02 14:38:14 +00:00
|
|
|
*/
|
2022-02-16 00:32:30 +00:00
|
|
|
@Injectable({providedIn: 'root'})
|
2016-08-02 14:54:14 +00:00
|
|
|
export class ApplicationInitStatus {
|
2023-02-26 09:53:59 +00:00
|
|
|
// Using non null assertion, these fields are defined below
|
|
|
|
|
// within the `new Promise` callback (synchronously).
|
|
|
|
|
private resolve!: (...args: any[]) => void;
|
|
|
|
|
private reject!: (...args: any[]) => void;
|
|
|
|
|
|
2017-05-16 22:14:55 +00:00
|
|
|
private initialized = false;
|
2017-09-09 01:06:33 +00:00
|
|
|
public readonly done = false;
|
2023-02-26 09:53:59 +00:00
|
|
|
public readonly donePromise: Promise<any> = new Promise((res, rej) => {
|
|
|
|
|
this.resolve = res;
|
|
|
|
|
this.reject = rej;
|
|
|
|
|
});
|
2016-08-02 14:38:14 +00:00
|
|
|
|
2023-02-26 09:53:59 +00:00
|
|
|
private readonly appInits = inject(APP_INITIALIZER, {optional: true}) ?? [];
|
2017-05-16 22:14:55 +00:00
|
|
|
|
2023-04-15 14:02:38 +00:00
|
|
|
constructor() {
|
|
|
|
|
if ((typeof ngDevMode === 'undefined' || ngDevMode) && !Array.isArray(this.appInits)) {
|
|
|
|
|
throw new RuntimeError(
|
|
|
|
|
RuntimeErrorCode.INVALID_MULTI_PROVIDER,
|
|
|
|
|
'Unexpected type of the `APP_INITIALIZER` token value ' +
|
|
|
|
|
`(expected an array, but got ${typeof this.appInits}). ` +
|
|
|
|
|
'Please check that the `APP_INITIALIZER` token is configured as a ' +
|
|
|
|
|
'`multi: true` provider.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 22:14:55 +00:00
|
|
|
/** @internal */
|
|
|
|
|
runInitializers() {
|
|
|
|
|
if (this.initialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 09:53:59 +00:00
|
|
|
const asyncInitPromises = [];
|
|
|
|
|
for (const appInits of this.appInits) {
|
|
|
|
|
const initResult = appInits();
|
|
|
|
|
if (isPromise(initResult)) {
|
|
|
|
|
asyncInitPromises.push(initResult);
|
2023-03-02 18:30:31 +00:00
|
|
|
} else if (isSubscribable(initResult)) {
|
2023-02-26 09:53:59 +00:00
|
|
|
const observableAsPromise = new Promise<void>((resolve, reject) => {
|
|
|
|
|
initResult.subscribe({complete: resolve, error: reject});
|
|
|
|
|
});
|
|
|
|
|
asyncInitPromises.push(observableAsPromise);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 22:14:55 +00:00
|
|
|
|
2017-07-27 23:13:16 +00:00
|
|
|
const complete = () => {
|
2023-02-26 09:53:59 +00:00
|
|
|
// @ts-expect-error overwriting a readonly
|
|
|
|
|
this.done = true;
|
2017-07-27 23:13:16 +00:00
|
|
|
this.resolve();
|
|
|
|
|
};
|
2017-05-16 22:14:55 +00:00
|
|
|
|
2020-04-13 23:40:21 +00:00
|
|
|
Promise.all(asyncInitPromises)
|
|
|
|
|
.then(() => {
|
|
|
|
|
complete();
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
this.reject(e);
|
|
|
|
|
});
|
2017-05-16 22:14:55 +00:00
|
|
|
|
2016-08-02 14:38:14 +00:00
|
|
|
if (asyncInitPromises.length === 0) {
|
2017-05-16 22:14:55 +00:00
|
|
|
complete();
|
2016-08-02 14:38:14 +00:00
|
|
|
}
|
2017-05-16 22:14:55 +00:00
|
|
|
this.initialized = true;
|
2016-08-02 14:38:14 +00:00
|
|
|
}
|
|
|
|
|
}
|