From 0a6d41ecf9c81dd6835819798ea06eec71d847e0 Mon Sep 17 00:00:00 2001 From: zsviczian Date: Tue, 4 Apr 2023 13:15:35 +0200 Subject: [PATCH] watchdog --- src/utils.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 1d545f977..b7ecc9c7d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -135,7 +135,8 @@ export const throttleRAF = ( 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 = ( lastArgsTrailing = null; scheduleFunc(lastArgs); } + if (watchdog) { + clearTimout(watchdog); + } }); }; @@ -165,6 +169,9 @@ export const throttleRAF = ( if (timerId !== null) { cancelAnimationFrame(timerId); timerId = null; + if (watchdog) { + clearTimout(watchdog); + } } if (lastArgs) { fn(...(lastArgsTrailing || lastArgs)); @@ -176,8 +183,18 @@ export const throttleRAF = ( 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; };