From 10501d886411dabd90a721ad0148694254025477 Mon Sep 17 00:00:00 2001 From: khaira777 <777gurkirat@gmail.com> Date: Thu, 9 Jul 2026 17:21:52 -0400 Subject: [PATCH] fix(kds): prevent setInterval leak during fallback polling Fixes #69 --- frontend/src/app/(dashboard)/kds/page.tsx | 23 ++++++++++++----------- frontend/src/app/kds-standalone/page.tsx | 19 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/frontend/src/app/(dashboard)/kds/page.tsx b/frontend/src/app/(dashboard)/kds/page.tsx index 77fa131..a6f7d84 100644 --- a/frontend/src/app/(dashboard)/kds/page.tsx +++ b/frontend/src/app/(dashboard)/kds/page.tsx @@ -76,6 +76,13 @@ export default function KdsPage() { const wsRef = useRef(null); const restIntervalRef = useRef(null); + const stopRestPolling = useCallback(() => { + if (restIntervalRef.current) { + clearInterval(restIntervalRef.current); + restIntervalRef.current = null; + } + }, []); + const fetchOrdersRest = useCallback(async () => { try { const { data } = await api.get(`/kitchen/orders?status=pending,preparing,ready,served`); @@ -88,18 +95,12 @@ export default function KdsPage() { }, []); const startRestPolling = useCallback(() => { + stopRestPolling(); setConnectionMode('rest'); setConnected(true); fetchOrdersRest(); restIntervalRef.current = setInterval(fetchOrdersRest, 5000); - }, [fetchOrdersRest]); - - const stopRestPolling = useCallback(() => { - if (restIntervalRef.current) { - clearInterval(restIntervalRef.current); - restIntervalRef.current = null; - } - }, []); + }, [fetchOrdersRest, stopRestPolling]); const updateItemStatus = useCallback(async (itemId: number, status: KitchenStatus) => { setUpdating(itemId); @@ -232,10 +233,10 @@ export default function KdsPage() { connectionTimeout = setTimeout(() => { if (ws.readyState === WebSocket.CONNECTING) { ws.close(); - startRestPolling(); + setConnectionMode('rest'); } }, 5000); - }, [startRestPolling]); + }, []); useEffect(() => { const savedUser = localStorage.getItem('kds_user'); @@ -266,7 +267,7 @@ export default function KdsPage() { startRestPolling(); } return () => stopRestPolling(); - }, [connectionMode, user, startRestPolling]); + }, [connectionMode, user, startRestPolling, stopRestPolling]); const filteredOrders = orders .map((order) => ({ diff --git a/frontend/src/app/kds-standalone/page.tsx b/frontend/src/app/kds-standalone/page.tsx index e1e86c1..0d4bba5 100644 --- a/frontend/src/app/kds-standalone/page.tsx +++ b/frontend/src/app/kds-standalone/page.tsx @@ -125,6 +125,13 @@ export default function KdsStandalonePage() { const wsRef = useRef(null); const restIntervalRef = useRef(null); + const stopRestPolling = useCallback(() => { + if (restIntervalRef.current) { + clearInterval(restIntervalRef.current); + restIntervalRef.current = null; + } + }, []); + const fetchOrdersRest = useCallback(async () => { try { const { data } = await api.get('/api/kds/orders'); @@ -149,21 +156,15 @@ export default function KdsStandalonePage() { } setConnected(false); } - }, []); + }, [stopRestPolling]); const startRestPolling = useCallback(() => { + stopRestPolling(); setConnectionMode('rest'); setConnected(true); fetchOrdersRest(); restIntervalRef.current = setInterval(fetchOrdersRest, 5000); - }, [fetchOrdersRest]); - - const stopRestPolling = useCallback(() => { - if (restIntervalRef.current) { - clearInterval(restIntervalRef.current); - restIntervalRef.current = null; - } - }, []); + }, [fetchOrdersRest, stopRestPolling]); const updateItemStatus = useCallback(async (itemId: number, status: KitchenStatus) => { setUpdating(itemId);