diff options
Diffstat (limited to 'src/stores/lib/Reaction.ts')
-rw-r--r-- | src/stores/lib/Reaction.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/stores/lib/Reaction.ts b/src/stores/lib/Reaction.ts new file mode 100644 index 000000000..0ca24a6fa --- /dev/null +++ b/src/stores/lib/Reaction.ts | |||
@@ -0,0 +1,29 @@ | |||
1 | import { autorun } from 'mobx'; | ||
2 | |||
3 | export default class Reaction { | ||
4 | reaction; | ||
5 | |||
6 | isRunning = false; | ||
7 | |||
8 | dispose; | ||
9 | |||
10 | constructor(reaction) { | ||
11 | this.reaction = reaction; | ||
12 | } | ||
13 | |||
14 | start() { | ||
15 | if (!this.isRunning) { | ||
16 | this.dispose = autorun(this.reaction); | ||
17 | this.isRunning = true; | ||
18 | } | ||
19 | } | ||
20 | |||
21 | stop() { | ||
22 | if (this.isRunning) { | ||
23 | this.dispose?.(); | ||
24 | this.isRunning = false; | ||
25 | } | ||
26 | } | ||
27 | } | ||
28 | |||
29 | export const createReactions = reactions => reactions.map(r => new Reaction(r)); | ||