Skip to content

Commit

Permalink
Merge pull request #200 from inversify/feat/update-binding-service-wi…
Browse files Browse the repository at this point in the history
…th-parent-constructor-param

Update BindingService with parent constructor parameter
  • Loading branch information
notaphplover authored Dec 20, 2024
2 parents 45a5cd2 + 8f92a45 commit 4e6442b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,96 @@ describe(BindingService.name, () => {
};
});

describe('when called, with existing bindings', () => {
let bindingServiceImplementation: BindingService;
describe('having a BindingService with existing bindings with no parent', () => {
describe('when called', () => {
let bindingServiceImplementation: BindingService;

let result: unknown;
let result: unknown;

beforeAll(() => {
bindingServiceImplementation = new BindingService();
beforeAll(() => {
bindingServiceImplementation = new BindingService(undefined);

bindingServiceImplementation.set(bindingFixture);
bindingServiceImplementation.set(bindingFixture);

result = bindingServiceImplementation.get(
bindingFixture.serviceIdentifier,
);
result = bindingServiceImplementation.get(
bindingFixture.serviceIdentifier,
);
});

it('should return Binding[]', () => {
expect(result).toStrictEqual([bindingFixture]);
});
});
});

it('should return Binding[]', () => {
expect(result).toStrictEqual([bindingFixture]);
describe('having a BindingService with non existing bindings with no parent', () => {
describe('when called', () => {
let bindingServiceImplementation: BindingService;

let result: unknown;

beforeAll(() => {
bindingServiceImplementation = new BindingService(undefined);

result = bindingServiceImplementation.get(
bindingFixture.serviceIdentifier,
);
});

it('should return undefined', () => {
expect(result).toBeUndefined();
});
});
});

describe('when called, with non existing bindings', () => {
let bindingServiceImplementation: BindingService;
describe('having a BindingService with parent with existing bindings', () => {
describe('when called', () => {
let bindingServiceImplementation: BindingService;
let parentBindingServiceImplementation: BindingService;

let result: unknown;
let result: unknown;

beforeAll(() => {
bindingServiceImplementation = new BindingService();
beforeAll(() => {
parentBindingServiceImplementation = new BindingService(undefined);
parentBindingServiceImplementation.set(bindingFixture);

result = bindingServiceImplementation.get(
bindingFixture.serviceIdentifier,
);
bindingServiceImplementation = new BindingService(
parentBindingServiceImplementation,
);

result = bindingServiceImplementation.get(
bindingFixture.serviceIdentifier,
);
});

it('should return Binding[]', () => {
expect(result).toStrictEqual([bindingFixture]);
});
});
});

describe('having a BindingService with parent with non existing bindings', () => {
describe('when called, with non existing bindings', () => {
let bindingServiceImplementation: BindingService;
let parentBindingServiceImplementation: BindingService;

it('should return undefined', () => {
expect(result).toBeUndefined();
let result: unknown;

beforeAll(() => {
parentBindingServiceImplementation = new BindingService(undefined);

bindingServiceImplementation = new BindingService(
parentBindingServiceImplementation,
);

result = bindingServiceImplementation.get(
bindingFixture.serviceIdentifier,
);
});

it('should return undefined', () => {
expect(result).toBeUndefined();
});
});
});
});
Expand Down Expand Up @@ -91,7 +146,7 @@ describe(BindingService.name, () => {
let bindingServiceImplementation: BindingService;

beforeAll(() => {
bindingServiceImplementation = new BindingService();
bindingServiceImplementation = new BindingService(undefined);

bindingServiceImplementation.set(bindingFixture);
bindingServiceImplementation.remove(bindingFixture.serviceIdentifier);
Expand Down Expand Up @@ -138,7 +193,7 @@ describe(BindingService.name, () => {
let bindingServiceImplementation: BindingService;

beforeAll(() => {
bindingServiceImplementation = new BindingService();
bindingServiceImplementation = new BindingService(undefined);

bindingServiceImplementation.set(bindingFixture);
bindingServiceImplementation.removeByModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ export class BindingService {
number,
Map<number, Binding<unknown>>
>;
readonly #parent: BindingService | undefined;

constructor() {
constructor(parent: BindingService | undefined) {
this.#idToBindingMap = new Map();
this.#moduleIdToIdToBindingMap = new Map();
this.#parent = parent;
}

public get<TInstance>(
serviceIdentifier: ServiceIdentifier,
): Binding<TInstance>[] | undefined {
return this.#idToBindingMap.get(serviceIdentifier) as
| Binding<TInstance>[]
| undefined;
return (this.#idToBindingMap.get(serviceIdentifier) ??
this.#parent?.get(serviceIdentifier)) as Binding<TInstance>[] | undefined;
}

public remove(serviceIdentifier: ServiceIdentifier): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe(plan.name, () => {
type: bindingTypeValues.ServiceRedirection,
};

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

bindingService.set(constantValueBinding);
bindingService.set(dynamicValueBinding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe(resolve.name, () => {
};

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

activationService.add(
constantValueBindingWithActivation.onActivation as BindingActivation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ResolutionContext {
getAsync<TActivated>(
serviceIdentifier: ServiceIdentifier<TActivated>,
options: OptionalGetOptions,
): Promise<TActivated> | undefined;
): Promise<TActivated | undefined>;
getAsync<TActivated>(
serviceIdentifier: ServiceIdentifier<TActivated>,
options?: GetOptions,
Expand Down

0 comments on commit 4e6442b

Please sign in to comment.