( func: T, wait: number, immediate = false )
| 418 | * Returns a debounced function with a flush() method to immediately execute pending calls |
| 419 | */ |
| 420 | export function debounce<T extends (...args: never[]) => unknown>( |
| 421 | func: T, |
| 422 | wait: number, |
| 423 | immediate = false |
| 424 | ): DebouncedFunction<T> { |
| 425 | let timeout: number | undefined; |
| 426 | let lastArgs: Parameters<T> | undefined; |
| 427 | let lastThis: unknown; |
| 428 | |
| 429 | const debounced = function (this: unknown, ...args: Parameters<T>) { |
| 430 | lastArgs = args; |
| 431 | // eslint-disable-next-line @typescript-eslint/no-this-alias -- Debounce preserves the caller's this binding for later invocation. |
| 432 | lastThis = this; |
| 433 | |
| 434 | const later = () => { |
| 435 | timeout = undefined; |
| 436 | lastArgs = undefined; |
| 437 | if (!immediate) { |
| 438 | func.apply(lastThis, args); |
| 439 | } |
| 440 | }; |
| 441 | |
| 442 | const callNow = immediate && !timeout; |
| 443 | window.clearTimeout(timeout); |
| 444 | timeout = window.setTimeout(later, wait); |
| 445 | |
| 446 | if (callNow) { |
| 447 | func.apply(this, args); |
| 448 | } |
| 449 | } as DebouncedFunction<T>; |
| 450 | |
| 451 | debounced.flush = () => { |
| 452 | if (timeout && lastArgs) { |
| 453 | window.clearTimeout(timeout); |
| 454 | timeout = undefined; |
| 455 | func.apply(lastThis, lastArgs); |
| 456 | lastArgs = undefined; |
| 457 | } |
| 458 | }; |
| 459 | |
| 460 | return debounced; |
| 461 | } |
no test coverage detected