Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(element): add support for dynamic module configuration #29

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions projects/elements/src/lib/lazy-elements/lazy-elements.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,26 +32,61 @@ 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],
exports: [LazyElementDirective, LazyElementDynamicDirective],
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,
Expand All @@ -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
},
{
Expand All @@ -83,6 +128,7 @@ export class LazyElementsModule {
]
};
}
// tslint:enable:unified-signatures

constructor(
lazyElementsLoaderService: LazyElementsLoaderService,
Expand Down
14 changes: 13 additions & 1 deletion projects/elements/src/lib/lazy-elements/lazy-elements.tokens.ts
Original file line number Diff line number Diff line change
@@ -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<ElementConfig[]>(
'LAZY_ELEMENT_CONFIGS'
Expand All @@ -14,3 +18,11 @@ export const LAZY_ELEMENT_ROOT_OPTIONS = new InjectionToken<
export const LAZY_ELEMENT_ROOT_GUARD = new InjectionToken<void>(
'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');