aboutsummaryrefslogtreecommitdiff
path: root/src/background/operators/impls/OperatorFactoryImpl.ts
blob: 34e7bb538fdaad7450a54c091d2264d8ae440ccf (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { inject, delay, injectable } from "tsyringe";
import Operator from "../Operator";
import OperatorFactory from "../OperatorFactory";
import OperatorFactoryChain from "../OperatorFactoryChain";
import CommandOperatorFactoryChain from "./CommandOperatorFactoryChain";
import InternalOperatorFactoryChain from "./InternalOperatorFactoryChain";
import NavigateOperatorFactoryChain from "./NavigateOperatorFactoryChain";
import RepeatOperatorFactoryChain from "./RepeatOperatorFactoryChain";
import TabOperatorFactoryChain from "./TabOperatorFactoryChain";
import ZoomOperatorFactoryChain from "./ZoomOperatorFactoryChain";
import * as operations from "../../../shared/operations";

@injectable()
export class OperatorFactoryImpl implements OperatorFactory {
  private readonly factoryChains: OperatorFactoryChain[];

  constructor(
    commandOperatorFactoryChain: CommandOperatorFactoryChain,
    internalOperatorFactoryChain: InternalOperatorFactoryChain,
    navigateOperatorFactoryChain: NavigateOperatorFactoryChain,
    tabOperatorFactoryChain: TabOperatorFactoryChain,
    zoomOperatorFactoryChain: ZoomOperatorFactoryChain,
    @inject(delay(() => RepeatOperatorFactoryChain))
    repeatOperatorFactoryChain: RepeatOperatorFactoryChain
  ) {
    this.factoryChains = [
      commandOperatorFactoryChain,
      internalOperatorFactoryChain,
      navigateOperatorFactoryChain,
      repeatOperatorFactoryChain,
      tabOperatorFactoryChain,
      zoomOperatorFactoryChain,
    ];
  }

  create(op: operations.Operation): Operator {
    for (const chain of this.factoryChains) {
      const operator = chain.create(op);
      if (operator !== null) {
        return operator;
      }
    }
    throw new Error("unknown operation: " + op.type);
  }
}