Skip to content

Commit

Permalink
feat!: Make the injectable id for "meta" namespaced to avoid collisions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Make sure previous usages of injectable id of meta do not break with the
new addition of namespace. Eg. what previously was di.injectManyWithMeta(someInjectionToken) ->
{ id: 'some-id', instance: ... }
...will now become:
{ id: 'some-scope:some-id', instance: ... }
...when a scope is present.
  • Loading branch information
Iku-turso committed May 15, 2023
1 parent a8527c1 commit baa52a9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default (containerId, { detectCycles = true } = {}) => {
getRelatedInjectables,
getInject: () => decoratedPrivateInject,
setDependee,
getNamespacedId,
});

const nonDecoratedPrivateInjectMany =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const privateInjectManyFor =
getRelatedInjectables,
getInject,
setDependee,
getNamespacedId,
}) =>
({ withMeta }) =>
(
Expand Down Expand Up @@ -34,16 +35,18 @@ export const privateInjectManyFor =
return instance;
}

const namespacedId = getNamespacedId(injectable);

if (!isPromise(instance)) {
return {
instance,
meta: { id: injectable.id },
meta: { id: namespacedId },
};
}

return instance.then(awaitedInstance => ({
instance: awaitedInstance,
meta: { id: injectable.id },
meta: { id: namespacedId },
}));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,84 @@ describe('injection with meta data', () => {
]);
});

it('given scope, when injecting many sync injectable with meta data, does so', () => {
const someInjectionToken = getInjectionToken({
id: 'some-injection-token-id',
});

const registerInScopeInjectable = getInjectable({
id: 'some-scope',

instantiate: di => injectable => {
di.register(injectable);
},

scope: true,
});

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

const di = createContainer('irrelevant');

di.register(registerInScopeInjectable);

const registerInScope = di.inject(registerInScopeInjectable);

registerInScope(someInjectable);

const actual = di.injectManyWithMeta(someInjectionToken);

expect(actual).toEqual([
{
instance: 'some-instance',
meta: { id: 'some-scope:some-injectable' },
},
]);
});

it('given scope, when injecting many async injectable with meta data, does so', async () => {
const someInjectionToken = getInjectionToken({
id: 'some-injection-token-id',
});

const registerInScopeInjectable = getInjectable({
id: 'some-scope',

instantiate: di => injectable => {
di.register(injectable);
},

scope: true,
});

const someInjectable = getInjectable({
id: 'some-injectable',
instantiate: async () => 'some-instance',
injectionToken: someInjectionToken,
});

const di = createContainer('irrelevant');

di.register(registerInScopeInjectable);

const registerInScope = di.inject(registerInScopeInjectable);

registerInScope(someInjectable);

const actual = await di.injectManyWithMeta(someInjectionToken);

expect(actual).toEqual([
{
instance: 'some-instance',
meta: { id: 'some-scope:some-injectable' },
},
]);
});

it('given injecting many async injectable with meta data, does so', async () => {
const someInjectionToken = getInjectionToken({
id: 'some-injection-token-id',
Expand Down

0 comments on commit baa52a9

Please sign in to comment.