Skip to content

Commit

Permalink
(fix): Add for root guard
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 9, 2018
1 parent 4516c3a commit 828e4a9
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/modal-dialog.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { SimpleModalComponent } from './simple-modal.component';
import { ModalDialogInstanceService } from './modal-dialog-instance.service';
// modules
import { CommonModule } from '@angular/common';
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, InjectionToken, SkipSelf, Optional } from '@angular/core';

/**
* Guard to make sure we have single initialization of forRoot
* @type {InjectionToken<ModalDialogModule>}
*/
export const MODAL_DIALOG_FORROOT_GUARD = new InjectionToken<ModalDialogModule>('MODAL_DIALOG_FORROOT_GUARD');

@NgModule({
imports: [CommonModule],
Expand All @@ -19,7 +25,26 @@ export class ModalDialogModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: ModalDialogModule,
providers: [ModalDialogInstanceService]
providers: [
{
provide: MODAL_DIALOG_FORROOT_GUARD,
useFactory: provideForRootGuard,
deps: [[ModalDialogModule, new Optional(), new SkipSelf()]]
},
ModalDialogInstanceService
]
};
}
}

/**
* @param dialogModule
* @returns {string}
*/
export function provideForRootGuard(dialogModule: ModalDialogModule): string {
if (dialogModule) {
throw new Error(
`ModalDialogModule.forRoot() called twice.`);
}
return 'guarded';
}

0 comments on commit 828e4a9

Please sign in to comment.