Skip to content

Commit

Permalink
feat(di): partially registries merge
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogradov committed Jan 10, 2019
1 parent c836d59 commit 7890e03
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export function withRegistry(...registries: Registry[]) {
const overrides = contextRegistries[registry.id];

providedRegistries[registry.id] = registry.inverted
? (overrides || registry)
: (registry || overrides);
? overrides ? registry.merge(overrides) : registry
: (registry && overrides) ? overrides.merge(registry) : registry;
});

return (
Expand Down Expand Up @@ -63,7 +63,6 @@ interface IRegistryComponents {
export class Registry {
id: string;
inverted: boolean;

private components: IRegistryComponents = {};

constructor({ id, inverted = false }: IRegistryOptions) {
Expand Down Expand Up @@ -98,7 +97,24 @@ export class Registry {
return this.components[id];
}

/**
* Returns list of components from registry.
*/
snapshot<RT>(): RT {
return this.components as any;
}

/**
* Override components by external registry.
*
* @param registry external registry
*/
merge(registry: Registry) {
this.components = {
...this.components,
...(registry ? registry.snapshot() : {}),
};

return this;
}
}

0 comments on commit 7890e03

Please sign in to comment.