From 8f92a4526ea62392dd749ed13cf28d9b6644840c Mon Sep 17 00:00:00 2001 From: notaphplover Date: Fri, 20 Dec 2024 14:18:31 +0100 Subject: [PATCH] feat(core): update Binding service with parent constructor parameter --- .../binding/services/BindingService.spec.ts | 101 ++++++++++++++---- .../src/binding/services/BindingService.ts | 9 +- .../planning/calculations/plan.int.spec.ts | 2 +- .../resolution/actions/resolve.int.spec.ts | 2 +- .../resolution/models/ResolutionContext.ts | 2 +- 5 files changed, 86 insertions(+), 30 deletions(-) diff --git a/packages/container/libraries/core/src/binding/services/BindingService.spec.ts b/packages/container/libraries/core/src/binding/services/BindingService.spec.ts index 617d6a4..faf9f62 100644 --- a/packages/container/libraries/core/src/binding/services/BindingService.spec.ts +++ b/packages/container/libraries/core/src/binding/services/BindingService.spec.ts @@ -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(); + }); }); }); }); @@ -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); @@ -138,7 +193,7 @@ describe(BindingService.name, () => { let bindingServiceImplementation: BindingService; beforeAll(() => { - bindingServiceImplementation = new BindingService(); + bindingServiceImplementation = new BindingService(undefined); bindingServiceImplementation.set(bindingFixture); bindingServiceImplementation.removeByModule( diff --git a/packages/container/libraries/core/src/binding/services/BindingService.ts b/packages/container/libraries/core/src/binding/services/BindingService.ts index f5e57be..04c66ca 100644 --- a/packages/container/libraries/core/src/binding/services/BindingService.ts +++ b/packages/container/libraries/core/src/binding/services/BindingService.ts @@ -8,18 +8,19 @@ export class BindingService { number, Map> >; + readonly #parent: BindingService | undefined; - constructor() { + constructor(parent: BindingService | undefined) { this.#idToBindingMap = new Map(); this.#moduleIdToIdToBindingMap = new Map(); + this.#parent = parent; } public get( serviceIdentifier: ServiceIdentifier, ): Binding[] | undefined { - return this.#idToBindingMap.get(serviceIdentifier) as - | Binding[] - | undefined; + return (this.#idToBindingMap.get(serviceIdentifier) ?? + this.#parent?.get(serviceIdentifier)) as Binding[] | undefined; } public remove(serviceIdentifier: ServiceIdentifier): void { diff --git a/packages/container/libraries/core/src/planning/calculations/plan.int.spec.ts b/packages/container/libraries/core/src/planning/calculations/plan.int.spec.ts index e0d3d63..4c1862e 100644 --- a/packages/container/libraries/core/src/planning/calculations/plan.int.spec.ts +++ b/packages/container/libraries/core/src/planning/calculations/plan.int.spec.ts @@ -304,7 +304,7 @@ describe(plan.name, () => { type: bindingTypeValues.ServiceRedirection, }; - bindingService = new BindingService(); + bindingService = new BindingService(undefined); bindingService.set(constantValueBinding); bindingService.set(dynamicValueBinding); diff --git a/packages/container/libraries/core/src/resolution/actions/resolve.int.spec.ts b/packages/container/libraries/core/src/resolution/actions/resolve.int.spec.ts index b673be3..8818b0d 100644 --- a/packages/container/libraries/core/src/resolution/actions/resolve.int.spec.ts +++ b/packages/container/libraries/core/src/resolution/actions/resolve.int.spec.ts @@ -198,7 +198,7 @@ describe(resolve.name, () => { }; activationService = new ActivationsService(); - bindingService = new BindingService(); + bindingService = new BindingService(undefined); activationService.add( constantValueBindingWithActivation.onActivation as BindingActivation, diff --git a/packages/container/libraries/core/src/resolution/models/ResolutionContext.ts b/packages/container/libraries/core/src/resolution/models/ResolutionContext.ts index 88bb722..71bc7e4 100644 --- a/packages/container/libraries/core/src/resolution/models/ResolutionContext.ts +++ b/packages/container/libraries/core/src/resolution/models/ResolutionContext.ts @@ -26,7 +26,7 @@ export interface ResolutionContext { getAsync( serviceIdentifier: ServiceIdentifier, options: OptionalGetOptions, - ): Promise | undefined; + ): Promise; getAsync( serviceIdentifier: ServiceIdentifier, options?: GetOptions,