Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(di): check types in registry #462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ interface IRegistryComponents {
[key: string]: any;
}

export class Registry {
export class Registry<T extends IRegistryComponents = IRegistryComponents> {
id: string;
overridable: boolean;
private components: IRegistryComponents = {};
private components: T;

constructor({ id, overridable = true }: IRegistryOptions) {
constructor({ id, overridable = true }: IRegistryOptions, components = {} as T) {
this.id = id;
this.overridable = overridable;
this.components = components;
}

/**
Expand All @@ -99,7 +100,7 @@ export class Registry {
* @param id component id
* @param component valid react component
*/
set<T>(id: string, component: ComponentType<T>) {
set<K extends keyof T>(id: K, component: T[K]) {
this.components[id] = component;

return this;
Expand All @@ -110,7 +111,7 @@ export class Registry {
*
* @param id component id
*/
get<T>(id: string): ComponentType<T> {
get<K extends keyof T>(id: K): T[K] {
if (__DEV__) {
if (!this.components[id]) {
throw new Error(`Component with id '${id}' not found.`);
Expand All @@ -130,10 +131,11 @@ export class Registry {
/**
* Override components by external registry.
*
* @internal
* @param registry external registry
belozer marked this conversation as resolved.
Show resolved Hide resolved
*/
merge(registry: Registry) {
const clone = new Registry({ id: this.id, overridable: this.overridable });
merge(registry: Registry<Partial<T>>) {
const clone = new Registry<T>({ id: this.id, overridable: this.overridable });

clone.components = {
...this.components,
Expand Down
27 changes: 27 additions & 0 deletions packages/di/test/di.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ describe('@bem-react/di', () => {
expect(registry.get('id-2')).to.eq(Component2);
});

it('should set components from constructor', () => {
const Component1 = () => null;
const Component2 = () => <span/>;

const registry = new Registry({ id: 'registry' }, {
'id-1': Component1,
'id-2': Component2
});

expect(registry.get('id-1')).to.eq(Component1);
expect(registry.get('id-2')).to.eq(Component2);
});

// NOTE: Affect performance after implementation
it.skip('should use internal copy of components from constructor', () => {
const Component1 = () => null;
const Component2 = () => <span/>;

const components = { 'id-1': Component1, 'id-2': Component2 };
const registry = new Registry({ id: 'registry' }, components);

components['id-2'] = () => <div />;

expect(registry.get('id-1')).to.eq(Component1);
expect(registry.get('id-2')).to.eq(Component2);
});

it('should return list of components', () => {
const registry = new Registry({ id: 'registry' });
const Component1 = () => null;
Expand Down