Skip to content

Commit

Permalink
fix: execution container not extends parent (#63)
Browse files Browse the repository at this point in the history
* fix: not extends parent

* refactor: excution container get

* chore: lint

* fix: scopeEscape not work
  • Loading branch information
JerrysShan authored Dec 22, 2022
1 parent dff4294 commit 948a67a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ export default class Container implements ContainerType {
public set(options: Partial<InjectableDefinition>) {
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);
/**
Expand Down
16 changes: 14 additions & 2 deletions src/execution_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class ExecutionContainer extends Container {
id: Identifier<T>,
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;
Expand All @@ -33,12 +33,24 @@ export default class ExecutionContainer extends Container {
return value;
}

public getDefinition<T = unknown>(id: Identifier<T>): InjectableMetadata<T> | 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) {
Expand Down
10 changes: 10 additions & 0 deletions test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand All @@ -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);
Expand Down

0 comments on commit 948a67a

Please sign in to comment.