From 9249897d27869d109c94863f38ba4171007f50c6 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:18:13 +0200 Subject: [PATCH] Prevents race conditions in timed prompts Adds a flag to prevent the timed prompt execution from running concurrently, avoiding potential race conditions and unexpected behavior. Also, addresses edge case in the status check where the displayed current prompt number could be inaccurate. --- commands/timedPrompts.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/commands/timedPrompts.ts b/commands/timedPrompts.ts index e9fc75a..8e8077e 100644 --- a/commands/timedPrompts.ts +++ b/commands/timedPrompts.ts @@ -9,15 +9,22 @@ export class TimedPromptsCommand { private activeInterval: number | null = null; private promptQueue: string[] = []; private currentIndex = 0; + private inProgress = false; constructor(private promptGenerator: PromptGeneratorService) {} // MARK: - Public Methods async execute(settings: FreewritingPromptsSettings): Promise { + // Check if already in progress to prevent race conditions + if (this.inProgress) { + return; + } + // Stop any existing timed sequence this.stop(); + this.inProgress = true; try { // Generate prompts const prompts = await this.promptGenerator.generateTimedPrompts(settings); @@ -42,6 +49,8 @@ export class TimedPromptsCommand { } catch (error) { console.error('Error executing timed prompts command:', error); // Error handling is done in the service layer + } finally { + this.inProgress = false; } } @@ -52,6 +61,7 @@ export class TimedPromptsCommand { } this.promptQueue = []; this.currentIndex = 0; + this.inProgress = false; } isRunning(): boolean { @@ -100,10 +110,13 @@ export class TimedPromptsCommand { // MARK: - Status Methods getStatus(): { isRunning: boolean; currentPrompt: number; totalPrompts: number } { + const totalPrompts = this.promptQueue.length; + const currentPrompt = totalPrompts === 0 ? 0 : Math.min(this.currentIndex + 1, totalPrompts); + return { isRunning: this.isRunning(), - currentPrompt: this.currentIndex + 1, - totalPrompts: this.promptQueue.length + currentPrompt, + totalPrompts }; }