Files
ontime/apps/server/src/utils/throttle.ts
T
Alex Christoffer Rasmussen 3895b37572 Allow time change from api (#1009)
Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com>
2024-06-02 15:21:09 +02:00

34 lines
853 B
TypeScript

/**
* Creates a throttled version of the passed function
* This function uses a leading algorithm
* which means that the function will be executed immediately on first call
* @param {Function} cb - function to throttle
* @param {number} delay - time (in ms) to throttle
* @returns {Function}
*/
export function throttle<T extends any[], U>(cb: (...args: T) => U, delay: number) {
let shouldWait = false;
let waitingArgs: T | null = null;
const timeoutFunc = () => {
if (waitingArgs == null) {
shouldWait = false;
} else {
cb(...waitingArgs);
waitingArgs = null;
setTimeout(timeoutFunc, delay);
}
};
return (...args: T) => {
if (shouldWait) {
waitingArgs = args;
return true;
}
cb(...args);
shouldWait = true;
setTimeout(timeoutFunc, delay);
return false;
};
}