0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-25 02:31:59 -05:00
ghost/apps/shade/src/utils/debounce.ts
Peter Zimon fbbf34e1d0
Added Shade fundamentals (#21812)
ref
https://linear.app/ghost/issue/DES-1020/create-new-react-app-for-shade

Shade is our new design system that follows React best practices and
leverages third-party libraries extensively. It's built on ShadCN/UI
which is one of the most popular React UI libraries today. This commit
adds an (almost) empty React app, set up to be the a starting point of
Shade.
2024-12-11 15:55:58 +01:00

24 lines
657 B
TypeScript

export function debounce<T extends unknown[]>(func: (...args: T) => void, wait: number, immediate: boolean = false): (...args: T) => void {
let timeoutId: ReturnType<typeof setTimeout> | null;
return function (this: unknown, ...args: T): void {
const later = () => {
timeoutId = null;
if (!immediate) {
func.apply(this, args);
}
};
const callNow = immediate && !timeoutId;
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(later, wait);
if (callNow) {
func.apply(this, args);
}
};
}