From fe77591d426e0f84058269e08ec10e85bd1a0a66 Mon Sep 17 00:00:00 2001 From: Pedram Amini Date: Sun, 18 Jan 2026 13:14:42 -0600 Subject: [PATCH] =?UTF-8?q?##=20CHANGES=20-=20Eliminated=20mobile=20sessio?= =?UTF-8?q?n=20ref=20initialization=20race=20with=20immediate=20saved=20de?= =?UTF-8?q?faults=20=F0=9F=9A=80=20-=20Made=20active=20tab=20ref=20mirror?= =?UTF-8?q?=20URL/saved=20tab=20instantly=20for=20reliable=20callbacks=20?= =?UTF-8?q?=F0=9F=A7=AD=20-=20Simplified=20hook=20test=20by=20removing=20u?= =?UTF-8?q?nnecessary=20async=20waiting=20on=20refs=20=F0=9F=A7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/web/hooks/useMobileSessionManagement.test.ts | 7 +++---- src/web/hooks/useMobileSessionManagement.ts | 6 ++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/__tests__/web/hooks/useMobileSessionManagement.test.ts b/src/__tests__/web/hooks/useMobileSessionManagement.test.ts index d6c5a144..cd878ed8 100644 --- a/src/__tests__/web/hooks/useMobileSessionManagement.test.ts +++ b/src/__tests__/web/hooks/useMobileSessionManagement.test.ts @@ -3,7 +3,7 @@ */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; -import { renderHook, act, waitFor } from '@testing-library/react'; +import { renderHook, act } from '@testing-library/react'; import { useMobileSessionManagement, type UseMobileSessionManagementDeps } from '../../../web/hooks/useMobileSessionManagement'; import type { Session } from '../../../web/hooks/useSessions'; @@ -82,9 +82,8 @@ describe('useMobileSessionManagement', () => { savedActiveTabId: 'tab-1', })); - await waitFor(() => { - expect(result.current.activeSessionIdRef.current).toBe('session-1'); - }); + // Refs should be initialized immediately with saved values (no race condition) + expect(result.current.activeSessionIdRef.current).toBe('session-1'); act(() => { result.current.sessionsHandlers.onSessionOutput('session-1', 'hello', 'ai', 'tab-1'); diff --git a/src/web/hooks/useMobileSessionManagement.ts b/src/web/hooks/useMobileSessionManagement.ts index bb1fbc11..8132b4a0 100644 --- a/src/web/hooks/useMobileSessionManagement.ts +++ b/src/web/hooks/useMobileSessionManagement.ts @@ -198,9 +198,11 @@ export function useMobileSessionManagement( const previousSessionStatesRef = useRef>(new Map()); // Ref to track activeSessionId for use in callbacks (avoids stale closure issues) - const activeSessionIdRef = useRef(null); + // Initialize with same value as state to avoid race condition where WebSocket + // messages arrive before useEffect syncs the ref + const activeSessionIdRef = useRef(urlSessionId || savedActiveSessionId); // Ref to track activeTabId for use in callbacks (avoids stale closure issues) - const activeTabIdRef = useRef(null); + const activeTabIdRef = useRef(urlTabId || savedActiveTabId); // Keep activeSessionIdRef in sync with state useEffect(() => {