aboutsummaryrefslogtreecommitdiff
path: root/src/content/operators/impls/find.ts
blob: 82310bc54755f8247ee6246f928ae0675406974e (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, injectable } from "tsyringe";
import RepeatOperator from "./RepeatOperator";
import Operator from "../Operator";
import OperatorFactoryChain from "../OperatorFactoryChain";
import FindMasterClient from "../../client/FindMasterClient";
import * as operations from "../../../shared/operations";

export class FindNextOperator implements Operator {
  constructor(private readonly findMasterClient: FindMasterClient) {}

  async run(): Promise<void> {
    this.findMasterClient.findNext();
  }
}

export class FindPrevOperator implements Operator {
  constructor(private readonly findMasterClient: FindMasterClient) {}

  async run(): Promise<void> {
    this.findMasterClient.findPrev();
  }
}

@injectable()
export class FindOperatorFactoryChain implements OperatorFactoryChain {
  constructor(
    @inject("FindMasterClient")
    private readonly findMasterClient: FindMasterClient
  ) {}
  create(op: operations.Operation, repeat: number): Operator | null {
    switch (op.type) {
      case operations.FIND_NEXT:
        return new RepeatOperator(
          new FindNextOperator(this.findMasterClient),
          repeat
        );
      case operations.FIND_PREV:
        return new RepeatOperator(
          new FindPrevOperator(this.findMasterClient),
          repeat
        );
    }
    return null;
  }
}