| 4 | |
| 5 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
| 6 | export function simpleDebounce<T extends Function>(fn: T): T { |
| 7 | let executing = false |
| 8 | let pendingExecution: any = null |
| 9 | return (async (...args) => { |
| 10 | if (executing) { |
| 11 | // if there are 2 executions 50ms apart, ignore the last one |
| 12 | pendingExecution = args |
| 13 | return null as any |
| 14 | } |
| 15 | executing = true |
| 16 | await fn(...args).catch((e) => console.error(e)) |
| 17 | if (pendingExecution) { |
| 18 | await fn(...pendingExecution).catch((e) => console.error(e)) |
| 19 | pendingExecution = null |
| 20 | } |
| 21 | executing = false |
| 22 | }) as any |
| 23 | } |
| 24 | /* eslint-enable @typescript-eslint/no-explicit-any */ |