Skip to content

Commit

Permalink
fix(injector): inject as singleton when scope !== REQUEST by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Oct 13, 2022
1 parent e7f8a3c commit 0918ffd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion spec/injection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { HttpContext } from '../src/router/router.ts';
Deno.test('Injection', async (testContext) => {
interface IDBService {
data: string;
id: string;
}

@Injectable()
Expand Down Expand Up @@ -49,8 +50,9 @@ Deno.test('Injection', async (testContext) => {
@Injectable()
class DatabaseService implements IDBService {
public data = 'coucou';

public id = crypto.randomUUID();
constructor() {
console.log('we construct');
}
}

Expand Down Expand Up @@ -153,6 +155,7 @@ Deno.test('Injection', async (testContext) => {
const singletonController = await app.get(SingletonController)!;
assertInstanceOf(singletonController.child2, GlobalInjectable);
assertInstanceOf(singletonController.dbService, DatabaseService);
assertEquals(firstController.child1.dbService.id, singletonController.dbService.id);
},
);

Expand Down
2 changes: 1 addition & 1 deletion src/injector/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class Injector {
Type,
);
await this.resolveDependencies(dependencies, actualType);
if (injectableMetadata?.scope === SCOPE.GLOBAL) {
if (injectableMetadata?.scope !== SCOPE.REQUEST) {
const resolvedDependencies = new Array<Constructor>();
for (const [idx, Dep] of dependencies.entries()) {
resolvedDependencies.push(
Expand Down

0 comments on commit 0918ffd

Please sign in to comment.