Skip to content

Commit 948a67a

Browse files
authored
fix: execution container not extends parent (#63)
* fix: not extends parent * refactor: excution container get * chore: lint * fix: scopeEscape not work
1 parent dff4294 commit 948a67a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/container.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ export default class Container implements ContainerType {
6767
public set(options: Partial<InjectableDefinition>) {
6868
if (options.id && !isUndefined(options.value)) {
6969
const md: InjectableMetadata = {
70+
...options,
7071
id: options.id,
71-
value: options.value,
7272
scope: options.scope ?? ScopeEnum.SINGLETON,
73-
type: options.type,
7473
};
7574
this.registry.set(md.id, md);
7675
/**

src/execution_container.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class ExecutionContainer extends Container {
1818
id: Identifier<T>,
1919
options: { noThrow?: boolean; defaultValue?: any } = {},
2020
): T {
21-
const md = this.getDefinition(id) ?? this.parent.getDefinition(id);
21+
const md = this.getDefinition(id);
2222
if (!md) {
2323
if (options.noThrow) {
2424
return options.defaultValue;
@@ -33,12 +33,24 @@ export default class ExecutionContainer extends Container {
3333
return value;
3434
}
3535

36+
public getDefinition<T = unknown>(id: Identifier<T>): InjectableMetadata<T> | undefined {
37+
return super.getDefinition(id) ?? this.parent.getDefinition(id);
38+
}
39+
40+
public getInjectableByTag(tag: string): any[] {
41+
let tags = super.getInjectableByTag(tag);
42+
if (!tags || tags.length === 0) {
43+
tags = this.parent.getInjectableByTag(tag);
44+
}
45+
return tags;
46+
}
47+
3648
public getCtx(): any {
3749
return this.ctx;
3850
}
3951

4052
public getHandler(name: string | symbol): HandlerFunction | undefined {
41-
return this.handlerMap.get(name) ?? this.parent.getHandler(name);
53+
return super.getHandler(name) ?? this.parent.getHandler(name);
4254
}
4355

4456
private setValue(md: InjectableMetadata, value: any) {

test/container.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ describe('container', () => {
120120
expect(execContainer.get('hasValue')).toBe('hello');
121121
expect(execContainer.get('hasValue')).toBe('hello');
122122
});
123+
124+
it('should get definition from parent', () => {
125+
const definition = execContainer.getDefinition(Phone);
126+
expect(definition).toBeDefined();
127+
});
123128
});
124129
});
125130

@@ -132,11 +137,16 @@ describe('container#tag', () => {
132137
describe('getInjectableByTag', () => {
133138
it('should get classes by tag', () => {
134139
const container = new Container('container#tag');
140+
const childContainer = new ExecutionContainer({}, container);
135141
container.set({ type: Foo });
136142
const clazzes = container.getInjectableByTag('controller');
137143
expect(clazzes.length).toBeGreaterThan(0);
138144
expect(clazzes[0]).toEqual(Foo);
139145

146+
const clazzes3 = childContainer.getInjectableByTag('controller');
147+
expect(clazzes3.length).toBeGreaterThan(0);
148+
expect(clazzes3[0]).toEqual(Foo);
149+
140150
const clazzes2 = container.getInjectableByTag('middleware');
141151
expect(clazzes2.length).toBeGreaterThan(0);
142152
expect(clazzes2[0]).toEqual(Foo);

0 commit comments

Comments
 (0)