Skip to content

Commit

Permalink
feat(module): add for root module guard implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
angelfraga committed Sep 22, 2019
1 parent bdb2449 commit 6af8667
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions projects/elements/src/lib/lazy-elements/lazy-elements.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Inject,
ModuleWithProviders,
ANALYZE_FOR_ENTRY_COMPONENTS,
Type
Type,
SkipSelf
} from '@angular/core';
import { CommonModule } from '@angular/common';

Expand All @@ -16,9 +17,19 @@ import {
} from './lazy-elements-loader.service';
import {
LAZY_ELEMENT_ROOT_OPTIONS,
LAZY_ELEMENT_CONFIGS
LAZY_ELEMENT_CONFIGS,
LAZY_ELEMENT_ROOT_GUARD
} from './lazy-elements.tokens';

export function createLazyElementRootGuard(options: LazyElementModuleOptions) {
if (options) {
throw new TypeError(
`LazyElementsModule.forRoot() called twice. Feature modules should use ThemeModule.forFeature() instead.`
);
}
return 'guarded';
}

@NgModule({
declarations: [LazyElementDirective, LazyElementDynamicDirective],
imports: [CommonModule],
Expand All @@ -44,6 +55,11 @@ export class LazyElementsModule {
provide: ANALYZE_FOR_ENTRY_COMPONENTS,
useValue: options,
multi: true
},
{
provide: LAZY_ELEMENT_ROOT_GUARD,
useFactory: createLazyElementRootGuard,
deps: [[LAZY_ELEMENT_CONFIGS, new Optional(), new SkipSelf()]]
}
]
};
Expand Down Expand Up @@ -72,7 +88,10 @@ export class LazyElementsModule {
lazyElementsLoaderService: LazyElementsLoaderService,
@Optional()
@Inject(LAZY_ELEMENT_CONFIGS)
elementConfigsMultiProvider: ElementConfig[][]
elementConfigsMultiProvider: ElementConfig[][],
@Optional()
@Inject(LAZY_ELEMENT_ROOT_GUARD)
guard: any
) {
if (elementConfigsMultiProvider && elementConfigsMultiProvider.length) {
const lastAddedConfigs =
Expand Down

0 comments on commit 6af8667

Please sign in to comment.