From 948a67a7601b027cb16417eca4c10c43eb523566 Mon Sep 17 00:00:00 2001 From: Jerrys2011 <1670303003@qq.com> Date: Thu, 22 Dec 2022 10:28:33 +0800 Subject: [PATCH] fix: execution container not extends parent (#63) * fix: not extends parent * refactor: excution container get * chore: lint * fix: scopeEscape not work --- src/container.ts | 3 +-- src/execution_container.ts | 16 ++++++++++++++-- test/container.test.ts | 10 ++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/container.ts b/src/container.ts index 2273663..85ab43f 100644 --- a/src/container.ts +++ b/src/container.ts @@ -67,10 +67,9 @@ export default class Container implements ContainerType { public set(options: Partial) { if (options.id && !isUndefined(options.value)) { const md: InjectableMetadata = { + ...options, id: options.id, - value: options.value, scope: options.scope ?? ScopeEnum.SINGLETON, - type: options.type, }; this.registry.set(md.id, md); /** diff --git a/src/execution_container.ts b/src/execution_container.ts index 0460b98..c703f1f 100644 --- a/src/execution_container.ts +++ b/src/execution_container.ts @@ -18,7 +18,7 @@ export default class ExecutionContainer extends Container { id: Identifier, options: { noThrow?: boolean; defaultValue?: any } = {}, ): T { - const md = this.getDefinition(id) ?? this.parent.getDefinition(id); + const md = this.getDefinition(id); if (!md) { if (options.noThrow) { return options.defaultValue; @@ -33,12 +33,24 @@ export default class ExecutionContainer extends Container { return value; } + public getDefinition(id: Identifier): InjectableMetadata | undefined { + return super.getDefinition(id) ?? this.parent.getDefinition(id); + } + + public getInjectableByTag(tag: string): any[] { + let tags = super.getInjectableByTag(tag); + if (!tags || tags.length === 0) { + tags = this.parent.getInjectableByTag(tag); + } + return tags; + } + public getCtx(): any { return this.ctx; } public getHandler(name: string | symbol): HandlerFunction | undefined { - return this.handlerMap.get(name) ?? this.parent.getHandler(name); + return super.getHandler(name) ?? this.parent.getHandler(name); } private setValue(md: InjectableMetadata, value: any) { diff --git a/test/container.test.ts b/test/container.test.ts index 20bd8f2..7a6bf51 100644 --- a/test/container.test.ts +++ b/test/container.test.ts @@ -120,6 +120,11 @@ describe('container', () => { expect(execContainer.get('hasValue')).toBe('hello'); expect(execContainer.get('hasValue')).toBe('hello'); }); + + it('should get definition from parent', () => { + const definition = execContainer.getDefinition(Phone); + expect(definition).toBeDefined(); + }); }); }); @@ -132,11 +137,16 @@ describe('container#tag', () => { describe('getInjectableByTag', () => { it('should get classes by tag', () => { const container = new Container('container#tag'); + const childContainer = new ExecutionContainer({}, container); container.set({ type: Foo }); const clazzes = container.getInjectableByTag('controller'); expect(clazzes.length).toBeGreaterThan(0); expect(clazzes[0]).toEqual(Foo); + const clazzes3 = childContainer.getInjectableByTag('controller'); + expect(clazzes3.length).toBeGreaterThan(0); + expect(clazzes3[0]).toEqual(Foo); + const clazzes2 = container.getInjectableByTag('middleware'); expect(clazzes2.length).toBeGreaterThan(0); expect(clazzes2[0]).toEqual(Foo);