Skip to content

Commit

Permalink
feat(di): the way to add typings for registry result
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogradov committed Dec 28, 2018
1 parent c19e842 commit b76e4e1
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,31 @@ export function withRegistry(...registries: Registry[]) {
};
}

export interface IComponentRegistryConsumer {
id: string;
children: (registry: any) => React.ReactNode;
}

export const ComponentRegistryConsumer: React.SFC<IComponentRegistryConsumer> = props => (
<RegistryConsumer>
{registries => props.children(registries[props.id].snapshot())}
</RegistryConsumer>
);

export interface IRegistryOptions {
id: string;
inverted?: boolean;
}

interface IRegistryComponents {
[key: string]: any;
}

export class Registry {
id: string;
inverted: boolean;

private components = new Map<string, any>();
private components: IRegistryComponents = {};

constructor({ id, inverted = false }: IRegistryOptions) {
this.id = id;
Expand All @@ -63,7 +78,7 @@ export class Registry {
* @param component valid react component
*/
set<T>(id: string, component: ComponentType<T>) {
this.components.set(id, component);
this.components[id] = component;

return this;
}
Expand All @@ -75,11 +90,15 @@ export class Registry {
*/
get<T>(id: string): ComponentType<T> {
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>(): RT {
return this.components as any;
}
}

0 comments on commit b76e4e1

Please sign in to comment.