aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/lib/Reaction.js
blob: b123ec01c1372e1739cc68b49e0b26b6ea4db2c8 (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
// @flow
import { autorun } from 'mobx';

export default class Reaction {
  reaction;

  isRunning = false;

  dispose;

  constructor(reaction) {
    this.reaction = reaction;
  }

  start() {
    if (!this.isRunning) {
      this.dispose = autorun(() => this.reaction());
      this.isRunning = true;
    }
  }

  stop() {
    if (this.isRunning) {
      this.dispose();
      this.isRunning = true;
    }
  }
}