aboutsummaryrefslogtreecommitdiff
path: root/src/content/operators/impls/find.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-11-28 13:25:47 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2020-12-09 23:08:59 +0900
commit809bdb11f3d213254c098411755631b1264c52f0 (patch)
treedf55fac613441001f357a047a9a17472f8ec9ede /src/content/operators/impls/find.ts
parentfd2055e2702251c856f60214f150c78d558dcbf7 (diff)
Make operators as a strategy pattern
Diffstat (limited to 'src/content/operators/impls/find.ts')
-rw-r--r--src/content/operators/impls/find.ts45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/content/operators/impls/find.ts b/src/content/operators/impls/find.ts
new file mode 100644
index 0000000..82310bc
--- /dev/null
+++ b/src/content/operators/impls/find.ts
@@ -0,0 +1,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;
+ }
+}