Skip to content

Commit b76e4e1

Browse files
committed
feat(di): the way to add typings for registry result
1 parent c19e842 commit b76e4e1

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/di/di.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,31 @@ export function withRegistry(...registries: Registry[]) {
4040
};
4141
}
4242

43+
export interface IComponentRegistryConsumer {
44+
id: string;
45+
children: (registry: any) => React.ReactNode;
46+
}
47+
48+
export const ComponentRegistryConsumer: React.SFC<IComponentRegistryConsumer> = props => (
49+
<RegistryConsumer>
50+
{registries => props.children(registries[props.id].snapshot())}
51+
</RegistryConsumer>
52+
);
53+
4354
export interface IRegistryOptions {
4455
id: string;
4556
inverted?: boolean;
4657
}
4758

59+
interface IRegistryComponents {
60+
[key: string]: any;
61+
}
62+
4863
export class Registry {
4964
id: string;
5065
inverted: boolean;
5166

52-
private components = new Map<string, any>();
67+
private components: IRegistryComponents = {};
5368

5469
constructor({ id, inverted = false }: IRegistryOptions) {
5570
this.id = id;
@@ -63,7 +78,7 @@ export class Registry {
6378
* @param component valid react component
6479
*/
6580
set<T>(id: string, component: ComponentType<T>) {
66-
this.components.set(id, component);
81+
this.components[id] = component;
6782

6883
return this;
6984
}
@@ -75,11 +90,15 @@ export class Registry {
7590
*/
7691
get<T>(id: string): ComponentType<T> {
7792
if (__DEV__) {
78-
if (!this.components.has(id)) {
93+
if (!this.components[id]) {
7994
throw new Error(`Component with id '${id}' not found.`);
8095
}
8196
}
8297

83-
return this.components.get(id);
98+
return this.components[id];
99+
}
100+
101+
snapshot<RT>(): RT {
102+
return this.components as any;
84103
}
85104
}

0 commit comments

Comments
 (0)