Skip to content

Commit

Permalink
refactor(config): module config registration (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenlautier authored Nov 7, 2020
1 parent 1914af2 commit 8ebb878
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
This was added due to a limitation (which previously was handled via hack and is not supported anymore). [See this issue](https://github.com/angular/angular/issues/8277).
- **module:** rename module to `SsvCommandModule` from `CommandModule`

## [1.6.1](https://github.com/sketch7/ngx.ux/compare/1.6.0...1.6.1) (2020-11-07)

### Refactor

- **config:** refactor module config registration

## [1.6.0](https://github.com/sketch7/ngx.ux/compare/1.5.2...1.6.0) (2020-11-04)

### Features
Expand Down
26 changes: 14 additions & 12 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import { NgModule, ModuleWithProviders, InjectionToken } from "@angular/core";
import { NgModule, ModuleWithProviders, InjectionToken, Optional } from "@angular/core";

import { CommandDirective } from "./command.directive";
import { CommandRefDirective } from "./command-ref.directive";
import { CommandOptions, COMMAND_DEFAULT_CONFIG, COMMAND_CONFIG } from "./config";

/** @internal */
export const _MODULE_CONFIG = new InjectionToken<CommandOptions>("_command-config");
export const MODULE_CONFIG_DATA = new InjectionToken<CommandOptions>("@ssv/ngx.command/configData");

const components = [
CommandDirective,
CommandRefDirective
];

@NgModule({
declarations: [CommandDirective, CommandRefDirective],
providers: [{ provide: COMMAND_CONFIG, useValue: COMMAND_DEFAULT_CONFIG }],
exports: [CommandDirective, CommandRefDirective],
declarations: components,
providers: [
{ provide: COMMAND_CONFIG, useFactory: _moduleConfigFactory, deps: [[MODULE_CONFIG_DATA, new Optional()]] },
],
exports: [...components],
})
export class SsvCommandModule {

static forRoot(config?: Partial<CommandOptions> | (() => Partial<CommandOptions>)): ModuleWithProviders<SsvCommandModule> {
return {
ngModule: SsvCommandModule,
providers: [
{
provide: COMMAND_CONFIG,
useFactory: moduleConfigFactory,
deps: [_MODULE_CONFIG],
},
{ provide: _MODULE_CONFIG, useValue: config },
{ provide: MODULE_CONFIG_DATA, useValue: config },
],
};
}

}

/** @internal */
export function moduleConfigFactory(config: CommandOptions | (() => CommandOptions)): CommandOptions {
export function _moduleConfigFactory(config: CommandOptions | (() => CommandOptions)): CommandOptions {
const cfg = typeof config === "function" ? config() : config;
return cfg
? {
Expand Down

0 comments on commit 8ebb878

Please sign in to comment.