From 80662496140aede725d0e0f519cc11089fff5502 Mon Sep 17 00:00:00 2001 From: Sergey Belozyorcev Date: Fri, 9 Aug 2019 04:43:38 +0300 Subject: [PATCH] feat(di): check registry types --- packages/di/di.tsx | 16 +++++++++------- packages/di/test/di.test.tsx | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/di/di.tsx b/packages/di/di.tsx index 781c3233..628de189 100644 --- a/packages/di/di.tsx +++ b/packages/di/di.tsx @@ -83,14 +83,15 @@ interface IRegistryComponents { [key: string]: any; } -export class Registry { +export class Registry { id: string; overridable: boolean; - private components: IRegistryComponents = {}; + private components: T; - constructor({ id, overridable = true }: IRegistryOptions) { + constructor({ id, overridable = true }: IRegistryOptions, components = {} as T) { this.id = id; this.overridable = overridable; + this.components = components; } /** @@ -99,7 +100,7 @@ export class Registry { * @param id component id * @param component valid react component */ - set(id: string, component: ComponentType) { + set(id: K, component: T[K]) { this.components[id] = component; return this; @@ -110,7 +111,7 @@ export class Registry { * * @param id component id */ - get(id: string): ComponentType { + get(id: K): T[K] { if (__DEV__) { if (!this.components[id]) { throw new Error(`Component with id '${id}' not found.`); @@ -130,10 +131,11 @@ export class Registry { /** * Override components by external registry. * + * @internal * @param registry external registry */ - merge(registry: Registry) { - const clone = new Registry({ id: this.id, overridable: this.overridable }); + merge(registry: Registry>) { + const clone = new Registry({ id: this.id, overridable: this.overridable }); clone.components = { ...this.components, diff --git a/packages/di/test/di.test.tsx b/packages/di/test/di.test.tsx index 22d4f92c..6ab1cc21 100644 --- a/packages/di/test/di.test.tsx +++ b/packages/di/test/di.test.tsx @@ -33,6 +33,33 @@ describe('@bem-react/di', () => { expect(registry.get('id-2')).to.eq(Component2); }); + it('should set components from constructor', () => { + const Component1 = () => null; + const Component2 = () => ; + + const registry = new Registry({ id: 'registry' }, { + 'id-1': Component1, + 'id-2': Component2 + }); + + expect(registry.get('id-1')).to.eq(Component1); + expect(registry.get('id-2')).to.eq(Component2); + }); + + // NOTE: Affect performance after implementation + it.skip('should use internal copy of components from constructor', () => { + const Component1 = () => null; + const Component2 = () => ; + + const components = { 'id-1': Component1, 'id-2': Component2 }; + const registry = new Registry({ id: 'registry' }, components); + + components['id-2'] = () =>
; + + expect(registry.get('id-1')).to.eq(Component1); + expect(registry.get('id-2')).to.eq(Component2); + }); + it('should return list of components', () => { const registry = new Registry({ id: 'registry' }); const Component1 = () => null;