refactor: streamline keep-alive timer management in useKeyboard hook

This commit is contained in:
Adam Shiervani 2025-09-18 11:13:03 +02:00 committed by Siyuan Miao
parent 820e462dda
commit a2b060b42e
1 changed files with 3 additions and 5 deletions

View File

@ -272,15 +272,13 @@ export default function useKeyboard() {
}, []); }, []);
const scheduleKeepAlive = useCallback(() => { const scheduleKeepAlive = useCallback(() => {
// Clear existing timer if it exists // Clears existing keepalive timer
if (keepAliveTimerRef.current) { cancelKeepAlive();
clearInterval(keepAliveTimerRef.current);
}
keepAliveTimerRef.current = setInterval(() => { keepAliveTimerRef.current = setInterval(() => {
sendKeypressKeepAliveHidRpc(); sendKeypressKeepAliveHidRpc();
}, KEEPALIVE_INTERVAL); }, KEEPALIVE_INTERVAL);
}, [sendKeypressKeepAliveHidRpc]); }, [cancelKeepAlive, sendKeypressKeepAliveHidRpc]);
// resetKeyboardState is used to reset the keyboard state to no keys pressed and no modifiers. // resetKeyboardState is used to reset the keyboard state to no keys pressed and no modifiers.
// This is useful for macros and when the browser loses focus to ensure that the keyboard state // This is useful for macros and when the browser loses focus to ensure that the keyboard state