Skip to content

Commit

Permalink
Merge pull request #283 from inversify/fix/update-bindng-activation-w…
Browse files Browse the repository at this point in the history
…ith-missing-resolution-context

Update BindinActivation with missing context
  • Loading branch information
notaphplover authored Jan 11, 2025
2 parents 4211b61 + 2d74b3f commit 3d3983b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-kiwis-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": patch
---

Updated BindingActivation with missing `ResolutionContext` param
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { ResolutionContext } from '../../resolution/models/ResolutionContext';
import { Resolved } from '../../resolution/models/Resolved';

export type BindingActivation<T = unknown> = (injectable: T) => Resolved<T>;
export type BindingActivation<T = unknown> = (
context: ResolutionContext,
injectable: T,
) => Resolved<T>;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ jest.mock('./resolveBindingServiceActivations');

import { ConstantValueBindingFixtures } from '../../binding/fixtures/ConstantValueBindingFixtures';
import { ConstantValueBinding } from '../../binding/models/ConstantValueBinding';
import { ResolutionContext } from '../models/ResolutionContext';
import { ResolutionParams } from '../models/ResolutionParams';
import { resolveBindingActivations } from './resolveBindingActivations';
import { resolveBindingServiceActivations } from './resolveBindingServiceActivations';
Expand Down Expand Up @@ -73,6 +74,7 @@ describe(resolveBindingActivations.name, () => {
beforeAll(() => {
onActivationMock = jest.fn();
paramsMock = {
context: Symbol() as unknown as jest.Mocked<ResolutionContext>,
getActivations: jest.fn(),
getBindings: jest.fn(),
} as Partial<
Expand Down Expand Up @@ -113,6 +115,14 @@ describe(resolveBindingActivations.name, () => {
jest.clearAllMocks();
});

it('should call binding.onActivation()', () => {
expect(onActivationMock).toHaveBeenCalledTimes(1);
expect(onActivationMock).toHaveBeenCalledWith(
paramsMock.context,
resolvedValue,
);
});

it('should call resolveBindingServiceActivations()', () => {
expect(resolveBindingServiceActivations).toHaveBeenCalledTimes(1);
expect(resolveBindingServiceActivations).toHaveBeenCalledWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export function resolveBindingActivations<TActivated>(
if (isPromise(activationResult)) {
activationResult = activationResult.then(
(resolved: SyncResolved<TActivated>): Resolved<TActivated> =>
onActivation(resolved),
onActivation(params.context, resolved),
);
} else {
activationResult = onActivation(activationResult);
activationResult = onActivation(params.context, activationResult);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals';
import { ServiceIdentifier } from '@inversifyjs/common';

import { BindingActivation } from '../../binding/models/BindingActivation';
import { ResolutionContext } from '../models/ResolutionContext';
import { ResolutionParams } from '../models/ResolutionParams';
import { resolveBindingServiceActivations } from './resolveBindingServiceActivations';

Expand All @@ -14,6 +15,7 @@ describe(resolveBindingServiceActivations.name, () => {

beforeAll(() => {
paramsMock = {
context: Symbol() as unknown as jest.Mocked<ResolutionContext>,
getActivations: jest.fn(),
} as Partial<
jest.Mocked<ResolutionParams>
Expand Down Expand Up @@ -82,7 +84,10 @@ describe(resolveBindingServiceActivations.name, () => {

it('should call activation', () => {
expect(activationMock).toHaveBeenCalledTimes(1);
expect(activationMock).toHaveBeenCalledWith(valueFixture);
expect(activationMock).toHaveBeenCalledWith(
paramsMock.context,
valueFixture,
);
});

it('should return value', () => {
Expand Down Expand Up @@ -125,7 +130,10 @@ describe(resolveBindingServiceActivations.name, () => {

it('should call activation', () => {
expect(activationMock).toHaveBeenCalledTimes(1);
expect(activationMock).toHaveBeenCalledWith(valueFixture);
expect(activationMock).toHaveBeenCalledWith(
paramsMock.context,
valueFixture,
);
});

it('should return value', () => {
Expand Down Expand Up @@ -209,7 +217,10 @@ describe(resolveBindingServiceActivations.name, () => {

it('should call activation', () => {
expect(activationMock).toHaveBeenCalledTimes(1);
expect(activationMock).toHaveBeenCalledWith(valueFixture);
expect(activationMock).toHaveBeenCalledWith(
paramsMock.context,
valueFixture,
);
});

it('should return value', () => {
Expand Down Expand Up @@ -252,7 +263,10 @@ describe(resolveBindingServiceActivations.name, () => {

it('should call activation', () => {
expect(activationMock).toHaveBeenCalledTimes(1);
expect(activationMock).toHaveBeenCalledWith(valueFixture);
expect(activationMock).toHaveBeenCalledWith(
paramsMock.context,
valueFixture,
);
});

it('should return value', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ export function resolveBindingServiceActivations<TActivated>(

if (isPromise(value)) {
return resolveBindingActivationsFromIteratorAsync(
params,
value,
activations[Symbol.iterator](),
);
}

return resolveBindingActivationsFromIterator(
params,
value,
activations[Symbol.iterator](),
);
}

function resolveBindingActivationsFromIterator<TActivated>(
params: ResolutionParams,
value: SyncResolved<TActivated>,
activationsIterator: Iterator<BindingActivation<TActivated>>,
): Resolved<TActivated> {
Expand All @@ -40,10 +43,11 @@ function resolveBindingActivationsFromIterator<TActivated>(

while (activationIteratorResult.done !== true) {
const nextActivatedValue: Resolved<TActivated> =
activationIteratorResult.value(activatedValue);
activationIteratorResult.value(params.context, activatedValue);

if (isPromise(nextActivatedValue)) {
return resolveBindingActivationsFromIteratorAsync(
params,
nextActivatedValue,
activationsIterator,
);
Expand All @@ -58,6 +62,7 @@ function resolveBindingActivationsFromIterator<TActivated>(
}

async function resolveBindingActivationsFromIteratorAsync<TActivated>(
params: ResolutionParams,
value: Promise<TActivated>,
activationsIterator: Iterator<BindingActivation<TActivated>>,
): Promise<SyncResolved<TActivated>> {
Expand All @@ -67,7 +72,10 @@ async function resolveBindingActivationsFromIteratorAsync<TActivated>(
activationsIterator.next();

while (activationIteratorResult.done !== true) {
activatedValue = await activationIteratorResult.value(activatedValue);
activatedValue = await activationIteratorResult.value(
params.context,
activatedValue,
);

activationIteratorResult = activationsIterator.next();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ResolutionContext } from '@inversifyjs/core';

import { Weapon } from '../models/Weapon';

export function upgradeWeapon(weapon: Weapon): Weapon {
export function upgradeWeapon(
_context: ResolutionContext,
weapon: Weapon,
): Weapon {
weapon.damage += 2;

return weapon;
Expand Down

0 comments on commit 3d3983b

Please sign in to comment.