Skip to content

Commit

Permalink
Resolves inversify#1409
Browse files Browse the repository at this point in the history
  • Loading branch information
AGrzes committed Feb 18, 2022
1 parent 8f3fd57 commit 6b53b78
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ class Container implements interfaces.Container {
return this._get<T>(getArgs) as Promise<T> | T;
}

public getMaybeAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): Promise<T> | T {
const getArgs = this._getNotAllArgs(serviceIdentifier, false);

return this._get<T>(getArgs) as Promise<T> | T;
}

public getTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T {
const getArgs = this._getNotAllArgs(serviceIdentifier, false, key, value);

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ namespace interfaces {
getAllTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T[];
getAllNamed<T>(serviceIdentifier: ServiceIdentifier<T>, named: string | number | symbol): T[];
getAsync<T>(serviceIdentifier: ServiceIdentifier<T>): Promise<T>;
getMaybeAsync<T>(serviceIdentifier: ServiceIdentifier<T>): Promise<T> | T
getNamedAsync<T>(serviceIdentifier: ServiceIdentifier<T>, named: string | number | symbol): Promise<T>;
getTaggedAsync<T>(serviceIdentifier: ServiceIdentifier<T>, key: string | number | symbol, value: unknown): Promise<T>;
getAllAsync<T>(serviceIdentifier: ServiceIdentifier<T>): Promise<T[]>;
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/binding_to_syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class BindingToSyntax<T> implements interfaces.BindingToSyntax<T> {

public toService(service: string | symbol | interfaces.Newable<T> | interfaces.Abstract<T>): void {
this.toDynamicValue(
(context) => context.container.get<T>(service)
(context) => context.container.getMaybeAsync(service)
);
}

Expand Down
18 changes: 18 additions & 0 deletions test/resolution/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2566,4 +2566,22 @@ describe("Resolve", () => {
expect(serviceFromGetAsync).eql(asyncServiceDynamicResolvedValue);
expect(serviceFromGet).eql(asyncServiceDynamicResolvedValue);
});

it("Should resolve service with async @postConstruct()", async () => {
@injectable()
class B {
@postConstruct()
async init() {
//
}
}

const container = new Container()
container.bind(B).toSelf().inSingletonScope()
container.bind('X').toService(B)

const x = await container.getAsync('X');
const b = await container.getAsync(B);
expect(b).eql(x)
})
});

0 comments on commit 6b53b78

Please sign in to comment.