I am new to Angular and currently using Angular6 for development purpose. Here is my query. Before my app starts I need to call three services that provide me some configs for the app. Lets call the services as Initialization, Conifg and UserDetails. So I have added the services in the app-module.ts file as below.
`
export function init_app(initService: InitializationService) {
return () => initService.getInit();
}
export function get_settings(configService: ConfigService) {
return () => configService.getConfig();
}
export function get_user_settings(userDetails: UserDetailsService) {
return () => userDetails.getSettings();
}
@NgModule({
imports: [/*my-imports*/],
providers: [
AppLoadService,
{ provide: APP_INITIALIZER, useFactory: init_app, deps: [InitializationService], multi: true },
{ provide: APP_INITIALIZER, useFactory: get_settings, deps: [ConfigService], multi: true },
{ provide: APP_INITIALIZER, useFactory: get_user_settings, deps: [UserDetailsService], multi: true }
]
})
` Now InitializationService has some values that are needed by the other two services to work properly. In app_init the services request are sent immediately.
I need my ConfigService and UserDetailsService to be called only when the call for InitializationService is done.
To tackle this I have done following,
// AppConfig.ts
export class AppConfig {
public static Config= {};
public static $subject = new Subject();
}
// InitializationService
getInit () {
const promise = new Promise((resolve, reject) => {
this.http.get().then(val => {
AppConfig.config = val;
AppConfig.$subject.next(val);
});
};
return promise;
}
//ConfigService
getConfig () {
if(AppConfig.Config.baseUrl === undefined) {
AppConfig.$subject.subscribe(val => {
this.http.get(AppConfig.Config.baseUrl).then(//doSomething);
})
}
}
So i create a static subject and subscribe to it. Once the Init service is done, it sends the next value for the subject, which is now subscribed and listened by Config service. Once the subscribe call is received I do my further operation.
Same code is repeated by my UserDetails getSettings() call.
Is my approach correct? Is there any other pattern tried and tested.
Any suggestion or better way to tackle the above?
Thanks in advance.
Regards, hawx