|
| 1 | +import { logger } from '../../logger'; |
| 2 | + |
| 3 | +/** Resolve effective debounce time, respecting VISOR_DEBOUNCE_OVERRIDE env var for testing */ |
| 4 | +function effectiveDebounceMs(requested: number): number { |
| 5 | + const override = process.env.VISOR_DEBOUNCE_OVERRIDE; |
| 6 | + if (override !== undefined) { |
| 7 | + const ms = parseInt(override, 10); |
| 8 | + if (!isNaN(ms) && ms >= 0) return ms; |
| 9 | + } |
| 10 | + return requested; |
| 11 | +} |
| 12 | + |
| 13 | +interface PendingEntry { |
| 14 | + timer: ReturnType<typeof setTimeout>; |
| 15 | + invocations: number; |
| 16 | + firstInvocationTime: number; |
| 17 | + resolve: (value: 'executed' | 'debounced') => void; |
| 18 | + reject: (err: unknown) => void; |
| 19 | + fn: () => Promise<unknown>; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Global debounce manager for step execution. |
| 24 | + * |
| 25 | + * When a step has `debounce: <ms>`, multiple invocations within the debounce |
| 26 | + * window are coalesced — only the LAST invocation actually executes, after |
| 27 | + * the window expires with no new invocations. |
| 28 | + * |
| 29 | + * Key: typically `workflow:stepId` — callers decide the grouping. |
| 30 | + * |
| 31 | + * Earlier invocations resolve with 'debounced' (skipped). |
| 32 | + * The final invocation resolves with 'executed' after the real execution. |
| 33 | + */ |
| 34 | +export class DebounceManager { |
| 35 | + private pending = new Map<string, PendingEntry>(); |
| 36 | + /** Waiters that were superseded and should resolve as 'debounced' */ |
| 37 | + private superseded = new Map<string, Array<(v: 'debounced') => void>>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Enqueue an execution. Returns 'executed' if this invocation won the |
| 41 | + * debounce race, or 'debounced' if it was superseded by a later one. |
| 42 | + */ |
| 43 | + enqueue( |
| 44 | + key: string, |
| 45 | + debounceMs: number, |
| 46 | + fn: () => Promise<unknown> |
| 47 | + ): Promise<'executed' | 'debounced'> { |
| 48 | + const actualMs = effectiveDebounceMs(debounceMs); |
| 49 | + return new Promise<'executed' | 'debounced'>((resolve, reject) => { |
| 50 | + const existing = this.pending.get(key); |
| 51 | + |
| 52 | + if (existing) { |
| 53 | + // Supersede previous invocation — it resolves as 'debounced' |
| 54 | + clearTimeout(existing.timer); |
| 55 | + // Move the previous resolve to superseded list |
| 56 | + if (!this.superseded.has(key)) { |
| 57 | + this.superseded.set(key, []); |
| 58 | + } |
| 59 | + this.superseded.get(key)!.push(existing.resolve as (v: 'debounced') => void); |
| 60 | + existing.invocations++; |
| 61 | + |
| 62 | + logger.info( |
| 63 | + `[DebounceManager] ${key}: invocation #${existing.invocations}, resetting ${actualMs}ms timer` |
| 64 | + ); |
| 65 | + } else { |
| 66 | + logger.info(`[DebounceManager] ${key}: first invocation, starting ${actualMs}ms debounce`); |
| 67 | + } |
| 68 | + |
| 69 | + const invocations = existing ? existing.invocations : 1; |
| 70 | + const firstTime = existing ? existing.firstInvocationTime : Date.now(); |
| 71 | + |
| 72 | + const timer = setTimeout(async () => { |
| 73 | + this.pending.delete(key); |
| 74 | + |
| 75 | + // Resolve all superseded waiters as 'debounced' |
| 76 | + const waiters = this.superseded.get(key) || []; |
| 77 | + this.superseded.delete(key); |
| 78 | + for (const w of waiters) { |
| 79 | + w('debounced'); |
| 80 | + } |
| 81 | + |
| 82 | + const elapsed = Date.now() - firstTime; |
| 83 | + logger.info( |
| 84 | + `[DebounceManager] ${key}: executing after ${invocations} invocation(s) over ${elapsed}ms` |
| 85 | + ); |
| 86 | + |
| 87 | + try { |
| 88 | + await fn(); |
| 89 | + resolve('executed'); |
| 90 | + } catch (err) { |
| 91 | + reject(err); |
| 92 | + } |
| 93 | + }, actualMs); |
| 94 | + |
| 95 | + this.pending.set(key, { |
| 96 | + timer, |
| 97 | + invocations, |
| 98 | + firstInvocationTime: firstTime, |
| 99 | + resolve, |
| 100 | + reject, |
| 101 | + fn, |
| 102 | + }); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + /** Cancel a pending debounce. All waiters resolve as 'debounced'. */ |
| 107 | + cancel(key: string): boolean { |
| 108 | + const entry = this.pending.get(key); |
| 109 | + if (!entry) return false; |
| 110 | + clearTimeout(entry.timer); |
| 111 | + this.pending.delete(key); |
| 112 | + entry.resolve('debounced'); |
| 113 | + const waiters = this.superseded.get(key) || []; |
| 114 | + this.superseded.delete(key); |
| 115 | + for (const w of waiters) { |
| 116 | + w('debounced'); |
| 117 | + } |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + /** Cancel all pending debounces. */ |
| 122 | + clear(): void { |
| 123 | + for (const [key] of this.pending) { |
| 124 | + this.cancel(key); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** Number of pending debounce keys. */ |
| 129 | + get size(): number { |
| 130 | + return this.pending.size; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +let __instance: DebounceManager | undefined; |
| 135 | + |
| 136 | +export function getDebounceManager(): DebounceManager { |
| 137 | + if (!__instance) __instance = new DebounceManager(); |
| 138 | + return __instance; |
| 139 | +} |
| 140 | + |
| 141 | +export function resetDebounceManager(): void { |
| 142 | + if (__instance) { |
| 143 | + __instance.clear(); |
| 144 | + __instance = undefined; |
| 145 | + } |
| 146 | +} |
0 commit comments