diff --git a/beszel/site/src/lib/utils.ts b/beszel/site/src/lib/utils.ts index 5dbf12f..f69c8ea 100644 --- a/beszel/site/src/lib/utils.ts +++ b/beszel/site/src/lib/utils.ts @@ -348,15 +348,20 @@ export function debounce any>(func: T, wait: numbe } } +// Cache for runOnce +const runOnceCache = new WeakMap() /** Run a function only once */ -export function runOnce any>(fn: T): (...args: Parameters) => ReturnType { - let done = false - let result: any - return (...args: any) => { - if (!done) { - result = fn(...args) - done = true +export function runOnce any>(fn: T): T { + return ((...args: Parameters) => { + let state = runOnceCache.get(fn) + if (!state) { + state = { done: false, result: undefined } + runOnceCache.set(fn, state) } - return result - } + if (!state.done) { + state.result = fn(...args) + state.done = true + } + return state.result + }) as T }