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

import { runInAction } from 'mobx';

import type EditorStore from '../editor/EditorStore';

import type ValidationService from './ValidationService';
import { SemanticsResult } from './xtextServiceResults';

export default class SemanticsService {
  constructor(
    private readonly store: EditorStore,
    private readonly validationService: ValidationService,
  ) {}

  onPush(push: unknown): void {
    const result = SemanticsResult.parse(push);
    runInAction(() => {
      if ('issues' in result && result.issues !== undefined) {
        this.validationService.setSemanticsIssues(result.issues);
      } else {
        this.validationService.setSemanticsIssues([]);
      }
      if ('error' in result) {
        this.store.setSemanticsError(result.error);
      } else {
        this.store.setSemanticsError(undefined);
      }
      if ('model' in result && result.model !== undefined) {
        this.store.setSemantics(result.model);
      }
      this.store.analysisCompleted();
    });
  }
}