Improves timed prompts command concurrency handling
Enhances execution flow to prevent race conditions during async prompt generation Refines inProgress flag management to ensure reliable concurrent execution: - Sets flag before async work begins - Guarantees flag reset in finally block - Prevents potential state inconsistencies during rapid command triggers Clarifies code comments explaining concurrency protection mechanism
This commit is contained in:
1 file changed
+12
-4
@@ -51,8 +51,10 @@ export class TimedPromptsCommand {
|
|||||||
* 4. Shows the first prompt immediately
|
* 4. Shows the first prompt immediately
|
||||||
* 5. Starts an interval to show remaining prompts
|
* 5. Starts an interval to show remaining prompts
|
||||||
*
|
*
|
||||||
* The inProgress flag prevents issues if the user triggers the command
|
* The inProgress flag prevents concurrent executions during async prompt
|
||||||
* multiple times quickly while prompt generation is in progress.
|
* generation. It's set to true before any async work begins and reset to
|
||||||
|
* false in the finally block after all work completes, ensuring reliable
|
||||||
|
* concurrency protection.
|
||||||
*
|
*
|
||||||
* @param settings - Current plugin settings for generation parameters
|
* @param settings - Current plugin settings for generation parameters
|
||||||
*/
|
*/
|
||||||
@@ -63,9 +65,10 @@ export class TimedPromptsCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set flag BEFORE any async work to prevent concurrent execution
|
||||||
this.inProgress = true;
|
this.inProgress = true;
|
||||||
|
|
||||||
// Stop any existing timed sequence
|
// Stop any existing timed sequence (does NOT reset inProgress)
|
||||||
this.stop();
|
this.stop();
|
||||||
|
|
||||||
|
|
||||||
@@ -94,6 +97,8 @@ export class TimedPromptsCommand {
|
|||||||
console.error('Error executing timed prompts command:', error);
|
console.error('Error executing timed prompts command:', error);
|
||||||
// Error handling is done in the service layer
|
// Error handling is done in the service layer
|
||||||
} finally {
|
} finally {
|
||||||
|
// Reset flag after all work completes (success or failure)
|
||||||
|
// This ensures the flag is always reset and prevents stuck state
|
||||||
this.inProgress = false;
|
this.inProgress = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,6 +109,9 @@ export class TimedPromptsCommand {
|
|||||||
* Cleans up the interval timer and resets all state. This is important
|
* Cleans up the interval timer and resets all state. This is important
|
||||||
* for preventing memory leaks and ensuring the next execution starts fresh.
|
* for preventing memory leaks and ensuring the next execution starts fresh.
|
||||||
* Safe to call even when no sequence is running.
|
* Safe to call even when no sequence is running.
|
||||||
|
*
|
||||||
|
* Note: Does NOT reset inProgress flag - that's managed by execute() to
|
||||||
|
* prevent race conditions during async prompt generation.
|
||||||
*/
|
*/
|
||||||
stop(): void {
|
stop(): void {
|
||||||
if (this.activeInterval !== null) {
|
if (this.activeInterval !== null) {
|
||||||
@@ -112,7 +120,7 @@ export class TimedPromptsCommand {
|
|||||||
}
|
}
|
||||||
this.promptQueue = [];
|
this.promptQueue = [];
|
||||||
this.currentIndex = 0;
|
this.currentIndex = 0;
|
||||||
this.inProgress = false;
|
// inProgress is NOT reset here - only execute() manages this flag
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user