Skip to content

Commit

Permalink
feat(core): support APP_INITIALIZER work with observable
Browse files Browse the repository at this point in the history
add support to observable with APP_INITIALIZER

close angular#15088

Co-authored-by: Andrew Kushnir <43554145+AndrewKushnir@users.noreply.github.com>

Co-authored-by: Andrew Kushnir <43554145+AndrewKushnir@users.noreply.github.com>
  • Loading branch information
vthinkxie and AndrewKushnir committed Feb 10, 2021
1 parent d067dc0 commit 18636fb
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 10 deletions.
11 changes: 8 additions & 3 deletions packages/core/src/application_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {Inject, Injectable, InjectionToken, Optional} from './di';
import {isPromise} from './util/lang';
import {isObservable, isPromise} from './util/lang';
import {noop} from './util/noop';


Expand All @@ -16,8 +16,8 @@ import {noop} from './util/noop';
* one or more initialization functions.
*
* The provided functions are injected at application startup and executed during
* app initialization. If any of these functions returns a Promise, initialization
* does not complete until the Promise is resolved.
* 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.
*
* 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.
Expand Down Expand Up @@ -68,6 +68,11 @@ export class ApplicationInitStatus {
const initResult = this.appInits[i]();
if (isPromise(initResult)) {
asyncInitPromises.push(initResult);
} else if (isObservable(initResult)) {
// Note: `.toPromise()` will be deprecated in RxJS v7 and removed completely in later
// versions. Once RxJS is updated in the repo to v7, this code should be refactored and
// the `lastValueFrom` function should be used instead.
asyncInitPromises.push(initResult.toPromise());
}
}
}
Expand Down
97 changes: 90 additions & 7 deletions packages/core/test/application_init_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import {Injector} from '@angular/core';
import {APP_INITIALIZER, ApplicationInitStatus} from '@angular/core/src/application_init';
import {Observable, Subscriber} from 'rxjs';

import {inject, TestBed, waitForAsync} from '../testing';

Expand All @@ -28,21 +29,23 @@ import {inject, TestBed, waitForAsync} from '../testing';
})));
});

describe('with async initializers', () => {
describe('with async promise initializers', () => {
let resolve: (result: any) => void;
let reject: (reason?: any) => void;
let promise: Promise<any>;
let completerResolver = false;
let initFnInvoked = false;
beforeEach(() => {
let initializerFactory = (injector: Injector) => {
return () => {
const initStatus = injector.get(ApplicationInitStatus);
initStatus.donePromise.then(() => {
expect(completerResolver).toBe(true);
expect(initFnInvoked).toBe(true);
});
};
};
promise = new Promise((res) => {
promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
TestBed.configureTestingModule({
providers: [
Expand All @@ -57,21 +60,101 @@ import {inject, TestBed, waitForAsync} from '../testing';
});
});

it('should update the status once all async initializers are done',
it('should update the status once all async promise initializers are done',
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
// instance for testing purposes to invoke initializer functions.
(status as any).runInitializers();

setTimeout(() => {
completerResolver = true;
initFnInvoked = true;
resolve(null);
});

expect(status.done).toBe(false);
status.donePromise.then(() => {
expect(status.done).toBe(true);
expect(completerResolver).toBe(true);
expect(initFnInvoked).toBe(true);
});
})));

it('should handle a case when promise is rejected',
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
// instance for testing purposes to invoke initializer functions.
(status as any).runInitializers();

setTimeout(() => {
initFnInvoked = true;
reject();
});

expect(status.done).toBe(false);
status.donePromise
.then(
() => fail('`donePromise.then` should not be invoked when promise is rejected'))
.catch(() => {
expect(status.done).toBe(false);
expect(initFnInvoked).toBe(true);
});
})));
});

describe('with app initializers represented using observables', () => {
let subscriber: Subscriber<any>;
let observable: Observable<any>;
let initFnInvoked = false;
beforeEach(() => {
observable = new Observable((res) => {
subscriber = res;
});
TestBed.configureTestingModule({
providers: [
{provide: APP_INITIALIZER, multi: true, useValue: () => observable},
]
});
});

it('should update the status once all async observable initializers are done',
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
// instance for testing purposes to invoke initializer functions.
(status as any).runInitializers();

setTimeout(() => {
initFnInvoked = true;
subscriber.next();
subscriber.complete();
});

expect(status.done).toBe(false);
status.donePromise.then(() => {
expect(status.done).toBe(true);
expect(initFnInvoked).toBe(true);
});
})));

it('should handle a case when observable emits an error',
waitForAsync(inject([ApplicationInitStatus], (status: ApplicationInitStatus) => {
// Accessing internal `runInitializers` function of the `ApplicationInitStatus` class
// instance for testing purposes to invoke initializer functions.
(status as any).runInitializers();

setTimeout(() => {
initFnInvoked = true;
subscriber.error();
});

expect(status.done).toBe(false);
status.donePromise
.then(
() => fail(
'`donePromise.then` should not be invoked when observable emits an error'))
.catch(() => {
expect(status.done).toBe(false);
expect(initFnInvoked).toBe(true);
});
})));
});
});
}
3 changes: 3 additions & 0 deletions packages/core/test/bundling/forms/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,9 @@
{
"name": "isObject"
},
{
"name": "isObservable"
},
{
"name": "isOptionsObj"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,9 @@
{
"name": "isObject"
},
{
"name": "isObservable"
},
{
"name": "isPositive"
},
Expand Down

0 comments on commit 18636fb

Please sign in to comment.