aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/lib/Reaction.js
blob: f8009b7f63e932df5aeb2491d5d21367d6e48508 (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
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 = false;
    }
  }
}

export const createReactions = reactions => (
  reactions.map(r => new Reaction(r))
);