Skip to content

Commit cfa695a

Browse files
committed
feat: update logic
Signed-off-by: Adam Setch <[email protected]>
1 parent d083a8e commit cfa695a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/renderer/hooks/useInactivityTimer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useCallback, useEffect, useRef } from 'react';
66
*/
77
export const useInactivityTimer = (callback: () => void, delay: number) => {
88
const savedCallback = useRef<(() => void) | null>(null);
9-
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
9+
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
1010

1111
// Remember the latest callback
1212
useEffect(() => {
@@ -18,10 +18,12 @@ export const useInactivityTimer = (callback: () => void, delay: number) => {
1818
if (timeoutRef.current) {
1919
clearTimeout(timeoutRef.current);
2020
}
21-
2221
if (delay !== null && savedCallback.current) {
2322
timeoutRef.current = setTimeout(() => {
23+
// Fire callback once inactivity threshold reached
2424
savedCallback.current?.();
25+
// Schedule next run while still inactive
26+
resetTimer();
2527
}, delay);
2628
}
2729
}, [delay]);
@@ -31,8 +33,6 @@ export const useInactivityTimer = (callback: () => void, delay: number) => {
3133
if (delay === null) {
3234
return;
3335
}
34-
35-
// Events that indicate user activity
3636
const events = [
3737
'mousedown',
3838
'mousemove',
@@ -47,7 +47,7 @@ export const useInactivityTimer = (callback: () => void, delay: number) => {
4747
document.addEventListener(event, resetTimer, { passive: true });
4848
});
4949

50-
// Start the initial timer
50+
// Start initial timer
5151
resetTimer();
5252

5353
// Cleanup function

0 commit comments

Comments
 (0)