[priority: 4] useGameLoop recreates interval every tick due to unstable tick reference #73

Closed
opened 2026-05-19 09:09:48 +02:00 by Anexim · 2 comments
Owner

Severity: 4 — High
File: src/lib/game/stores/gameHooks.ts, lines 17-22

Description:

export function useGameLoop() {
  const tick = useGameStore((s) => s.tick);
  useEffect(() => {
    const interval = setInterval(tick, TICK_MS);
    return () => clearInterval(interval);
  }, [tick]);
}

The tick function is recreated on every state change, causing the useEffect to cleanup and re-run every 200ms. This is wasteful and can cause timing jitter.

Fix: Use getState() inside the interval callback:

useEffect(() => {
  const interval = setInterval(() => useGameStore.getState().tick(), TICK_MS);
  return () => clearInterval(interval);
}, []);
**Severity:** 4 — High **File:** `src/lib/game/stores/gameHooks.ts`, lines 17-22 **Description:** ```ts export function useGameLoop() { const tick = useGameStore((s) => s.tick); useEffect(() => { const interval = setInterval(tick, TICK_MS); return () => clearInterval(interval); }, [tick]); } ``` The `tick` function is recreated on every state change, causing the `useEffect` to cleanup and re-run every 200ms. This is wasteful and can cause timing jitter. **Fix:** Use `getState()` inside the interval callback: ```ts useEffect(() => { const interval = setInterval(() => useGameStore.getState().tick(), TICK_MS); return () => clearInterval(interval); }, []); ```
Anexim added the ai:todo label 2026-05-19 09:09:48 +02:00
n8n-gitea was assigned by Anexim 2026-05-19 09:09:48 +02:00
Author
Owner

Starting work on Issue 73: useGameLoop recreates interval every tick due to unstable tick reference. Fixing to use getState() inside interval callback with empty dependency array.

Starting work on Issue 73: useGameLoop recreates interval every tick due to unstable tick reference. Fixing to use getState() inside interval callback with empty dependency array.
Author
Owner

Fixed: Changed useGameLoop to use useEffect with empty dependency array [] and useGameStore.getState().tick() inside the interval callback. This prevents interval recreation on every tick.

Fixed: Changed `useGameLoop` to use `useEffect` with empty dependency array `[]` and `useGameStore.getState().tick()` inside the interval callback. This prevents interval recreation on every tick.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Anexim/Mana-Loop#73