Skip to content

Commit

Permalink
fix(rtc): set connection state on init
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious committed Mar 20, 2023
1 parent 52ffcab commit 47833e6
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions packages/agora-rtc-react/src/hooks/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import type { ConnectionState, IAgoraRTCClient } from "agora-rtc-sdk-ng";

import { useState } from "react";
import { useClientEvent } from "./events";
import { useEffect, useState } from "react";
import { listen } from "../listen";

export function useConnectionState(client?: IAgoraRTCClient | null): ConnectionState {
const [connectionState, setConnectionState] = useState(
client ? client.connectionState : "DISCONNECTED",
);
useClientEvent(client, "connection-state-change", setConnectionState);
useEffect(() => {
if (client) {
setConnectionState(client.connectionState);
return listen(client, "connection-state-change", setConnectionState);
} else {
setConnectionState("DISCONNECTED");
}
}, [client]);
return connectionState;
}

Expand Down Expand Up @@ -59,12 +66,18 @@ const initQuality = (): NetworkQuality => ({
*/
export function useNetworkQuality(client?: IAgoraRTCClient | null): NetworkQuality {
const [networkQuality, setNetworkQuality] = useState<NetworkQuality>(initQuality);
useClientEvent(client, "network-quality", q =>
setNetworkQuality({
uplink: q.uplinkNetworkQuality,
downlink: q.downlinkNetworkQuality,
delay: client?.getRTCStats().RTT ?? 0,
}),
);
useEffect(() => {
if (client) {
return listen(client, "network-quality", q =>
setNetworkQuality({
uplink: q.uplinkNetworkQuality,
downlink: q.downlinkNetworkQuality,
delay: client?.getRTCStats().RTT ?? 0,
}),
);
} else {
setNetworkQuality(initQuality());
}
}, [client]);
return networkQuality;
}

0 comments on commit 47833e6

Please sign in to comment.