Skip to content

Commit

Permalink
Revert "Permit injection using only injectable or injection token. Re…
Browse files Browse the repository at this point in the history
…place "id" with "module" for better error messages."

This reverts commit 1582b1f.
  • Loading branch information
jansav committed Jan 17, 2022
1 parent 0dd2251 commit ff6892e
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 165 deletions.
9 changes: 5 additions & 4 deletions packages/injectable/ogre-tools-injectable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ declare module '@ogre-tools/injectable' {
key: Symbol;
};

export function getInjectionToken<TInstance, TInstantiationParameter = void>({
module: NodeModule,
}): InjectionToken<TInstance, TInstantiationParameter>;
export function getInjectionToken<
TInstance,
TInstantiationParameter = void,
>(): InjectionToken<TInstance, TInstantiationParameter>;

export interface Injectable<
TInjectionToken,
TInstance,
TInstantiationParameter,
> {
module: NodeModule;
id?: string;
setup?: (di: DependencyInjectionContainer) => void | Promise<void>;
causesSideEffects?: boolean;
lifecycle?: lifecycleEnum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default (...listOfGetRequireContexts) => {

const injectableIsBeingSetupped = pipeline(
context,
includes(`setup(${injectable.module.filename})`),
includes(`setup(${injectable.id})`),
);

if (
Expand All @@ -51,13 +51,13 @@ export default (...listOfGetRequireContexts) => {
!injectableIsBeingSetupped
) {
throw new Error(
`Tried to inject setuppable "${injectable.module.filename}" before setups are ran.`,
`Tried to inject setuppable "${injectable.id}" before setups are ran.`,
);
}

if (sideEffectsArePrevented && injectable.causesSideEffects) {
throw new Error(
`Tried to inject "${injectable.module.filename}" when side-effects are prevented.`,
`Tried to inject "${injectable.id}" when side-effects are prevented.`,
);
}

Expand All @@ -71,15 +71,17 @@ export default (...listOfGetRequireContexts) => {
},

register: injectable => {
if (!injectable.module) {
throw new Error('Tried to register injectable without module.');
if (!injectable.id) {
throw new Error('Tried to register injectable without ID.');
}

injectables.push({
...injectable,

aliases: [
injectable,
injectable.id,
injectable.instantiate,
...(injectable.injectionToken ? [injectable.injectionToken] : []),
...(injectable.aliases || []),
],
Expand Down Expand Up @@ -129,7 +131,7 @@ export default (...listOfGetRequireContexts) => {
map(originalInjectable => {
const overridingInjectable = getOverridingInjectable({
overridingInjectables,
alias: originalInjectable.module.filename,
alias: originalInjectable.id,
});

return overridingInjectable
Expand All @@ -142,9 +144,7 @@ export default (...listOfGetRequireContexts) => {
map(async injectable => {
await injectable.setup({
inject: (alias, parameter) =>
privateDi.inject(alias, parameter, [
`setup(${injectable.module.filename})`,
]),
privateDi.inject(alias, parameter, [`setup(${injectable.id})`]),
});
}),

Expand Down Expand Up @@ -199,7 +199,7 @@ const autoRegisterInjectables = ({ getRequireContextForInjectables, di }) => {
return {
id: key,
...injectableExport,
aliases: [injectableExport],
aliases: [injectableExport, ...(injectableExport.aliases || [])],
};
}),

Expand All @@ -215,16 +215,13 @@ const getInjectable = ({ injectables, alias }) => {

if (relatedInjectables.length === 0) {
throw new Error(
`Tried to inject non-registered injectable "${alias.module.filename}".`,
`Tried to inject non-registered injectable "${alias.toString()}".`,
);
}

if (relatedInjectables.length > 1) {
throw new Error(
`Tried to inject single injectable for injection token "${
alias.module.filename
}" but found multiple injectables: "${relatedInjectables
.map(relatedInjectable => relatedInjectable.module.filename)
.join('", "')}"`,
`Tried to inject injectable with ambiguous alias: "${alias.toString()}".`,
);
}

Expand Down
Loading

0 comments on commit ff6892e

Please sign in to comment.