2024-01-25 23:27:53 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
2023-12-05 22:18:45 +08:00
|
|
|
import { useMutation, useSubscription } from '@apollo/client';
|
|
|
|
import { CONNECTION_STATUS_SUBSCRIPTION } from './queries';
|
2024-03-06 06:33:31 +08:00
|
|
|
import { UPDATE_CONNECTION_ALIVE_AT, UPDATE_USER_CLIENT_RTT } from './mutations';
|
2023-12-05 22:18:45 +08:00
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
const STATS_INTERVAL = window.meetingClientSettings.public.stats.interval;
|
2023-12-06 00:12:12 +08:00
|
|
|
|
2023-12-05 22:18:45 +08:00
|
|
|
const ConnectionStatus = () => {
|
2024-01-25 23:27:53 +08:00
|
|
|
const networkRttInMs = useRef(null); // Ref to store the current timeout
|
|
|
|
const lastStatusUpdatedAtReceived = useRef(null); // Ref to store the current timeout
|
|
|
|
const timeoutRef = useRef(null);
|
|
|
|
|
2024-03-06 06:33:31 +08:00
|
|
|
const [updateUserClientRtt] = useMutation(UPDATE_USER_CLIENT_RTT);
|
2023-12-05 22:18:45 +08:00
|
|
|
|
|
|
|
const handleUpdateUserClientResponseAt = () => {
|
2024-03-06 06:33:31 +08:00
|
|
|
updateUserClientRtt({
|
2024-01-25 23:27:53 +08:00
|
|
|
variables: {
|
|
|
|
networkRttInMs: networkRttInMs.current,
|
|
|
|
},
|
|
|
|
});
|
2023-12-05 22:18:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const [updateConnectionAliveAtToMeAsNow] = useMutation(UPDATE_CONNECTION_ALIVE_AT);
|
|
|
|
|
|
|
|
const handleUpdateConnectionAliveAt = () => {
|
2024-01-25 23:27:53 +08:00
|
|
|
const startTime = performance.now();
|
|
|
|
updateConnectionAliveAtToMeAsNow().then(() => {
|
|
|
|
const endTime = performance.now();
|
|
|
|
networkRttInMs.current = endTime - startTime;
|
|
|
|
}).finally(() => {
|
|
|
|
if (timeoutRef.current) {
|
|
|
|
clearTimeout(timeoutRef.current);
|
|
|
|
}
|
2023-12-05 22:18:45 +08:00
|
|
|
|
2024-01-25 23:27:53 +08:00
|
|
|
timeoutRef.current = setTimeout(() => {
|
|
|
|
handleUpdateConnectionAliveAt();
|
|
|
|
}, STATS_INTERVAL);
|
|
|
|
});
|
2023-12-05 22:18:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
handleUpdateConnectionAliveAt();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const { loading, error, data } = useSubscription(CONNECTION_STATUS_SUBSCRIPTION);
|
|
|
|
|
2024-03-12 22:10:59 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (!loading && !error && data) {
|
|
|
|
data.user_connectionStatus.forEach((curr) => {
|
|
|
|
if (curr.connectionAliveAt != null
|
|
|
|
&& curr.userClientResponseAt == null
|
|
|
|
&& (curr.statusUpdatedAt == null
|
|
|
|
|| curr.statusUpdatedAt !== lastStatusUpdatedAtReceived.current
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
lastStatusUpdatedAtReceived.current = curr.statusUpdatedAt;
|
|
|
|
handleUpdateUserClientResponseAt();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [data]);
|
2023-12-05 22:18:45 +08:00
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ConnectionStatus;
|