Skip to content

Commit

Permalink
fix: Improve typing of injectionDecoratorToken and instantiationDecor…
Browse files Browse the repository at this point in the history
…atorToken

Signed-off-by: Sebastian Malton <[email protected]>
  • Loading branch information
Nokel81 authored and Iku-turso committed Mar 30, 2023
1 parent 8f98ecc commit b204cf1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
28 changes: 19 additions & 9 deletions packages/injectable/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,29 @@ export const lifecycleEnum: {
};
};

type Decorator = InjectionToken<
{ decorate: (toBeDecorated: unknown) => typeof toBeDecorated },
unknown
>;

type RegistrationCallback = (injectable: Injectable<any, any, any>) => void;

type TargetableDecorator = Decorator & {
target?: Injectable<any, any, any> | InjectionToken<any, any>;
export type InjectionTargetDecorator<InjectionInstance> = {
decorate: (instance: InjectionInstance) => InjectionInstance;
};

export type InstantiationTargetDecorator<
InjectionInstance extends InjectionTokenInstance,
InjectionTokenInstance = InjectionInstance,
InstantiationParam = void,
> = {
decorate: (instantiate: Instantiate<InjectionInstance, InstantiationParam>) => Instantiate<InjectionInstance, InstantiationParam>;
target: InjectionToken<InjectionInstance, InstantiationParam> | Injectable<InjectionInstance, InjectionTokenInstance, InstantiationParam>;
};

export const injectionDecoratorToken: TargetableDecorator;
export const instantiationDecoratorToken: TargetableDecorator;
export const createInstantiationTargetDecorator: <
InjectionInstance extends InjectionTokenInstance,
InjectionTokenInstance = InjectionInstance,
InstantiationParam = void,
>(desc: InstantiationTargetDecorator<InjectionInstance, InjectionTokenInstance, InstantiationParam>) => InstantiationTargetDecorator<InjectionInstance, InjectionTokenInstance, InstantiationParam>;

export const injectionDecoratorToken: InjectionToken<InjectionTargetDecorator<any>, void>;
export const instantiationDecoratorToken: InjectionToken<InstantiationTargetDecorator<any, any>, void>;
export const registrationCallbackToken: RegistrationCallback;
export const deregistrationCallbackToken: RegistrationCallback;
export const isInjectable: (thing: any) => boolean;
Expand Down
9 changes: 3 additions & 6 deletions packages/injectable/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import createContainer from './src/dependency-injection-container/createContaine

import isInjectable from './src/getInjectable/isInjectable';
import isInjectionToken from './src/getInjectionToken/isInjectionToken';
import {

export {
deregistrationCallbackToken,
injectionDecoratorToken,
createInstantiationTargetDecorator,
instantiationDecoratorToken,
registrationCallbackToken,
} from './src/dependency-injection-container/tokens';

export {
createContainer,
getInjectable,
getInjectionToken,
injectionDecoratorToken,
instantiationDecoratorToken,
registrationCallbackToken,
deregistrationCallbackToken,
isInjectable,
isInjectionToken,
lifecycleEnum,
Expand Down
24 changes: 24 additions & 0 deletions packages/injectable/core/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,35 @@ import {
DiContainerForInjection,
getInjectable,
getInjectionToken,
instantiationDecoratorToken,
createInstantiationTargetDecorator,
lifecycleEnum,
} from '.';

const di = createContainer('some-container');

const someGetNumberInjectionToken = getInjectionToken<() => number>({
id: "some-get-number-token",
});

const decorateSomeGetNumberInjectable = getInjectable({
id: "decorate-some-get-number",
decorable: false,
instantiate: () => createInstantiationTargetDecorator({
target: someGetNumberInjectionToken,
decorate: (someGetNumberInstantiate) => (di) => {
const someGetNumber = someGetNumberInstantiate(di);

return () => {
console.log("some other thing");

return someGetNumber();
}
},
}),
injectionToken: instantiationDecoratorToken,
})

// given injectable with unspecified type for instantiation parameter, argument typing is OK
const someInjectableForTypingOfInstantiate = getInjectable({
id: 'some-injectable',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const deregistrationCallbackToken = getInjectionToken({
decorable: false,
});

export const createInstantiationTargetDecorator = desc => desc;

export const instantiationDecoratorToken = getInjectionToken({
id: 'instantiate-decorator-token',
decorable: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
exports[`createContainer.dependency-graph given dependency graphing, dependencies and injected, creates Plant-UML graph 1`] = `
"@startuml
hide members
class "some-token-injectable" as someTokenInjectable<Singleton> << (S,lightGreen) >> $some-token-injectable $some-container $some-parent-injectable $some-sync-child-injectable $some-injection-token #line:darkRed
class "some-customizable-sync-injectable" as someCustomizableSyncInjectable<Singleton\\nsome-custom-node-info> << (S,lightGreen) >> $some-customizable-sync-injectable $some-container $some-parent-injectable $some-sync-child-injectable $some-injection-token #line:darkRed
class "some-injection-token" as someInjectionToken<Transient\\nToken> << (X,orange) >> $some-injection-token $some-container $some-parent-injectable $some-sync-child-injectable #line:green
class "some-sync-child-injectable" as someSyncChildInjectable<Transient> << (T,orchid) >> $some-sync-child-injectable $some-container $some-parent-injectable #line:darkRed
class "some-customizable-async-injectable" as someCustomizableAsyncInjectable<Singleton\\nAsync\\nsome-custom-node-info> << (S,lightGreen) >> $some-customizable-async-injectable $some-container $some-parent-injectable $some-async-child-injectable #line:darkRed
class "some-async-child-injectable" as someAsyncChildInjectable<Singleton\\nAsync> << (S,lightGreen) >> $some-async-child-injectable $some-container $some-parent-injectable #line:darkRed
class "some-parent-injectable" as someParentInjectable<Singleton\\nAsync> << (S,lightGreen) >> $some-parent-injectable $some-container #line:darkRed
class "some-keyed-injectable" as someKeyedInjectable<Keyed> << (K,pink) >> $some-keyed-injectable $some-container $some-parent-injectable #line:darkRed
class \\"some-token-injectable\\" as someTokenInjectable<Singleton> << (S,lightGreen) >> $some-token-injectable $some-container $some-parent-injectable $some-sync-child-injectable $some-injection-token #line:darkRed
class \\"some-customizable-sync-injectable\\" as someCustomizableSyncInjectable<Singleton\\\\nsome-custom-node-info> << (S,lightGreen) >> $some-customizable-sync-injectable $some-container $some-parent-injectable $some-sync-child-injectable $some-injection-token #line:darkRed
class \\"some-injection-token\\" as someInjectionToken<Transient\\\\nToken> << (X,orange) >> $some-injection-token $some-container $some-parent-injectable $some-sync-child-injectable #line:green
class \\"some-sync-child-injectable\\" as someSyncChildInjectable<Transient> << (T,orchid) >> $some-sync-child-injectable $some-container $some-parent-injectable #line:darkRed
class \\"some-customizable-async-injectable\\" as someCustomizableAsyncInjectable<Singleton\\\\nAsync\\\\nsome-custom-node-info> << (S,lightGreen) >> $some-customizable-async-injectable $some-container $some-parent-injectable $some-async-child-injectable #line:darkRed
class \\"some-async-child-injectable\\" as someAsyncChildInjectable<Singleton\\\\nAsync> << (S,lightGreen) >> $some-async-child-injectable $some-container $some-parent-injectable #line:darkRed
class \\"some-parent-injectable\\" as someParentInjectable<Singleton\\\\nAsync> << (S,lightGreen) >> $some-parent-injectable $some-container #line:darkRed
class \\"some-keyed-injectable\\" as someKeyedInjectable<Keyed> << (K,pink) >> $some-keyed-injectable $some-container $some-parent-injectable #line:darkRed
someInjectionToken --[#black,plain,thickness=1]up* someTokenInjectable #text:black
someInjectionToken --[#orange,plain,thickness=1]up* someCustomizableSyncInjectable #text:green : some-custom-link-info
someSyncChildInjectable --[#black,plain,thickness=1]up* someInjectionToken #text:black
someParentInjectable --[#black,plain,thickness=1]up* someSyncChildInjectable #text:black
someAsyncChildInjectable --[#orange,plain,thickness=4]up* someCustomizableAsyncInjectable #text:green : Async\\nsome-custom-link-info
someAsyncChildInjectable --[#orange,plain,thickness=4]up* someCustomizableAsyncInjectable #text:green : Async\\\\nsome-custom-link-info
someParentInjectable --[#black,plain,thickness=4]up* someAsyncChildInjectable #text:black : Async
someContainer --[#black,plain,thickness=4]up* someParentInjectable #text:black : Async
someParentInjectable --[#black,plain,thickness=1]up* someKeyedInjectable #text:black
Expand Down

0 comments on commit b204cf1

Please sign in to comment.