From 133f57da3d45efc507ab753abc87b913383a5545 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Thu, 14 May 2026 01:22:53 -0700 Subject: [PATCH] perf(renderer): gate mobile pairing polling (#1820) --- .../src/components/settings/MobilePane.tsx | 13 +-- .../mobile-pairing-device-polling.test.ts | 42 +++++++ .../settings/mobile-pairing-device-polling.ts | 108 ++++++++++++++++++ 3 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 src/renderer/src/components/settings/mobile-pairing-device-polling.test.ts create mode 100644 src/renderer/src/components/settings/mobile-pairing-device-polling.ts diff --git a/src/renderer/src/components/settings/MobilePane.tsx b/src/renderer/src/components/settings/MobilePane.tsx index bc0db2456..6dd0ae7e0 100644 --- a/src/renderer/src/components/settings/MobilePane.tsx +++ b/src/renderer/src/components/settings/MobilePane.tsx @@ -16,6 +16,7 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle } from '../ui/dialog' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select' import type { SettingsSearchEntry } from './settings-search' import { useAppStore } from '../../store' +import { useMobilePairingDevicePolling } from './mobile-pairing-device-polling' // Why: the section heading "When you leave the mobile app" carries the // "what happens" framing so the option labels only need to vary on the @@ -177,13 +178,11 @@ export function MobilePane(): React.JSX.Element { setDeviceCountAtQr(devices.length) }, [qrDataUrl]) // eslint-disable-line react-hooks/exhaustive-deps - useEffect(() => { - if (deviceCountAtQr === null || devices.length > deviceCountAtQr) { - return - } - const interval = setInterval(() => void loadDevices(), 3000) - return () => clearInterval(interval) - }, [deviceCountAtQr, devices.length, loadDevices]) + useMobilePairingDevicePolling({ + deviceCountAtQr, + currentDeviceCount: devices.length, + loadDevices + }) async function copyPairingCode() { if (!pairingUrl) { diff --git a/src/renderer/src/components/settings/mobile-pairing-device-polling.test.ts b/src/renderer/src/components/settings/mobile-pairing-device-polling.test.ts new file mode 100644 index 000000000..0a19fe674 --- /dev/null +++ b/src/renderer/src/components/settings/mobile-pairing-device-polling.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from 'vitest' +import { shouldPollMobilePairingDevices } from './mobile-pairing-device-polling' + +describe('mobile pairing device polling', () => { + it('polls only while waiting for a new device in the visible focused window', () => { + expect( + shouldPollMobilePairingDevices({ + deviceCountAtQr: 1, + currentDeviceCount: 1, + visibilityState: 'visible', + focused: true + }) + ).toBe(true) + + expect( + shouldPollMobilePairingDevices({ + deviceCountAtQr: 1, + currentDeviceCount: 2, + visibilityState: 'visible', + focused: true + }) + ).toBe(false) + + expect( + shouldPollMobilePairingDevices({ + deviceCountAtQr: 1, + currentDeviceCount: 1, + visibilityState: 'hidden', + focused: true + }) + ).toBe(false) + + expect( + shouldPollMobilePairingDevices({ + deviceCountAtQr: 1, + currentDeviceCount: 1, + visibilityState: 'visible', + focused: false + }) + ).toBe(false) + }) +}) diff --git a/src/renderer/src/components/settings/mobile-pairing-device-polling.ts b/src/renderer/src/components/settings/mobile-pairing-device-polling.ts new file mode 100644 index 000000000..92242c3c9 --- /dev/null +++ b/src/renderer/src/components/settings/mobile-pairing-device-polling.ts @@ -0,0 +1,108 @@ +import { useEffect } from 'react' + +export const MOBILE_PAIRING_DEVICE_POLL_MS = 3000 + +export type MobilePairingDevicePollState = { + deviceCountAtQr: number | null + currentDeviceCount: number + visibilityState: Document['visibilityState'] + focused: boolean +} + +export function shouldPollMobilePairingDevices({ + deviceCountAtQr, + currentDeviceCount, + visibilityState, + focused +}: MobilePairingDevicePollState): boolean { + return ( + deviceCountAtQr !== null && + currentDeviceCount <= deviceCountAtQr && + visibilityState === 'visible' && + focused + ) +} + +export function useMobilePairingDevicePolling({ + deviceCountAtQr, + currentDeviceCount, + loadDevices +}: { + deviceCountAtQr: number | null + currentDeviceCount: number + loadDevices: () => Promise +}): void { + useEffect(() => { + if (deviceCountAtQr === null || currentDeviceCount > deviceCountAtQr) { + return + } + + let stopped = false + let pollInFlight = false + let timeoutId: number | null = null + + const clearPendingPoll = (): void => { + if (timeoutId !== null) { + window.clearTimeout(timeoutId) + timeoutId = null + } + } + + const canPoll = (): boolean => + shouldPollMobilePairingDevices({ + deviceCountAtQr, + currentDeviceCount, + visibilityState: document.visibilityState, + focused: document.hasFocus() + }) + + const scheduleNextPoll = (): void => { + clearPendingPoll() + if (stopped || !canPoll()) { + return + } + timeoutId = window.setTimeout(() => { + timeoutId = null + if (stopped || !canPoll()) { + return + } + if (pollInFlight) { + scheduleNextPoll() + return + } + pollInFlight = true + // Why: wait for each IPC call to settle before scheduling the next + // poll, avoiding overlapping device-list requests on a slow host. + void loadDevices().finally(() => { + pollInFlight = false + scheduleNextPoll() + }) + }, MOBILE_PAIRING_DEVICE_POLL_MS) + } + + const resumePolling = (): void => { + if (!canPoll()) { + clearPendingPoll() + return + } + if (pollInFlight) { + return + } + pollInFlight = true + void loadDevices().finally(() => { + pollInFlight = false + scheduleNextPoll() + }) + } + + scheduleNextPoll() + window.addEventListener('focus', resumePolling) + document.addEventListener('visibilitychange', resumePolling) + return () => { + stopped = true + clearPendingPoll() + window.removeEventListener('focus', resumePolling) + document.removeEventListener('visibilitychange', resumePolling) + } + }, [deviceCountAtQr, currentDeviceCount, loadDevices]) +}