mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 01:13:55 +00:00
797a0edf57
* feat: add external endpoint * add external to messagecontrol --------- Co-authored-by: Carlos Valente <carlosvalente@pm.me>
33 lines
813 B
TypeScript
33 lines
813 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;
|
|
const timeoutFunc = () => {
|
|
if (waitingArgs == null) {
|
|
shouldWait = false;
|
|
} else {
|
|
cb(...waitingArgs);
|
|
waitingArgs = null;
|
|
setTimeout(timeoutFunc, delay);
|
|
}
|
|
};
|
|
|
|
return (...args: T) => {
|
|
if (shouldWait) {
|
|
waitingArgs = args;
|
|
return;
|
|
}
|
|
|
|
cb(...args);
|
|
shouldWait = true;
|
|
setTimeout(timeoutFunc, delay);
|
|
};
|
|
}
|