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 authored and awinogradov committed Nov 26, 2018
1 parent 9e762ef commit 24e9015
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 24e9015

Please sign in to comment.