From b76e4e1cf76063fb2abb914c823bc71b33b141e0 Mon Sep 17 00:00:00 2001 From: awinogradov Date: Fri, 28 Dec 2018 15:21:33 +0300 Subject: [PATCH] feat(di): the way to add typings for registry result --- packages/di/di.tsx | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/di/di.tsx b/packages/di/di.tsx index a573a690..ee2d4d93 100644 --- a/packages/di/di.tsx +++ b/packages/di/di.tsx @@ -40,16 +40,31 @@ export function withRegistry(...registries: Registry[]) { }; } +export interface IComponentRegistryConsumer { + id: string; + children: (registry: any) => React.ReactNode; +} + +export const ComponentRegistryConsumer: React.SFC = props => ( + + {registries => props.children(registries[props.id].snapshot())} + +); + export interface IRegistryOptions { id: string; inverted?: boolean; } +interface IRegistryComponents { + [key: string]: any; +} + export class Registry { id: string; inverted: boolean; - private components = new Map(); + private components: IRegistryComponents = {}; constructor({ id, inverted = false }: IRegistryOptions) { this.id = id; @@ -63,7 +78,7 @@ export class Registry { * @param component valid react component */ set(id: string, component: ComponentType) { - this.components.set(id, component); + this.components[id] = component; return this; } @@ -75,11 +90,15 @@ export class Registry { */ get(id: string): ComponentType { if (__DEV__) { - if (!this.components.has(id)) { + if (!this.components[id]) { throw new Error(`Component with id '${id}' not found.`); } } - return this.components.get(id); + return this.components[id]; + } + + snapshot(): RT { + return this.components as any; } }