How does inRequestScope method works?

18 views Asked by At
// GameFactory.ts
export default class GameFactory {
  public static create(): AbstractGame {
    DependencyRegistrar.registerDependencies();
    return container.get<AbstractGame>(TYPES.Game);
  }
}
// AbstractGame.ts
export default abstract class AbstractGame {
   constructor(
    @inject(TYPES.Player)
    @named("localPlayer")
    protected localPlayer: AbstractPlayer,
    @inject(TYPES.Player)
    @named("opponent")
    protected opponent: AbstractPlayer,
  ) {}
}
// AbstractPlayer.ts
export default abstract class AbstractPlayer {
@inject(TYPES.InputManager) inputManager!: InputManager;
@inject(TYPES.CharacterController) protected charCtrl!: CharacterController;

constructor() {}
...
}
// CharacterController.ts
export default class CharacterController {
@inject(TYPES.InputManager) inputManager!: InputManager;

constructor() {}
...
}
// DependencyRegistrar.ts
export default class DependencyRegistrar {
  public static registerDependencies(): void {
    container
      .bind<InputManager>(TYPES.InputManager)
      .to(InputManager)

    container
      .bind<CharacterController>(TYPES.CharacterController)
      .to(CharacterController);

    container
      .bind<AbstractGame>(TYPES.Game)
      .to(Game)
      .inSingletonScope()

    container
      .bind<AbstractPlayer>(TYPES.Player)
      .to(LocalPlayer)
      .inSingletonScope()
      .whenTargetNamed("localPlayer");

    container
      .bind<AbstractPlayer>(TYPES.Player)
      .to(Opponent)
      .inSingletonScope()
      .whenTargetNamed("opponent");
  }
}

Hi. These are a portion of my bindings. My AbstractPlayer injects the InputManager and the CharacterController classes. Also, the CharacterController class injects the InputManager class and I want to get the same instance of InputManager that the AbstractPlayer class injects.

I tried using the inRequestScope method on the InputManager binding but it didn't work as expected and I'm getting the same instance of the InputManager for both the local player and the opponent.

0

There are 0 answers