Skip to content

Commit

Permalink
Revert "Revert "Remove concept of "injectable viability" for being YA…
Browse files Browse the repository at this point in the history
…GNI""

This reverts commit 485e303.
  • Loading branch information
Iku-turso committed Jan 21, 2022
1 parent 4c88b4a commit e4979c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 188 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import tap from 'lodash/fp/tap';
import conforms from 'lodash/fp/conforms';
import every from 'lodash/fp/every';
import filter from 'lodash/fp/filter';
import find from 'lodash/fp/find';
import findLast from 'lodash/fp/findLast';
import first from 'lodash/fp/first';
import forEach from 'lodash/fp/forEach';
import get from 'lodash/fp/get';
import includes from 'lodash/fp/includes';
import invoke from 'lodash/fp/invoke';
import lifecycleEnum from './lifecycleEnum';
Expand All @@ -22,19 +20,17 @@ export default (...listOfGetRequireContexts) => {

const instanceMap = new Map();

const getLifecycle = injectableKey =>
const getLifecycle = alias =>
getInjectable({
injectables,
alias: injectableKey,
di: privateDi,
alias,
}).lifecycle;

const privateDi = {
inject: (alias, instantiationParameter, context = []) => {
const originalInjectable = getInjectable({
injectables,
alias,
di: privateDi,
});

const overriddenInjectable = getOverridingInjectable({
Expand Down Expand Up @@ -162,15 +158,14 @@ export default (...listOfGetRequireContexts) => {
},

permitSideEffects: alias => {
getInjectable({ injectables, alias, di: privateDi }).permitSideEffects();
getInjectable({ injectables, alias }).permitSideEffects();
},

getLifecycle,

purge: injectableKey => {
const injectable = getInjectable({
injectables,
di: privateDi,
alias: injectableKey,
});

Expand Down Expand Up @@ -212,7 +207,7 @@ const autoRegisterInjectables = ({ getRequireContextForInjectables, di }) => {
);
};

const getInjectable = ({ injectables, alias, di }) => {
const getInjectable = ({ injectables, alias }) => {
const relatedInjectables = pipeline(
injectables,
filter(getRelatedInjectables(alias)),
Expand All @@ -224,47 +219,14 @@ const getInjectable = ({ injectables, alias, di }) => {
);
}

if (relatedInjectables.length > 1 && !viabilityIsOk(relatedInjectables)) {
if (relatedInjectables.length > 1) {
throw new Error(
`Tried to inject one of multiple injectables with no way to demonstrate viability for "${relatedInjectables
.map(get('id'))
.join('", "')}"`,
`Tried to inject injectable with ambiguous alias: "${alias.toString()}".`,
);
}

const viableInjectables = pipeline(
relatedInjectables,
filter(injectable =>
injectable.viability ? injectable.viability(di) : true,
),
);

if (relatedInjectables.length === 1 && viableInjectables.length === 0) {
throw new Error(
`Tried to inject injectable with no viability for "${relatedInjectables[0].id}"`,
);
}

if (viableInjectables.length === 0) {
throw new Error(
`Tried to inject one of multiple injectables with no viability within "${relatedInjectables
.map(get('id'))
.join('", "')}"`,
);
}

if (viableInjectables.length !== 1) {
throw new Error(
`Tried to inject one of multiple injectables with non-singular viability within "${relatedInjectables
.map(get('id'))
.join('", "')}"`,
);
}

return first(viableInjectables);
return first(relatedInjectables);
};

const viabilityIsOk = every('viability');

const getOverridingInjectable = ({ overridingInjectables, alias }) =>
pipeline(overridingInjectables, findLast(getRelatedInjectables(alias)));
Original file line number Diff line number Diff line change
Expand Up @@ -680,161 +680,23 @@ describe('createContainer', () => {
);
});

it('given multiple injectables with same alias, but no way to demonstrate viability, when injected, throws', () => {
it('when injecting ambiguous injectable, throws', () => {
const someInjectable = getInjectable({
id: 'some-injectable-id',
aliases: ['some-alias'],
viability: undefined,
id: 'some-ambiguous-injectable-id',
instantiate: () => 'irrelevant',
});

const someOtherInjectable = getInjectable({
id: 'some-other-injectable-id',
aliases: ['some-alias'],
viability: () => {},
id: 'some-ambiguous-injectable-id',
instantiate: () => 'irrelevant',
});

const di = getDi(someInjectable, someOtherInjectable);

expect(() => {
di.inject('some-alias');
}).toThrow(
'Tried to inject one of multiple injectables with no way to demonstrate viability for "some-injectable-id", "some-other-injectable-id"',
);
});

it('given multiple injectables with same alias, one of which is viable, when injected, injects viable instance', () => {
const someInjectable = getInjectable({
aliases: ['some-alias'],
viability: () => false,
instantiate: () => 'irrelevant',
});

const someOtherInjectable = getInjectable({
aliases: ['some-alias'],
viability: () => true,
instantiate: () => 'viable-instance',
});

const di = getDi(someInjectable, someOtherInjectable);

const actual = di.inject('some-alias');

expect(actual).toBe('viable-instance');
});

it('given multiple injectables with same alias, one of which is viable, given overridden, when injected, injects overridden instance', () => {
const someInjectable = getInjectable({
aliases: ['some-alias'],
viability: () => false,
instantiate: () => 'irrelevant',
});

const someOtherInjectable = getInjectable({
aliases: ['some-alias'],
viability: () => true,
instantiate: () => 'irrelevant',
});

const di = getDi(someInjectable, someOtherInjectable);

di.override('some-alias', () => 'overridden-instance');

const actual = di.inject('some-alias');

expect(actual).toBe('overridden-instance');
});

it('given multiple injectables with same alias, one of which is viable by considering a third injectable, injects viable instance', () => {
const someThirdInjectable = getInjectable({
aliases: ['third-injectable-alias'],
instantiate: () => 'third-injectable-instance',
});

const someInjectable = getInjectable({
aliases: ['some-alias'],
viability: di =>
di.inject('third-injectable-alias') !== 'third-injectable-instance',
instantiate: () => 'irrelevant',
});

const someOtherInjectable = getInjectable({
aliases: ['some-alias'],
viability: di =>
di.inject('third-injectable-alias') === 'third-injectable-instance',
instantiate: () => 'viable-instance',
});

const di = getDi(someInjectable, someOtherInjectable, someThirdInjectable);

const actual = di.inject('some-alias');

expect(actual).toBe('viable-instance');
});

it('given multiple injectables with same alias, all of which are unviable, when injected, throws', () => {
const someInjectable = getInjectable({
id: 'some-injectable-id',
aliases: ['some-alias'],
viability: () => false,
instantiate: () => 'irrelevant',
});

const someOtherInjectable = getInjectable({
id: 'some-other-injectable-id',
aliases: ['some-alias'],
viability: () => false,
instantiate: () => 'irrelevant',
});

const di = getDi(someInjectable, someOtherInjectable);

expect(() => {
di.inject('some-alias');
}).toThrow(
'Tried to inject one of multiple injectables with no viability within "some-injectable-id", "some-other-injectable-id"',
);
});

it('given multiple injectables with same alias, all of which are viable, when injected, throws', () => {
const someInjectable = getInjectable({
id: 'some-injectable-id',
aliases: ['some-alias'],
viability: () => true,
instantiate: () => 'irrelevant',
});

const someOtherInjectable = getInjectable({
id: 'some-other-injectable-id',
aliases: ['some-alias'],
viability: () => true,
instantiate: () => 'irrelevant',
});

const di = getDi(someInjectable, someOtherInjectable);

expect(() => {
di.inject('some-alias');
}).toThrow(
'Tried to inject one of multiple injectables with non-singular viability within "some-injectable-id", "some-other-injectable-id"',
);
});

it('given single injectable, but unviable, when injected, throws', () => {
const someInjectable = getInjectable({
id: 'some-injectable-id',
aliases: ['some-alias'],
viability: () => false,
instantiate: () => 'irrelevant',
});

const di = getDi(someInjectable);

expect(() => {
di.inject('some-alias');
di.inject('some-ambiguous-injectable-id');
}).toThrow(
'Tried to inject injectable with no viability for "some-injectable-id"',
`Tried to inject injectable with ambiguous alias: "some-ambiguous-injectable-id"`,
);
});

Expand Down

0 comments on commit e4979c6

Please sign in to comment.