aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/RootStore.ts
blob: e277c808549ba1a4f2cf7a34fca9a0165cb4d4f9 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { getLogger } from 'loglevel';
import { makeAutoObservable, runInAction } from 'mobx';

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

const log = getLogger('RootStore');

export default class RootStore {
  private readonly compressor = new Compressor((text) =>
    this.setInitialValue(text),
  );

  private initialValue: string | undefined;

  private editorStoreClass: typeof EditorStore | undefined;

  editorStore: EditorStore | undefined;

  readonly pwaStore: PWAStore;

  readonly themeStore: ThemeStore;

  disposed = false;

  constructor() {
    this.pwaStore = new PWAStore();
    this.themeStore = new ThemeStore();
    makeAutoObservable<RootStore, 'compressor' | 'editorStoreClass'>(this, {
      compressor: false,
      editorStoreClass: false,
      pwaStore: false,
      themeStore: false,
    });
    (async () => {
      const { default: EditorStore } = await import('./editor/EditorStore');
      runInAction(() => {
        if (this.disposed) {
          return;
        }
        this.editorStoreClass = EditorStore;
        if (this.initialValue !== undefined) {
          this.setInitialValue(this.initialValue);
        }
      });
    })().catch((error) => {
      log.error('Failed to load EditorStore', error);
    });
    this.compressor.decompressInitial();
  }

  private setInitialValue(initialValue: string): void {
    this.initialValue = initialValue;
    if (this.editorStoreClass !== undefined) {
      const EditorStore = this.editorStoreClass;
      this.editorStore = new EditorStore(
        this.initialValue,
        this.pwaStore,
        (text) => this.compressor.compress(text),
      );
    }
  }

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