Skip to content

Commit

Permalink
feat: Make deregistration of injectables variadic
Browse files Browse the repository at this point in the history
  • Loading branch information
Iku-turso committed Apr 7, 2022
1 parent d5ee408 commit 8681a6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ describe('createContainer.deregistration', () => {
}).toThrow('Tried to inject non-registered injectable "some-injectable".');
});

it('given multiple registered injectables and deregistered, when injecting, throws', () => {
const di = createContainer();

const someInjectable = getInjectable({
id: 'some-injectable',
instantiate: () => 'irrelevant',
});

const someOtherInjectable = getInjectable({
id: 'some-other-injectable',
instantiate: () => 'irrelevant',
});

di.register(someInjectable, someOtherInjectable);

di.deregister(someInjectable, someOtherInjectable);

expect(() => {
di.inject(someOtherInjectable);
}).toThrow(
'Tried to inject non-registered injectable "some-other-injectable".',
);
});

it('given not registered, when still deregistering, throws', () => {
const di = createContainer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,23 @@ export default () => {

register: withRegistrationDecorators(nonDecoratedRegister),

deregister: alias => {
if (!injectableMap.has(alias.id)) {
throw new Error(
`Tried to deregister non-registered injectable "${alias.id}".`,
);
}
deregister: (...aliases) => {
aliases.forEach(alias => {
if (!injectableMap.has(alias.id)) {
throw new Error(
`Tried to deregister non-registered injectable "${alias.id}".`,
);
}

purgeInstances(alias);
purgeInstances(alias);

injectables = pipeline(injectables, reject(isRelatedTo(alias)));
injectables = pipeline(injectables, reject(isRelatedTo(alias)));

overridingInjectables = pipeline(
overridingInjectables,
reject(isRelatedTo(alias)),
);
overridingInjectables = pipeline(
overridingInjectables,
reject(isRelatedTo(alias)),
);
});
},

override: (alias, instantiateStub) => {
Expand Down

0 comments on commit 8681a6e

Please sign in to comment.