Files
readest/apps/readest-app/src/utils/debounce.ts
T
Huang Xin 033e161455 layout: overlay the footer of the translator popup and less sensitive page flip with mouse wheel (#1238)
* layout: overlay the footer of the translator popup

* fix: less sensitive page flip with mouse wheel, addresses #1165
2025-05-25 09:32:00 +02:00

39 lines
993 B
TypeScript

interface DebounceOptions {
emitLast?: boolean;
}
/**
* Debounces a function by waiting `delay` ms after the last call before executing it.
* If `emitLast` is false, it cancels the call instead of delaying it.
*/
export const debounce = <T extends (...args: Parameters<T>) => void | Promise<void>>(
func: T,
delay: number,
options: DebounceOptions = { emitLast: true },
): ((...args: Parameters<T>) => void) => {
let timeout: ReturnType<typeof setTimeout> | null = null;
let lastArgs: Parameters<T> | null = null;
return (...args: Parameters<T>): void => {
if (timeout) {
clearTimeout(timeout);
}
if (options.emitLast) {
lastArgs = args;
timeout = setTimeout(() => {
if (lastArgs) {
func(...(lastArgs as Parameters<T>));
lastArgs = null;
}
timeout = null;
}, delay);
} else {
timeout = setTimeout(() => {
func(...args);
timeout = null;
}, delay);
}
};
};