From 13b464db253566290be6a1063ad8e296288d3339 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 14 Sep 2023 03:05:28 +0200 Subject: feat: specify random seed for generation --- .../frontend/src/xtext/ModelGenerationService.ts | 26 +++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'subprojects/frontend/src/xtext/ModelGenerationService.ts') diff --git a/subprojects/frontend/src/xtext/ModelGenerationService.ts b/subprojects/frontend/src/xtext/ModelGenerationService.ts index 1e9f837a..29a70623 100644 --- a/subprojects/frontend/src/xtext/ModelGenerationService.ts +++ b/subprojects/frontend/src/xtext/ModelGenerationService.ts @@ -4,12 +4,18 @@ * SPDX-License-Identifier: EPL-2.0 */ +import type { Transaction } from '@codemirror/state'; + import type EditorStore from '../editor/EditorStore'; import type UpdateService from './UpdateService'; import { ModelGenerationResult } from './xtextServiceResults'; +const INITIAL_RANDOM_SEED = 1; + export default class ModelGenerationService { + private nextRandomSeed = INITIAL_RANDOM_SEED; + constructor( private readonly store: EditorStore, private readonly updateService: UpdateService, @@ -26,14 +32,24 @@ export default class ModelGenerationService { } } + onTransaction(transaction: Transaction): void { + if (transaction.docChanged) { + this.resetRandomSeed(); + } + } + onDisconnect(): void { this.store.modelGenerationCancelled(); + this.resetRandomSeed(); } - async start(): Promise { - const result = await this.updateService.startModelGeneration(); + async start(randomSeed?: number): Promise { + const randomSeedOrNext = randomSeed ?? this.nextRandomSeed; + this.nextRandomSeed = randomSeedOrNext + 1; + const result = + await this.updateService.startModelGeneration(randomSeedOrNext); if (!result.cancelled) { - this.store.addGeneratedModel(result.data.uuid); + this.store.addGeneratedModel(result.data.uuid, randomSeedOrNext); } } @@ -43,4 +59,8 @@ export default class ModelGenerationService { this.store.modelGenerationCancelled(); } } + + private resetRandomSeed() { + this.nextRandomSeed = INITIAL_RANDOM_SEED; + } } -- cgit v1.2.3-70-g09d2