fix: prevent multiple getPermissions RPC calls on page load

The getPermissions useEffect had send and pollPermissions in its dependency
array. Since send gets recreated when rpcDataChannel changes, this caused
multiple getPermissions RPC calls (5 observed) on page load.

Fix:
- Add rpcDataChannel readiness check to prevent calls before channel is open
- Remove send and pollPermissions from dependency array
- Keep only currentMode and rpcDataChannel.readyState as dependencies

This ensures getPermissions is called only when:
1. The RPC channel becomes ready (readyState changes to "open")
2. The session mode changes (observer <-> primary)

Eliminates duplicate RPC calls while maintaining correct behavior for
mode changes and initial connection.
This commit is contained in:
Alex P 2025-10-10 22:23:25 +03:00
parent 00e6edbfa8
commit f9e190f8b9
1 changed files with 4 additions and 2 deletions

View File

@ -62,7 +62,7 @@ interface PermissionsResponse {
export function usePermissions() {
const { currentMode } = useSessionStore();
const { setRpcHidProtocolVersion, rpcHidChannel } = useRTCStore();
const { setRpcHidProtocolVersion, rpcHidChannel, rpcDataChannel } = useRTCStore();
const [permissions, setPermissions] = useState<Record<string, boolean>>({});
const [isLoading, setIsLoading] = useState(true);
const previousCanControl = useRef<boolean>(false);
@ -102,8 +102,10 @@ export function usePermissions() {
const { send } = useJsonRpc(handleRpcRequest);
useEffect(() => {
if (rpcDataChannel?.readyState !== "open") return;
pollPermissions(send);
}, [send, currentMode, pollPermissions]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentMode, rpcDataChannel?.readyState]);
// Monitor permission changes and re-initialize HID when gaining control
useEffect(() => {