fix(store): use Promise-based lock for refreshEnvironment

- Replace boolean flag with Promise-based lock pattern
- Prevents race condition when multiple callers invoke refresh
- Concurrent calls now properly await the same Promise

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jarrod Watts
2026-01-03 12:44:53 +11:00
parent 2e85c4f5c9
commit ea8c55cdb8

View File

@@ -49,7 +49,7 @@ export class HudStore {
private readonly emitIntervalMs: number;
private settingsError: string | null = null;
private configError: string | null = null;
private refreshing = false;
private refreshPromise: Promise<void> | null = null;
constructor(options: HudStoreOptions) {
if (options.initialTranscriptPath) {
@@ -201,9 +201,17 @@ export class HudStore {
this.emit();
};
private async refreshEnvironment(): Promise<void> {
if (this.refreshing) return;
this.refreshing = true;
private refreshEnvironment(): Promise<void> {
if (this.refreshPromise) return this.refreshPromise;
this.refreshPromise = this.doRefreshEnvironment().finally(() => {
this.refreshPromise = null;
});
return this.refreshPromise;
}
private async doRefreshEnvironment(): Promise<void> {
try {
const [settingsResult, configResult] = await Promise.all([
this.settingsReader.readWithStatusAsync(),
@@ -246,8 +254,6 @@ export class HudStore {
this.emit();
} catch (err) {
logger.warn('HudStore', 'Environment refresh failed', { err });
} finally {
this.refreshing = false;
}
}