diff options
Diffstat (limited to 'subprojects/frontend/src/xtext/ModelGenerationService.ts')
-rw-r--r-- | subprojects/frontend/src/xtext/ModelGenerationService.ts | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/subprojects/frontend/src/xtext/ModelGenerationService.ts b/subprojects/frontend/src/xtext/ModelGenerationService.ts new file mode 100644 index 00000000..29a70623 --- /dev/null +++ b/subprojects/frontend/src/xtext/ModelGenerationService.ts | |||
@@ -0,0 +1,66 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | |||
7 | import type { Transaction } from '@codemirror/state'; | ||
8 | |||
9 | import type EditorStore from '../editor/EditorStore'; | ||
10 | |||
11 | import type UpdateService from './UpdateService'; | ||
12 | import { ModelGenerationResult } from './xtextServiceResults'; | ||
13 | |||
14 | const INITIAL_RANDOM_SEED = 1; | ||
15 | |||
16 | export default class ModelGenerationService { | ||
17 | private nextRandomSeed = INITIAL_RANDOM_SEED; | ||
18 | |||
19 | constructor( | ||
20 | private readonly store: EditorStore, | ||
21 | private readonly updateService: UpdateService, | ||
22 | ) {} | ||
23 | |||
24 | onPush(push: unknown): void { | ||
25 | const result = ModelGenerationResult.parse(push); | ||
26 | if ('status' in result) { | ||
27 | this.store.setGeneratedModelMessage(result.uuid, result.status); | ||
28 | } else if ('error' in result) { | ||
29 | this.store.setGeneratedModelError(result.uuid, result.error); | ||
30 | } else { | ||
31 | this.store.setGeneratedModelSemantics(result.uuid, result); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | onTransaction(transaction: Transaction): void { | ||
36 | if (transaction.docChanged) { | ||
37 | this.resetRandomSeed(); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | onDisconnect(): void { | ||
42 | this.store.modelGenerationCancelled(); | ||
43 | this.resetRandomSeed(); | ||
44 | } | ||
45 | |||
46 | async start(randomSeed?: number): Promise<void> { | ||
47 | const randomSeedOrNext = randomSeed ?? this.nextRandomSeed; | ||
48 | this.nextRandomSeed = randomSeedOrNext + 1; | ||
49 | const result = | ||
50 | await this.updateService.startModelGeneration(randomSeedOrNext); | ||
51 | if (!result.cancelled) { | ||
52 | this.store.addGeneratedModel(result.data.uuid, randomSeedOrNext); | ||
53 | } | ||
54 | } | ||
55 | |||
56 | async cancel(): Promise<void> { | ||
57 | const result = await this.updateService.cancelModelGeneration(); | ||
58 | if (!result.cancelled) { | ||
59 | this.store.modelGenerationCancelled(); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | private resetRandomSeed() { | ||
64 | this.nextRandomSeed = INITIAL_RANDOM_SEED; | ||
65 | } | ||
66 | } | ||