aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/editor/GeneratedModelStore.ts
blob: f2695d9a38e24a77edfcd1c90f279342a94ec481 (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
/*
 * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { makeAutoObservable } from 'mobx';

import GraphStore from '../graph/GraphStore';
import type { SemanticsSuccessResult } from '../xtext/xtextServiceResults';

import type EditorStore from './EditorStore';

export default class GeneratedModelStore {
  title: string;

  message = 'Waiting for server';

  error = false;

  graph: GraphStore | undefined;

  constructor(
    randomSeed: number,
    private readonly editorStore: EditorStore,
  ) {
    const time = new Date().toLocaleTimeString(undefined, { hour12: false });
    this.title = `Generated at ${time} (${randomSeed})`;
    makeAutoObservable<GeneratedModelStore, 'editorStore'>(this, {
      editorStore: false,
    });
  }

  get running(): boolean {
    return !this.error && this.graph === undefined;
  }

  setMessage(message: string): void {
    if (this.running) {
      this.message = message;
    }
  }

  setError(message: string): void {
    if (this.running) {
      this.error = true;
      this.message = message;
    }
  }

  setSemantics(semantics: SemanticsSuccessResult): void {
    if (this.running) {
      this.graph = new GraphStore(this.editorStore);
      this.graph.setSemantics(semantics);
    }
  }
}