Skip to content

Commit

Permalink
Merge pull request #240 from inversify/feat/update-container-binding-…
Browse files Browse the repository at this point in the history
…to-rely-on-star-data-structure

Update BindingService
  • Loading branch information
notaphplover authored Jan 4, 2025
2 parents 7d00022 + 11b499a commit dec391e
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 271 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-fireants-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": major
---

Renamed `BindingService.remove` to `removeAllByServiceId`.
5 changes: 5 additions & 0 deletions .changeset/red-ads-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": major
---

Renamed `BindingService.removeByModule` to `removeAllByModuleId`.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe(Container.name, () => {
> as jest.Mocked<ActivationsService>;
bindingServiceMock = {
get: jest.fn(),
remove: jest.fn(),
removeAllByServiceId: jest.fn(),
set: jest.fn(),
} as Partial<jest.Mocked<BindingService>> as jest.Mocked<BindingService>;
deactivationServiceMock = {
Expand Down Expand Up @@ -996,9 +996,11 @@ describe(Container.name, () => {
);
});

it('should call bindingService.remove()', () => {
expect(bindingServiceMock.remove).toHaveBeenCalledTimes(1);
expect(bindingServiceMock.remove).toHaveBeenCalledWith(
it('should call bindingService.removeAllByServiceId()', () => {
expect(bindingServiceMock.removeAllByServiceId).toHaveBeenCalledTimes(
1,
);
expect(bindingServiceMock.removeAllByServiceId).toHaveBeenCalledWith(
serviceIdentifierFixture,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
BindingScope,
bindingScopeValues,
BindingService,
DeactivationParams,
DeactivationsService,
getClassMetadata,
GetOptions,
Expand Down Expand Up @@ -175,7 +176,7 @@ export class Container {
serviceIdentifier: ServiceIdentifier,
options?: IsBoundOptions,
): boolean {
const bindings: Binding<unknown>[] | undefined =
const bindings: Iterable<Binding<unknown>> | undefined =
this.#bindingService.get(serviceIdentifier);

if (bindings === undefined) {
Expand All @@ -193,9 +194,13 @@ export class Container {
bindingMetadata.tags.set(options.tag.key, options.tag.value);
}

return bindings.some((binding: Binding): boolean =>
binding.isSatisfiedBy(bindingMetadata),
);
for (const binding of bindings) {
if (binding.isSatisfiedBy(bindingMetadata)) {
return true;
}
}

return false;
}

public async load(...modules: ContainerModule[]): Promise<void> {
Expand Down Expand Up @@ -227,21 +232,12 @@ export class Container {

public async unbind(serviceIdentifier: ServiceIdentifier): Promise<void> {
await resolveServiceDeactivations(
{
getBindings: <TInstance>(
serviceIdentifier: ServiceIdentifier<TInstance>,
): Binding<TInstance>[] | undefined =>
this.#bindingService.get(serviceIdentifier),
getClassMetadata,
getDeactivations: <TActivated>(
serviceIdentifier: ServiceIdentifier<TActivated>,
) => this.#deactivationService.get(serviceIdentifier),
},
this.#buildDeactivationParams(),
serviceIdentifier,
);

this.#activationService.removeAllByServiceId(serviceIdentifier);
this.#bindingService.remove(serviceIdentifier);
this.#bindingService.removeAllByServiceId(serviceIdentifier);
this.#deactivationService.removeAllByServiceId(serviceIdentifier);
}

Expand Down Expand Up @@ -284,6 +280,19 @@ export class Container {
};
}

#buildDeactivationParams(): DeactivationParams {
return {
getBindings: <TInstance>(
serviceIdentifier: ServiceIdentifier<TInstance>,
): Iterable<Binding<TInstance>> | undefined =>
this.#bindingService.get(serviceIdentifier),
getClassMetadata,
getDeactivations: <TActivated>(
serviceIdentifier: ServiceIdentifier<TActivated>,
) => this.#deactivationService.get(serviceIdentifier),
};
}

#buildPlanResult(
isMultiple: boolean,
serviceIdentifier: ServiceIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ export class ConstantValueBindingFixtures {
},
};
}

public static get withModuleIdUndefined(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
moduleId: undefined,
};
}

public static get withModuleId(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
moduleId: 1,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe(ActivationsService.name, () => {
});
});

describe('.removeAllByModule', () => {
describe('.removeAllByModuleId', () => {
let moduleIdFixture: number;

beforeAll(() => {
Expand Down
Loading

0 comments on commit dec391e

Please sign in to comment.