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

Fix async to service binding resolution #1635

Merged
merged 2 commits into from
Nov 14, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

### Fixed
- Fixed container to properly resolve async `.toService` bindings.

## [6.1.4]

Expand Down
11 changes: 8 additions & 3 deletions src/syntax/binding_to_syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ class BindingToSyntax<T> implements interfaces.BindingToSyntax<T> {
}

public toService(service: interfaces.ServiceIdentifier<T>): void {
this.toDynamicValue((context: interfaces.Context) =>
context.container.get<T>(service),
);
this.toDynamicValue((context: interfaces.Context): T | Promise<T> => {
try {
return context.container.get<T>(service);
} catch (_error: unknown) {
// This is a performance degradation in this edge case, we do need to improve the internal resolution architecture in order to solve this properly.
return context.container.getAsync<T>(service);
}
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/bugs/issue_1518.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';

import { Container } from '../../src/inversify';

describe('Issue 1515', () => {
describe('Issue 1518', () => {
it('should not throw on deactivating undefined singleton values', () => {
const container: Container = new Container();
const symbol: symbol = Symbol.for('foo');
Expand Down
46 changes: 46 additions & 0 deletions test/bugs/issue_1564.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { Container, inject, injectable } from '../../src/inversify';

describe('Issue 1564', () => {
it('should not throw on getting async services bound using "toService"', async () => {
@injectable()
class Database {
constructor() {
console.log('new Database');
}
}

@injectable()
class Service1 {
constructor(@inject(Database) public database: Database) {
console.log('new Service1');
}
}

@injectable()
class Service2 {
constructor(@inject(Service1) public service1: Service1) {
console.log('new Service2');
}
}

const container: Container = new Container({ defaultScope: 'Request' });

container.bind(Database).toDynamicValue(async () => {
console.log('connecting to db...');
return new Database();
});

container.bind(Service1).toSelf();
container.bind(Service2).toSelf();

container.bind('services').toService(Service1);
container.bind('services').toService(Service2);

const result: unknown[] = await container.getAllAsync('services');

expect(result).to.have.length(2);
});
});