Skip to content

Commit

Permalink
Fix async to service binding resolution (#1635)
Browse files Browse the repository at this point in the history
* style: update test with right description

* fix: update BindingToSyntax.toService() to resolve async dependencies
  • Loading branch information
notaphplover authored Nov 14, 2024
1 parent af2ca0a commit 606232e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
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);
});
});

0 comments on commit 606232e

Please sign in to comment.