From 8b4924c8ada8e7a49207084ff323b5b45df52cfb Mon Sep 17 00:00:00 2001 From: Heorhi Shakanau Date: Wed, 16 Oct 2019 17:46:27 +0300 Subject: [PATCH] feat(element): add support for dynamic module configuration - support dynamic config in LazyElementsModule.forRoot - support dynamic config in LazyElementsModule.forFeature --- .../lib/lazy-elements/lazy-elements.module.ts | 62 ++++++++++++++++--- .../lib/lazy-elements/lazy-elements.tokens.ts | 14 ++++- 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/projects/elements/src/lib/lazy-elements/lazy-elements.module.ts b/projects/elements/src/lib/lazy-elements/lazy-elements.module.ts index b59d766..2e5213d 100644 --- a/projects/elements/src/lib/lazy-elements/lazy-elements.module.ts +++ b/projects/elements/src/lib/lazy-elements/lazy-elements.module.ts @@ -18,7 +18,9 @@ import { import { LAZY_ELEMENT_ROOT_OPTIONS, LAZY_ELEMENT_CONFIGS, - LAZY_ELEMENT_ROOT_GUARD + LAZY_ELEMENT_ROOT_GUARD, + LAZY_ELEMENT_MODULE_ROOT_OPTIONS, + LAZY_ELEMENT_MODULE_OPTIONS } from './lazy-elements.tokens'; export function createLazyElementRootGuard(options: LazyElementModuleOptions) { @@ -30,6 +32,32 @@ export function createLazyElementRootGuard(options: LazyElementModuleOptions) { return 'guarded'; } +// tslint:disable:unified-signatures +// Switching to unified signatures breaks the typechecking +export function elementConfigsFactory( + options: () => LazyElementModuleOptions | LazyElementModuleRootOptions +): ElementConfig[]; +export function elementConfigsFactory( + options: LazyElementModuleOptions | LazyElementModuleRootOptions +): ElementConfig[]; +export function elementConfigsFactory(options) { + const optionsObject = typeof options === 'function' ? options() : options; + return optionsObject && optionsObject.elementConfigs + ? optionsObject.elementConfigs + : []; +} + +export function rootOptionsFactory( + options: () => LazyElementModuleRootOptions | LazyElementModuleRootOptions +): LazyElementRootOptions; +export function rootOptionsFactory( + options: LazyElementModuleRootOptions | LazyElementModuleRootOptions +): LazyElementRootOptions; +export function rootOptionsFactory(options) { + const optionsObject = typeof options === 'function' ? options() : options; + return (optionsObject && optionsObject.rootOptions) || {}; +} + @NgModule({ declarations: [LazyElementDirective, LazyElementDynamicDirective], imports: [CommonModule], @@ -37,19 +65,28 @@ export function createLazyElementRootGuard(options: LazyElementModuleOptions) { providers: [] }) export class LazyElementsModule { - static forRoot(options: LazyElementModuleRootOptions): ModuleWithProviders { + static forRoot( + options: () => LazyElementModuleRootOptions + ): ModuleWithProviders; + static forRoot(options: LazyElementModuleRootOptions): ModuleWithProviders; + static forRoot(options) { return { ngModule: LazyElementsModule, providers: [ + { + provide: LAZY_ELEMENT_MODULE_ROOT_OPTIONS, + useValue: options + }, { provide: LAZY_ELEMENT_CONFIGS, - useValue: - options && options.elementConfigs ? options.elementConfigs : [], + useFactory: elementConfigsFactory, + deps: [LAZY_ELEMENT_MODULE_ROOT_OPTIONS], multi: true }, { provide: LAZY_ELEMENT_ROOT_OPTIONS, - useValue: options.rootOptions ? options.rootOptions : {} + useFactory: rootOptionsFactory, + deps: [LAZY_ELEMENT_MODULE_ROOT_OPTIONS] }, { provide: ANALYZE_FOR_ENTRY_COMPONENTS, @@ -65,14 +102,22 @@ export class LazyElementsModule { }; } - static forFeature(options: LazyElementModuleOptions): ModuleWithProviders { + static forFeature( + options: () => LazyElementModuleOptions + ): ModuleWithProviders; + static forFeature(options: LazyElementModuleOptions): ModuleWithProviders; + static forFeature(options) { return { ngModule: LazyElementsModule, providers: [ + { + provide: LAZY_ELEMENT_MODULE_OPTIONS, + useValue: options + }, { provide: LAZY_ELEMENT_CONFIGS, - useValue: - options && options.elementConfigs ? options.elementConfigs : [], + useFactory: elementConfigsFactory, + deps: [LAZY_ELEMENT_MODULE_OPTIONS], multi: true }, { @@ -83,6 +128,7 @@ export class LazyElementsModule { ] }; } + // tslint:enable:unified-signatures constructor( lazyElementsLoaderService: LazyElementsLoaderService, diff --git a/projects/elements/src/lib/lazy-elements/lazy-elements.tokens.ts b/projects/elements/src/lib/lazy-elements/lazy-elements.tokens.ts index 8b369e1..3450dbf 100644 --- a/projects/elements/src/lib/lazy-elements/lazy-elements.tokens.ts +++ b/projects/elements/src/lib/lazy-elements/lazy-elements.tokens.ts @@ -1,7 +1,11 @@ import { InjectionToken } from '@angular/core'; import { ElementConfig } from './lazy-elements-loader.service'; -import { LazyElementRootOptions } from './lazy-elements.module'; +import { + LazyElementModuleOptions, + LazyElementModuleRootOptions, + LazyElementRootOptions +} from './lazy-elements.module'; export const LAZY_ELEMENT_CONFIGS = new InjectionToken( 'LAZY_ELEMENT_CONFIGS' @@ -14,3 +18,11 @@ export const LAZY_ELEMENT_ROOT_OPTIONS = new InjectionToken< export const LAZY_ELEMENT_ROOT_GUARD = new InjectionToken( 'LAZY_ELEMENT_ROOT_GUARD' ); + +export const LAZY_ELEMENT_MODULE_OPTIONS = new InjectionToken< + LazyElementModuleOptions +>('LAZY_ELEMENT_MODULE_OPTIONS'); + +export const LAZY_ELEMENT_MODULE_ROOT_OPTIONS = new InjectionToken< + LazyElementModuleRootOptions +>('LAZY_ELEMENT_MODULE_ROOT_OPTIONS');