/* eslint-disable @typescript-eslint/no-explicit-any */ /** * MIT License Copyright (c) 2017 Jakub Chodorowicz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ export type Options = { isImmediate?: boolean maxWait?: number callback?: (data: Result) => void } export interface DebouncedFunction any> { (this: ThisParameterType, ...args: Args & Parameters): Promise> cancel: (reason?: any) => void } interface DebouncedPromise { resolve: (result: FunctionReturn) => void reject: (reason?: any) => void } export function debounce any>( func: F, waitMilliseconds = 50, options: Options> = {}, ): DebouncedFunction { let timeoutId: ReturnType | undefined const isImmediate = options.isImmediate ?? false const callback = options.callback ?? false const maxWait = options.maxWait let lastInvokeTime = Date.now() let promises: DebouncedPromise>[] = [] function nextInvokeTimeout() { if (maxWait !== undefined) { const timeSinceLastInvocation = Date.now() - lastInvokeTime if (timeSinceLastInvocation + waitMilliseconds >= maxWait) { return maxWait - timeSinceLastInvocation } } return waitMilliseconds } const debouncedFunction = function (this: ThisParameterType, ...args: Parameters) { // eslint-disable-next-line no-invalid-this, @typescript-eslint/no-this-alias const context = this return new Promise>((resolve, reject) => { const invokeFunction = function () { timeoutId = undefined lastInvokeTime = Date.now() if (!isImmediate) { const result = func.apply(context, args) callback && callback(result) promises.forEach(({ resolve }) => resolve(result)) promises = [] } } const shouldCallNow = isImmediate && timeoutId === undefined if (timeoutId !== undefined) { clearTimeout(timeoutId) } timeoutId = setTimeout(invokeFunction, nextInvokeTimeout()) if (shouldCallNow) { const result = func.apply(context, args) callback && callback(result) return resolve(result) } promises.push({ resolve, reject }) }) } debouncedFunction.cancel = function (reason?: any) { if (timeoutId !== undefined) { clearTimeout(timeoutId) } promises.forEach(({ reject }) => reject(reason)) promises = [] } return debouncedFunction }