aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/lib/TypedStore.ts
blob: 8bae529bae0dbdd2d0b8e45cf67aad77dd693978 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
  action,
  computed,
  IReactionPublic,
  makeObservable,
  observable,
} from 'mobx';
import { Actions } from '../../actions/lib/actions';
import { ApiInterface } from '../../api';
import { Stores } from '../../@types/stores.types';
import Reaction from './Reaction';

export default abstract class TypedStore {
  _reactions: Reaction[] = [];

  @observable _status: any = null;

  stores: Stores;

  api: ApiInterface;

  actions: Actions;

  @action _setResetStatus() {
    this._status = null;
  }

  @computed get actionStatus() {
    return this._status || [];
  }

  set actionStatus(status) {
    this._status = status;
  }

  constructor(stores: Stores, api: ApiInterface, actions: Actions) {
    makeObservable(this);

    this.stores = stores;
    this.api = api;
    this.actions = actions;
  }

  registerReactions(reactions: ((r: IReactionPublic) => void)[]): void {
    for (const reaction of reactions) {
      this._reactions.push(new Reaction(reaction));
    }
  }

  public abstract setup(): void;

  initialize(): void {
    this.setup();
    for (const reaction of this._reactions) reaction.start();
  }

  teardown(): void {
    for (const reaction of this._reactions) reaction.stop();
  }

  resetStatus(): void {
    this._setResetStatus();
  }
}