My APP_INITIALIZER seems to fail the Observable test in the Angular framework code and is never called. I have an appInitFactory, and a service class containing the init logic. The app module correctly configged, and the init method returns the Observable. I've looked at other posts, which led to this example https://stackblitz.com/edit/angular-goyhqg?file=src%2Fapp%2Finit.service.ts but it didn't work for me. My code below. Thanks for any comments
app_module
export function initializeAppFactory(uscfg: UserConfigService): () => Observable<any> {
return () => uscfg.loadUserInfo();
};
...
{ provide: APP_INITIALIZER,
useFactory: () => initializeAppFactory,
multi: true,
deps: [UserConfigService]}
service method
public loadUserInfo(): Observable<any> {
let obs: Observable<any> = this.http.get(this.baseUrl + '/auth-info', httpOptions)
.pipe(tap(value => {
... process, incl more asynch calls; the Obs is not subscribed
}));
return obs;
}
Angular code, iterating over and running the initializers. The return seems to be the initializer function, not the observable. The test in isSubscribable is "not null obj and obj has subscribe function" which fails.
/** @internal */
runInitializers() {
if (this.initialized) {
return;
}
const asyncInitPromises = [];
for (const appInits of this.appInits) {
const initResult = appInits();
if (isPromise(initResult)) {
asyncInitPromises.push(initResult);
} else if (isSubscribable(initResult)) {
const observableAsPromise = new Promise<void>((resolve, reject) => {
initResult.subscribe({complete: resolve, error: reject});
});
asyncInitPromises.push(observableAsPromise);
}
}
/**
* Determine if the argument is a Subscribable
*/
export function isSubscribable<T>(obj: any|Subscribable<T>): obj is Subscribable<T> {
return !!obj && typeof obj.subscribe === 'function';
}
You shouldn't wrap "initializeAppFactory" inside another arrow function.
export function initializeAppFactory(uscfg: UserConfigService): () => Observable<any> {
return () => uscfg.loadUserInfo();
};
...
{ provide: APP_INITIALIZER,
useFactory: initializeAppFactory, // No wrapping in arrow function
multi: true,
deps: [UserConfigService]}