Skip to content

Commit

Permalink
fix(di): use map as class option for using in es5
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Oct 1, 2018
1 parent 91f868e commit 7fd489f
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,38 @@ export function withRegistry<P>(...registries: Registry[]) {
};
}

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

export class Registry extends Map<any, any> {
export class Registry {
id: string;
inverted?: boolean = false;
inverted: boolean;

constructor(options: IRegistry) {
super();
this.id = options.id;
this.inverted = options.inverted;
private components = new Map<string, any>();

constructor({ id, inverted = false }: IRegistryOptions) {
this.id = id;
this.inverted = inverted;
}

set<T>(id: string, component: React.ComponentType<T>) {
this.components.set(id, component);
}

get<P>(id: any): React.ComponentType<P> {
/**
* Get react component from registry by id.
*
* @param id component id
*/
get<T>(id: string): React.ComponentType<T> {
if (__DEV__) {
if (!this.has(id)) {
if (!this.components.has(id)) {
throw new Error(`Component with id '${id}' not found.`);
}
}

return this.get(id);
return this.components.get(id);
}
}

0 comments on commit 7fd489f

Please sign in to comment.