Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow toService to be bound to async service #1441

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2597,4 +2597,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)
})
});