aboutsummaryrefslogtreecommitdiff
path: root/src/content/operators/impls/OperatorFactoryImpl.ts
blob: 22b35c890c6ea8f77a77dfd6f294f19137cd52e6 (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
49
50
51
import { inject, injectable } from "tsyringe";
import OperatorFactory from "../OperatorFactory";
import BackgroundOperationOperator from "./BackgroundOperationOperator";
import Operator from "../Operator";
import OperatorFactoryChain from "../OperatorFactoryChain";
import { Operation } from "../../../shared/operations";
import OperationClient from "../../client/OperationClient";
import AddonOperatorFactoryChain from "./AddonOperatorFactoryChain";
import ClipboardOperatorFactoryChain from "./ClipboardOperatorFactoryChain";
import FindOperatorFactoryChain from "./FindOperatorFactoryChain";
import FocusOperatorFactoryChain from "./FocusOperatorFactoryChain";
import FollowOperatorFactoryChain from "./FollowOperatorFactoryChain";
import MarkOperatorFactoryChain from "./MarkOperatorFactoryChain";
import ScrollOperatorFactoryChain from "./ScrollOperatorFactoryChain";

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

  constructor(
    addonOperatorFactoryChain: AddonOperatorFactoryChain,
    clipboardOperatorFactoryChain: ClipboardOperatorFactoryChain,
    findOperatorFactoryChain: FindOperatorFactoryChain,
    focusOperatorFactoryChain: FocusOperatorFactoryChain,
    followOperatorFactoryChain: FollowOperatorFactoryChain,
    markOperatorFactoryChain: MarkOperatorFactoryChain,
    scrollOperatorFactoryChain: ScrollOperatorFactoryChain,
    @inject("OperationClient")
    private readonly operationClient: OperationClient
  ) {
    this.factoryChains = [
      addonOperatorFactoryChain,
      clipboardOperatorFactoryChain,
      findOperatorFactoryChain,
      focusOperatorFactoryChain,
      followOperatorFactoryChain,
      markOperatorFactoryChain,
      scrollOperatorFactoryChain,
    ];
  }

  create(op: Operation, repeat: number): Operator {
    for (const chain of this.factoryChains) {
      const operator = chain.create(op, repeat);
      if (operator != null) {
        return operator;
      }
    }
    return new BackgroundOperationOperator(this.operationClient, repeat, op);
  }
}