Skip to content

Commit

Permalink
Merge pull request #201 from inversify/feat/update-activation-service…
Browse files Browse the repository at this point in the history
…-with-parent-constructor-param

Update ActivationService with parent constructor param
  • Loading branch information
notaphplover authored Dec 20, 2024
2 parents 4e6442b + eda94bc commit 2d7d9bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe(ActivationsService.name, () => {
OneToManyMapStar<BindingActivation, BindingActivationRelation>
>;

let parentActivationService: ActivationsService;
let activationsService: ActivationsService;

beforeAll(() => {
Expand All @@ -33,7 +34,9 @@ describe(ActivationsService.name, () => {
OneToManyMapStar<BindingActivation, BindingActivationRelation>
>;

activationsService = new ActivationsService();
parentActivationService = new ActivationsService(undefined);

activationsService = new ActivationsService(parentActivationService);
});

describe('.add', () => {
Expand Down Expand Up @@ -80,10 +83,18 @@ describe(ActivationsService.name, () => {
serviceIdFixture = 'service-identifier';
});

describe('when called, and activationMaps.get() returns undefined', () => {
describe('when called, and activationMaps.get() returns undefined and parent activationMaps.get() returns Iterable', () => {
let bindingActivationFixture: BindingActivation;

let result: unknown;

beforeAll(() => {
bindingActivationFixture = Symbol() as unknown as BindingActivation;

activationMapsMock.get
.mockReturnValueOnce(undefined)
.mockReturnValueOnce([bindingActivationFixture]);

result = activationsService.get(serviceIdFixture);
});

Expand All @@ -92,15 +103,21 @@ describe(ActivationsService.name, () => {
});

it('should call activationMaps.get()', () => {
expect(activationMapsMock.get).toHaveBeenCalledTimes(1);
expect(activationMapsMock.get).toHaveBeenCalledWith(
expect(activationMapsMock.get).toHaveBeenCalledTimes(2);
expect(activationMapsMock.get).toHaveBeenNthCalledWith(
1,
'serviceId',
serviceIdFixture,
);
expect(activationMapsMock.get).toHaveBeenNthCalledWith(
2,
'serviceId',
serviceIdFixture,
);
});

it('should return undefined', () => {
expect(result).toBeUndefined();
it('should return BindingActivation[]', () => {
expect(result).toStrictEqual([bindingActivationFixture]);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export class ActivationsService {
BindingActivationRelation
>;

constructor() {
readonly #parent: ActivationsService | undefined;

constructor(parent: ActivationsService | undefined) {
this.#activationMaps = new OneToManyMapStar<
BindingActivation,
BindingActivationRelation
Expand All @@ -31,6 +33,8 @@ export class ActivationsService {
isOptional: false,
},
});

this.#parent = parent;
}

public add(
Expand All @@ -43,17 +47,12 @@ export class ActivationsService {
public get(
serviceIdentifier: ServiceIdentifier,
): Iterable<BindingActivation> | undefined {
const bindings: Iterable<BindingActivation> | undefined =
return (
this.#activationMaps.get(
ActivationRelationKind.serviceId,
serviceIdentifier,
);

if (bindings === undefined) {
return undefined;
}

return bindings;
) ?? this.#parent?.get(serviceIdentifier)
);
}

public removeAllByModuleId(moduleId: number): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe(resolve.name, () => {
type: bindingTypeValues.ServiceRedirection,
};

activationService = new ActivationsService();
activationService = new ActivationsService(undefined);
bindingService = new BindingService(undefined);

activationService.add(
Expand Down

0 comments on commit 2d7d9bf

Please sign in to comment.