Skip to content

Commit

Permalink
feat: 🎸 support registration of new transformers via module
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeevKatz committed Oct 19, 2020
1 parent 6a56ceb commit 1157d71
Showing 1 changed file with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import { NgModule } from '@angular/core';
import { Inject, InjectionToken, ModuleWithProviders, NgModule, Optional } from '@angular/core';
import { Class } from 'utility-types';

import { ControlValueTransformerRegister } from './register';
import { ControlValueTransformerDirective } from './directive';
import { Transformer } from './shared';

/**
* Injection token that used to run {@link registerControlValueTransformers}.
*/
export const REGISTER_CONTROL_VALUE_TRANSFORMERS = new InjectionToken<void>('REGISTER_CONTROL_VALUE_TRANSFORMERS');

/**
* Registration factory for new control value transformers.
* @param controlValueTransformerRegister - Registration service for control value transformers.
* @param controlValueTransformers - Control value transformers to register.
*/
export function registerControlValueTransformers(
controlValueTransformerRegister: ControlValueTransformerRegister,
...controlValueTransformers: Transformer[]
) {
controlValueTransformers.forEach((controlValueTransformer) =>
controlValueTransformerRegister.register(controlValueTransformer)
);
}

/**
* Control value transformer module.
Expand All @@ -9,4 +31,26 @@ import { ControlValueTransformerDirective } from './directive';
declarations: [ControlValueTransformerDirective],
exports: [ControlValueTransformerDirective],
})
export class ControlValueTransformerModule {}
export class ControlValueTransformerModule {
/**
* Registers new control value transformers.
* @param controlValueTransformers - List of classes that implements {@link Transformer} and decorated with {@link Transformer}.
*/
static registerTransformers(
controlValueTransformers: Class<Transformer>[]
): ModuleWithProviders<ControlValueTransformerModule> {
return {
ngModule: ControlValueTransformerModule,
providers: [
...controlValueTransformers,
{
provide: REGISTER_CONTROL_VALUE_TRANSFORMERS,
useFactory: registerControlValueTransformers,
deps: [ControlValueTransformerRegister, ...controlValueTransformers],
},
],
};
}

constructor(@Optional() @Inject(REGISTER_CONTROL_VALUE_TRANSFORMERS) registerControlValueTransformers: void) {}
}

0 comments on commit 1157d71

Please sign in to comment.