This commit is contained in:
zsviczian 2023-04-04 13:15:35 +02:00 committed by GitHub
parent 11109fcc62
commit 0a6d41ecf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -135,7 +135,8 @@ export const throttleRAF = <T extends any[]>(
let timerId: number | null = null;
let lastArgs: T | null = null;
let lastArgsTrailing: T | null = null;
let watchdog: NodeJS.Timeout | null = null;
const scheduleFunc = (args: T) => {
timerId = window.requestAnimationFrame(() => {
timerId = null;
@ -146,6 +147,9 @@ export const throttleRAF = <T extends any[]>(
lastArgsTrailing = null;
scheduleFunc(lastArgs);
}
if (watchdog) {
clearTimout(watchdog);
}
});
};
@ -165,6 +169,9 @@ export const throttleRAF = <T extends any[]>(
if (timerId !== null) {
cancelAnimationFrame(timerId);
timerId = null;
if (watchdog) {
clearTimout(watchdog);
}
}
if (lastArgs) {
fn(...(lastArgsTrailing || lastArgs));
@ -176,8 +183,18 @@ export const throttleRAF = <T extends any[]>(
if (timerId !== null) {
cancelAnimationFrame(timerId);
timerId = null;
if (watchdog) {
clearTimout(watchdog);
}
}
};
watchdog = setTimeout(() => {
console.log("watchdog", timerId);
if (timerId !== null) {
cancelAnimationFrame(timerId);
timerId = null;
}
},1000);
return ret;
};