aboutsummaryrefslogtreecommitdiff
path: root/src/background/operators/impls/OperatorFactoryImpl.ts
blob: ce874913d03f9b9a3ff06648e2b79c68ddd078a7 (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
46
47
48
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 FindOperatorFactoryChain from "./FindOperatorFactoryChain";
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,
    findOperatorFactoryChain: FindOperatorFactoryChain,
    @inject(delay(() => RepeatOperatorFactoryChain))
    repeatOperatorFactoryChain: RepeatOperatorFactoryChain
  ) {
    this.factoryChains = [
      commandOperatorFactoryChain,
      internalOperatorFactoryChain,
      navigateOperatorFactoryChain,
      repeatOperatorFactoryChain,
      tabOperatorFactoryChain,
      zoomOperatorFactoryChain,
      findOperatorFactoryChain,
    ];
  }

  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);
  }
}