Skip to content

Commit

Permalink
fix(injector): scoped token injected dependencies are not singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Jun 3, 2023
1 parent ce638c6 commit 4ef31d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
6 changes: 4 additions & 2 deletions spec/scoped-lifecycle-hook.test.ts
Expand Up @@ -5,6 +5,8 @@ import { Injectable, SCOPE } from '../src/injector/injectable/decorator.ts';
import { Module } from '../src/module/decorator.ts';
import { Controller, Get } from '../src/router/controller/decorator.ts';
import { HttpContext } from '../src/router/router.ts';
import { Inject } from "../src/injector/decorator.ts";
import { TokenInjector } from "../src/injector/injectable/constructor.ts";

Deno.test('Scoped Lifecycle hooks', async (testContext) => {
@Injectable({ scope: SCOPE.REQUEST })
Expand All @@ -22,15 +24,15 @@ Deno.test('Scoped Lifecycle hooks', async (testContext) => {
return this.child1.somethingThatMatters;
}
constructor(
public child1: ScopedInjectable,
@Inject('SCOPED_TOKEN') public child1: ScopedInjectable,
) {
}
}

@Module({
controllers: [ScopedController],
injectables: [
ScopedInjectable,
new TokenInjector(ScopedInjectable, 'SCOPED_TOKEN'),
],
})
class MyModule {}
Expand Down
34 changes: 3 additions & 31 deletions src/injector/injector.ts
Expand Up @@ -16,7 +16,6 @@ import {
} from './injectable/decorator.ts';
import { InjectableHelper } from './injectable/helper.ts';
import { HttpContext } from '../router/router.ts';
import { BeforeControllerMethodIsCalled } from '../hook/interfaces.ts';

export class Injector {
private resolved = new Map<
Expand Down Expand Up @@ -81,7 +80,8 @@ export class Injector {
let canBeSingleton = true;
const dependencies = this.getDependencies(Type);
dependencies.forEach((DependencyType, idx) => {
if (!this.resolved.has(this.getParamToken(Type, idx) ?? DependencyType)) {
const actualType = this.getParamToken(Type, idx) ?? DependencyType;
if (!this.resolved.has(actualType)) {
throw new Error(
`${Type.name} dependency ${
this.getParamToken(Type, idx) ?? DependencyType.name
Expand Down Expand Up @@ -126,7 +126,7 @@ export class Injector {

const injectableMetadata = MetadataHelper.getMetadata<InjectableOption>(
injectionData,
Type,
actualType,
);
await this.resolveDependencies(dependencies, actualType);
if (injectableMetadata?.scope !== SCOPE.REQUEST) {
Expand Down Expand Up @@ -206,32 +206,4 @@ export class Injector {
private getDependencies(Type: Constructor): Constructor[] {
return MetadataHelper.getMetadata('design:paramtypes', Type) || [];
}

private async executeOnAppBoostrapHook(
Controllers?: ControllerConstructor[],
injectables?: Array<InjectableConstructor | TokenInjector>,
) {
if (Controllers) {
for (const controller of Controllers) {
if (InjectableHelper.isGlobal(controller)) {
// deno-lint-ignore no-explicit-any
await (await this.get<any>(controller)).onAppBootstrap?.();
}
}
}
if (injectables) {
for (const injectable of injectables) {
const actualType = injectable instanceof TokenInjector
? injectable.useClass
: injectable;
const actualKey = injectable instanceof TokenInjector
? injectable.token
: injectable;
if (InjectableHelper.isGlobal(actualType)) {
// deno-lint-ignore no-explicit-any
await (await this.get<any>(actualKey)).onAppBootstrap?.();
}
}
}
}
}

0 comments on commit 4ef31d5

Please sign in to comment.