aboutsummaryrefslogtreecommitdiff
path: root/src/background/controllers/OperationController.ts
blob: 7e431533e79b4511d07ac0c66ba29288d62cf307 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { inject, injectable } from "tsyringe";
import * as operations from "../../shared/operations";
import OperatorFactory from "../operators/OperatorFactory";
import RepeatUseCase from "../usecases/RepeatUseCase";

@injectable()
export default class OperationController {
  constructor(
    private readonly repeatUseCase: RepeatUseCase,
    @inject("OperatorFactory")
    private readonly operatorFactory: OperatorFactory
  ) {}

  async exec(repeat: number, op: operations.Operation): Promise<void> {
    await this.doOperation(repeat, op);
    if (this.repeatUseCase.isRepeatable(op)) {
      this.repeatUseCase.storeLastOperation(op);
    }
  }

  private async doOperation(
    repeat: number,
    operation: operations.Operation
  ): Promise<void> {
    const operator = this.operatorFactory.create(operation);
    for (let i = 0; i < repeat; ++i) {
      // eslint-disable-next-line no-await-in-loop
      await operator.run();
    }
  }
}