mirror of
https://github.com/fankes/beszel.git
synced 2025-10-18 17:29:28 +08:00
refactor: improve runOnce
with weakmap cache
This commit is contained in:
@@ -348,15 +348,20 @@ export function debounce<T extends (...args: any[]) => any>(func: T, wait: numbe
|
||||
}
|
||||
}
|
||||
|
||||
// Cache for runOnce
|
||||
const runOnceCache = new WeakMap<Function, { done: boolean; result: unknown }>()
|
||||
/** Run a function only once */
|
||||
export function runOnce<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
|
||||
let done = false
|
||||
let result: any
|
||||
return (...args: any) => {
|
||||
if (!done) {
|
||||
result = fn(...args)
|
||||
done = true
|
||||
export function runOnce<T extends (...args: any[]) => any>(fn: T): T {
|
||||
return ((...args: Parameters<T>) => {
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user