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: add forRoot() guard #67

Merged
merged 1 commit into from
Sep 16, 2019
Merged
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
19 changes: 17 additions & 2 deletions src/ngx-upload.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDevMode, ModuleWithProviders, NgModule } from '@angular/core';
import { InjectionToken, isDevMode, ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
import { NgxDragAndDropDirective } from './directives/dropzone.directive';
import { NgxThumbnailDirective } from './directives/thumbnail.directive';
import { NgxInputFileDirective } from './directives/inputfile.directive';
Expand Down Expand Up @@ -33,6 +33,15 @@ export function _loggerFactory(options: LoggerOptions): NgxUploadLogger {
return new NoOpLogger();
}

export const NGXUPLOAD_ROOT_GUARD = new InjectionToken<void>('Internal forRoot Guard');

export function createNgxUploadRootGuard(NGX_LOGGER_OPTIONS) {
if (NGX_LOGGER_OPTIONS) {
throw new TypeError('NgxUploadModule.forRoot() is called twice.')
}
return 'guarded';
}

@NgModule({
declarations: [
...ngxDeclarations
Expand All @@ -45,6 +54,7 @@ export function _loggerFactory(options: LoggerOptions): NgxUploadLogger {
})

export class NgxUploadModule {

static forRoot(dropTargetOptions?: DropTargetOptions,
loggerOptions?: LoggerOptions): ModuleWithProviders {

Expand All @@ -60,9 +70,14 @@ export class NgxUploadModule {
provide: NgxUploadLogger,
useFactory: _loggerFactory,
deps: [NGX_LOGGER_OPTIONS]
},
{
provide: NGXUPLOAD_ROOT_GUARD,
useFactory: createNgxUploadRootGuard,
deps: [[NGX_LOGGER_OPTIONS, new Optional(), new SkipSelf()]]
}
]
}

};
}