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
|
|
|
|
*/
|
|
|
|
|
2016-09-18 15:55:08 -07:00
|
|
|
import {isPromise} from '../src/util/lang';
|
2016-08-02 07:38:14 -07:00
|
|
|
|
2018-04-26 14:08:13 -07:00
|
|
|
import {Inject, Injectable, InjectionToken, Optional} from './di';
|
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.
|
|
|
|
*
|
|
|
|
* The provided function are injected at application startup and executed during
|
2020-01-13 13:19:53 -08:00
|
|
|
* app initialization. If any of these functions returns a Promise, initialization
|
|
|
|
* does not complete until the Promise is resolved.
|
|
|
|
*
|
|
|
|
* 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`
|
|
|
|
*
|
2018-10-19 16:27:04 +01:00
|
|
|
* @publicApi
|
2016-08-02 07:38:14 -07:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
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 {
|
2018-06-18 16:38:33 -07:00
|
|
|
// TODO(issue/24571): remove '!'.
|
2020-04-13 16:40:21 -07:00
|
|
|
private resolve!: Function;
|
2018-06-18 16:38:33 -07:00
|
|
|
// TODO(issue/24571): remove '!'.
|
2020-04-13 16:40:21 -07:00
|
|
|
private reject!: Function;
|
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
|
|
|
|
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 = () => {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|