2024-05-01 19:38:12 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
2024-06-08 04:41:36 +08:00
|
|
|
import { throttle } from 'radash';
|
2024-05-02 03:48:12 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2024-04-20 04:34:43 +08:00
|
|
|
import {
|
|
|
|
VIDEO_STREAMS_SUBSCRIPTION,
|
|
|
|
VideoStreamsResponse,
|
|
|
|
} from './queries';
|
|
|
|
import { setStreams } from './state';
|
2024-05-01 19:38:12 +08:00
|
|
|
import { AdapterProps } from '../../components-data/graphqlToMiniMongoAdapterManager/component';
|
2024-06-10 21:18:47 +08:00
|
|
|
import createUseSubscription from '/imports/ui/core/hooks/createUseSubscription';
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-06-08 04:41:36 +08:00
|
|
|
const throttledSetStreams = throttle({ interval: 500 }, setStreams);
|
|
|
|
|
2024-06-10 21:18:47 +08:00
|
|
|
type SubscriptionData = VideoStreamsResponse['user_camera'][number];
|
|
|
|
|
|
|
|
const useVideoStreamsSubscription = createUseSubscription(
|
|
|
|
VIDEO_STREAMS_SUBSCRIPTION,
|
|
|
|
{},
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
2024-05-01 19:38:12 +08:00
|
|
|
const VideoStreamAdapter: React.FC<AdapterProps> = ({
|
|
|
|
onReady,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const ready = useRef(false);
|
2024-06-10 21:18:47 +08:00
|
|
|
const { data, loading, errors } = useVideoStreamsSubscription();
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-02 03:48:12 +08:00
|
|
|
if (loading) return;
|
|
|
|
|
2024-06-10 21:18:47 +08:00
|
|
|
if (errors) {
|
|
|
|
errors.forEach((error) => {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'video_stream_sub_error',
|
|
|
|
extraInfo: {
|
|
|
|
errorName: error.name,
|
|
|
|
errorMessage: error.message,
|
|
|
|
},
|
|
|
|
}, 'Video streams subscription failed.');
|
|
|
|
});
|
2024-05-02 03:48:12 +08:00
|
|
|
}
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
if (!data) {
|
2024-06-08 04:41:36 +08:00
|
|
|
throttledSetStreams([]);
|
2024-04-20 04:34:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:18:47 +08:00
|
|
|
const streams = (data as SubscriptionData[]).map(({ streamId, user, voice }) => ({
|
2024-04-20 04:34:43 +08:00
|
|
|
stream: streamId,
|
2024-05-08 05:05:36 +08:00
|
|
|
deviceId: streamId.split('_')[3],
|
2024-04-20 04:34:43 +08:00
|
|
|
name: user.name,
|
2024-05-06 21:45:59 +08:00
|
|
|
nameSortable: user.nameSortable,
|
2024-06-08 04:41:36 +08:00
|
|
|
userId: user.userId,
|
|
|
|
user,
|
|
|
|
floor: voice?.floor ?? false,
|
|
|
|
lastFloorTime: voice?.lastFloorTime ?? '0',
|
2024-05-02 03:48:12 +08:00
|
|
|
type: 'stream' as const,
|
2024-04-20 04:34:43 +08:00
|
|
|
}));
|
|
|
|
|
2024-06-08 04:41:36 +08:00
|
|
|
throttledSetStreams(streams);
|
2024-04-20 04:34:43 +08:00
|
|
|
}, [data]);
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
useEffect(() => {
|
2024-06-04 21:40:54 +08:00
|
|
|
if (!ready.current) {
|
2024-05-01 19:38:12 +08:00
|
|
|
ready.current = true;
|
|
|
|
onReady('VideoStreamAdapter');
|
|
|
|
}
|
2024-05-02 03:48:12 +08:00
|
|
|
}, [loading]);
|
2024-05-01 19:38:12 +08:00
|
|
|
|
|
|
|
return children;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default VideoStreamAdapter;
|