Skip to content

Instantly share code, notes, and snippets.

@peplocanto
Last active December 14, 2021 12:53
Show Gist options
  • Save peplocanto/2ecc58ba9c0ed09dd9de035a012fb88b to your computer and use it in GitHub Desktop.
Save peplocanto/2ecc58ba9c0ed09dd9de035a012fb88b to your computer and use it in GitHub Desktop.
How to start services in core module
// core.providers.ts
import { APP_INITIALIZER } from '@angular/core';
export const load<domain>ServiceFnFactory = (<domain>Service: <domain>Service): (() => Promise<any>) => () => {
<domain>Service.init();
return Promise.resolve();
};
export const APP_INITIALIZER_PROVIDERS: Provider[] = [
{
provide: APP_INITIALIZER,
useFactory: load<domain>ServiceFnFactory,
deps: [<domain>Service],
multi: true
}
];
// core.module.ts
import { APP_INITIALIZER_PROVIDERS } from 'app-core/core.providers';
@NgModule({
imports: [...MODULES],
declarations: [...INNER_COMPONENTS, ...OUTER_COMPONENTS],
exports: [...OUTER_COMPONENTS],
providers: [...APP_INITIALIZER_PROVIDERS]
})
export class CoreModule {
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
if (!!parentModule) {
console.error('"CoreModule" already loaded. Import it _ONLY_ on main "AppRootModule".');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment