-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: guard against importing ConsoleDriverModule without forRoot (#12)
* refactor: simplify LogDriver provider in ConsoleDriverModule * feat: guard against importing ConsoleDriverModule without forRoot
- Loading branch information
Showing
4 changed files
with
38 additions
and
30 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...ects/ngworker/lumberjack/src/lib/log-drivers/console-driver/console-driver-root.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NgModule } from '@angular/core'; | ||
|
||
import { LogDriverToken } from '../log-driver'; | ||
|
||
import { ConsoleDriver } from './console.driver'; | ||
|
||
@NgModule({ | ||
providers: [ | ||
{ | ||
provide: LogDriverToken, | ||
useClass: ConsoleDriver, | ||
multi: true, | ||
}, | ||
], | ||
}) | ||
export class ConsoleDriverRootModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 8 additions & 17 deletions
25
projects/ngworker/lumberjack/src/lib/log-drivers/console-driver/console-driver.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,19 @@ | ||
import { ModuleWithProviders, NgModule } from '@angular/core'; | ||
|
||
import { defaultLogDriverConfig, LogDriverConfig, LogDriverConfigToken } from '../../configs/log-driver.config'; | ||
import { LogDriverToken } from '../log-driver'; | ||
|
||
import { ConsoleDriver } from './console.driver'; | ||
|
||
export function consoleFactory(config: LogDriverConfig): ConsoleDriver { | ||
return new ConsoleDriver(config); | ||
} | ||
import { ConsoleDriverRootModule } from './console-driver-root.module'; | ||
|
||
@NgModule() | ||
export class ConsoleDriverModule { | ||
static forRoot(config: LogDriverConfig = defaultLogDriverConfig): ModuleWithProviders<ConsoleDriverModule> { | ||
static forRoot(config: LogDriverConfig = defaultLogDriverConfig): ModuleWithProviders<ConsoleDriverRootModule> { | ||
return { | ||
ngModule: ConsoleDriverModule, | ||
providers: [ | ||
{ provide: LogDriverConfigToken, useValue: config }, | ||
{ | ||
provide: LogDriverToken, | ||
useFactory: consoleFactory, | ||
multi: true, | ||
deps: [LogDriverConfigToken], | ||
}, | ||
], | ||
ngModule: ConsoleDriverRootModule, | ||
providers: [{ provide: LogDriverConfigToken, useValue: config }], | ||
}; | ||
} | ||
|
||
constructor() { | ||
throw new Error('Do not import ConsoleDriverModule directly. Use ConsoleDriverModule.forRoot.'); | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
projects/ngworker/lumberjack/src/lib/log-drivers/console-driver/console.driver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters