Skip to content

Commit

Permalink
refactor: update decorators to handle pending class metadata count
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Dec 16, 2024
1 parent 832c935 commit eb97fda
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jest.mock('../calculations/buildManagedMetadataFromMaybeClassElementMetadata');
jest.mock('../calculations/handleInjectionError');
jest.mock('./injectBase');

import { decrementPendingClassMetadataCount } from '../actions/decrementPendingClassMetadataCount';
import { buildManagedMetadataFromMaybeClassElementMetadata } from '../calculations/buildManagedMetadataFromMaybeClassElementMetadata';
import { handleInjectionError } from '../calculations/handleInjectionError';
import { ClassElementMetadata } from '../models/ClassElementMetadata';
Expand Down Expand Up @@ -84,7 +85,10 @@ describe(inject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should call injectBaseDecorator()', () => {
Expand Down Expand Up @@ -164,7 +168,10 @@ describe(inject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should throw handleInjectionError()', () => {
Expand Down Expand Up @@ -253,7 +260,10 @@ describe(inject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should call injectBaseDecorator()', () => {
Expand Down Expand Up @@ -338,7 +348,10 @@ describe(inject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should throw handleInjectionError()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LazyServiceIdentifier, ServiceIdentifier } from '@inversifyjs/common';

import { decrementPendingClassMetadataCount } from '../actions/decrementPendingClassMetadataCount';
import { buildManagedMetadataFromMaybeClassElementMetadata } from '../calculations/buildManagedMetadataFromMaybeClassElementMetadata';
import { handleInjectionError } from '../calculations/handleInjectionError';
import { ClassElementMetadata } from '../models/ClassElementMetadata';
Expand All @@ -24,9 +25,16 @@ export function inject(
): void => {
try {
if (parameterIndex === undefined) {
injectBase(updateMetadata)(target, propertyKey as string | symbol);
injectBase(updateMetadata, decrementPendingClassMetadataCount)(
target,
propertyKey as string | symbol,
);
} else {
injectBase(updateMetadata)(target, propertyKey, parameterIndex);
injectBase(updateMetadata, decrementPendingClassMetadataCount)(
target,
propertyKey,
parameterIndex,
);
}
} catch (error: unknown) {
handleInjectionError(target, propertyKey, parameterIndex, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe(injectBase.name, () => {
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata
>;
let updatePendingClassMetadataCountMock: jest.Mock<
(
target: object,
) => (metadata: MaybeClassElementMetadata | undefined) => void
>;

beforeAll(() => {
updateMetadataMock =
Expand All @@ -37,6 +42,7 @@ describe(injectBase.name, () => {
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata
>();
updatePendingClassMetadataCountMock = jest.fn();
});

describe('when called, as property decorator', () => {
Expand All @@ -55,7 +61,7 @@ describe(injectBase.name, () => {
).mockReturnValueOnce(updateMaybeClassMetadataPropertyResult);

class TargetFixture {
@injectBase(updateMetadataMock)
@injectBase(updateMetadataMock, updatePendingClassMetadataCountMock)
public foo: string | undefined;
}

Expand Down Expand Up @@ -94,7 +100,7 @@ describe(injectBase.name, () => {

class TargetFixture {
constructor(
@injectBase(updateMetadataMock)
@injectBase(updateMetadataMock, updatePendingClassMetadataCountMock)
public foo: string | undefined,
) {}
}
Expand Down Expand Up @@ -125,7 +131,7 @@ describe(injectBase.name, () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class TargetFixture {
public doSomethingWithFoo(
@injectBase(updateMetadataMock)
@injectBase(updateMetadataMock, updatePendingClassMetadataCountMock)
foo: string | undefined,
) {
console.log(foo ?? '?');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,80 @@ export function injectBase(
updateMetadata: (
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata,
updatePendingClassMetadataCount: (
target: object,
) => (metadata: MaybeClassElementMetadata | undefined) => void,
): ParameterDecorator & PropertyDecorator {
const decorator: ParameterDecorator & PropertyDecorator = (
target: object,
propertyKey: string | symbol | undefined,
parameterIndex?: number,
): void => {
if (parameterIndex === undefined) {
injectProperty(updateMetadata)(target, propertyKey as string | symbol);
injectProperty(updateMetadata, updatePendingClassMetadataCount)(
target,
propertyKey as string | symbol,
);
} else {
injectParameter(updateMetadata)(target, propertyKey, parameterIndex);
injectParameter(updateMetadata, updatePendingClassMetadataCount)(
target,
propertyKey,
parameterIndex,
);
}
};

return decorator;
}

function buildComposedUpdateMetadata(
updateMetadata: (
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata,
updatePendingClassMetadataCount: (
target: object,
) => (metadata: MaybeClassElementMetadata | undefined) => void,
): (
target: object,
) => (
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata {
return (
target: object,
): ((
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata) => {
const updateTargetPendingClassMetadataCount: (
metadata: MaybeClassElementMetadata | undefined,
) => void = updatePendingClassMetadataCount(target);

return (
metadata: MaybeClassElementMetadata | undefined,
): MaybeClassElementMetadata => {
updateTargetPendingClassMetadataCount(metadata);

return updateMetadata(metadata);
};
};
}

function injectParameter(
updateMetadata: (
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata,
updatePendingClassMetadataCount: (
target: object,
) => (metadata: MaybeClassElementMetadata | undefined) => void,
): ParameterDecorator {
const buildComposedUpdateMetadataFromTarget: (
target: object,
) => (
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata = buildComposedUpdateMetadata(
updateMetadata,
updatePendingClassMetadataCount,
);

return (
target: object,
propertyKey: string | symbol | undefined,
Expand All @@ -42,7 +95,7 @@ function injectParameter(
classMetadataReflectKey,
getDefaultClassMetadata,
updateMaybeClassMetadataConstructorArgument(
updateMetadata,
buildComposedUpdateMetadataFromTarget(target),
parameterIndex,
),
);
Expand All @@ -61,13 +114,28 @@ function injectProperty(
updateMetadata: (
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata,
updatePendingClassMetadataCount: (
target: object,
) => (metadata: MaybeClassElementMetadata | undefined) => void,
): PropertyDecorator {
const buildComposedUpdateMetadataFromTarget: (
target: object,
) => (
metadata: MaybeClassElementMetadata | undefined,
) => MaybeClassElementMetadata = buildComposedUpdateMetadata(
updateMetadata,
updatePendingClassMetadataCount,
);

return (target: object, propertyKey: string | symbol): void => {
updateReflectMetadata(
target.constructor,
classMetadataReflectKey,
getDefaultClassMetadata,
updateMaybeClassMetadataProperty(updateMetadata, propertyKey),
updateMaybeClassMetadataProperty(
buildComposedUpdateMetadataFromTarget(target),
propertyKey,
),
);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jest.mock('../calculations/buildManagedMetadataFromMaybeClassElementMetadata');
jest.mock('../calculations/handleInjectionError');
jest.mock('./injectBase');

import { decrementPendingClassMetadataCount } from '../actions/decrementPendingClassMetadataCount';
import { buildManagedMetadataFromMaybeClassElementMetadata } from '../calculations/buildManagedMetadataFromMaybeClassElementMetadata';
import { handleInjectionError } from '../calculations/handleInjectionError';
import { ClassElementMetadata } from '../models/ClassElementMetadata';
Expand Down Expand Up @@ -84,7 +85,10 @@ describe(multiInject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should call injectBaseDecorator()', () => {
Expand Down Expand Up @@ -167,7 +171,10 @@ describe(multiInject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should throw handleInjectionError()', () => {
Expand Down Expand Up @@ -256,7 +263,10 @@ describe(multiInject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should call injectBaseDecorator()', () => {
Expand Down Expand Up @@ -341,7 +351,10 @@ describe(multiInject.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
decrementPendingClassMetadataCount,
);
});

it('should throw handleInjectionError()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LazyServiceIdentifier, ServiceIdentifier } from '@inversifyjs/common';

import { decrementPendingClassMetadataCount } from '../actions/decrementPendingClassMetadataCount';
import { buildManagedMetadataFromMaybeClassElementMetadata } from '../calculations/buildManagedMetadataFromMaybeClassElementMetadata';
import { handleInjectionError } from '../calculations/handleInjectionError';
import { ClassElementMetadata } from '../models/ClassElementMetadata';
Expand All @@ -24,9 +25,16 @@ export function multiInject(
): void => {
try {
if (parameterIndex === undefined) {
injectBase(updateMetadata)(target, propertyKey as string | symbol);
injectBase(updateMetadata, decrementPendingClassMetadataCount)(
target,
propertyKey as string | symbol,
);
} else {
injectBase(updateMetadata)(target, propertyKey, parameterIndex);
injectBase(updateMetadata, decrementPendingClassMetadataCount)(
target,
propertyKey,
parameterIndex,
);
}
} catch (error: unknown) {
handleInjectionError(target, propertyKey, parameterIndex, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock(
jest.mock('../calculations/handleInjectionError');
jest.mock('./injectBase');

import { incrementPendingClassMetadataCount } from '../actions/incrementPendingClassMetadataCount';
import { updateMetadataName } from '../actions/updateMetadataName';
import { buildMaybeClassElementMetadataFromMaybeClassElementMetadata } from '../calculations/buildMaybeClassElementMetadataFromMaybeClassElementMetadata';
import { handleInjectionError } from '../calculations/handleInjectionError';
Expand Down Expand Up @@ -94,7 +95,10 @@ describe(named.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
incrementPendingClassMetadataCount,
);
});

it('should call injectBaseDecorator()', () => {
Expand Down Expand Up @@ -173,7 +177,10 @@ describe(named.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
incrementPendingClassMetadataCount,
);
});

it('should throw handleInjectionError()', () => {
Expand Down Expand Up @@ -259,7 +266,10 @@ describe(named.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
incrementPendingClassMetadataCount,
);
});

it('should call injectBaseDecorator()', () => {
Expand Down Expand Up @@ -339,7 +349,10 @@ describe(named.name, () => {

it('should call injectBase()', () => {
expect(injectBase).toHaveBeenCalledTimes(1);
expect(injectBase).toHaveBeenCalledWith(updateMetadataMock);
expect(injectBase).toHaveBeenCalledWith(
updateMetadataMock,
incrementPendingClassMetadataCount,
);
});

it('should throw handleInjectionError()', () => {
Expand Down
Loading

0 comments on commit eb97fda

Please sign in to comment.