aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/RootStore.ts
blob: 54a8050109cd8b547e0ce2d60bb3c803a248bae5 (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
import { getLogger } from 'loglevel';
import { makeAutoObservable, runInAction } from 'mobx';

import PWAStore from './PWAStore';
import type EditorStore from './editor/EditorStore';
import ThemeStore from './theme/ThemeStore';

const log = getLogger('RootStore');

export default class RootStore {
  editorStore: EditorStore | undefined;

  readonly pwaStore: PWAStore;

  readonly themeStore: ThemeStore;

  disposed = false;

  constructor(initialValue: string) {
    this.pwaStore = new PWAStore();
    this.themeStore = new ThemeStore();
    makeAutoObservable(this, {
      pwaStore: false,
      themeStore: false,
    });
    import('./editor/EditorStore')
      .then(({ default: EditorStore }) => {
        runInAction(() => {
          if (this.disposed) {
            return;
          }
          this.editorStore = new EditorStore(initialValue, this.pwaStore);
        });
      })
      .catch((error) => {
        log.error('Failed to load EditorStore', error);
      });
  }

  dispose(): void {
    if (this.disposed) {
      return;
    }
    this.editorStore?.dispose();
    this.disposed = true;
  }
}