2024-04-20 04:34:43 +08:00
|
|
|
import { makeVar, useReactiveVar } from '@apollo/client';
|
2024-06-08 04:41:36 +08:00
|
|
|
import type { ConnectingStream, Stream } from './types';
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
interface State {
|
|
|
|
isConnecting: boolean;
|
|
|
|
isConnected: boolean;
|
|
|
|
currentVideoPageIndex: number;
|
|
|
|
numberOfPages: number;
|
|
|
|
pageSize: number;
|
|
|
|
userId: string | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const videoState = makeVar<State>({
|
2024-04-20 04:34:43 +08:00
|
|
|
isConnecting: false,
|
|
|
|
isConnected: false,
|
|
|
|
currentVideoPageIndex: 0,
|
|
|
|
numberOfPages: 0,
|
|
|
|
pageSize: 0,
|
|
|
|
userId: null,
|
|
|
|
});
|
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
const useVideoState = () => useReactiveVar(videoState);
|
|
|
|
|
|
|
|
const setVideoState = (state: Partial<State>) => {
|
|
|
|
videoState({
|
|
|
|
...videoState(),
|
|
|
|
...state,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-04-20 04:34:43 +08:00
|
|
|
const getVideoState = () => videoState();
|
|
|
|
|
2024-06-07 20:52:16 +08:00
|
|
|
const connectingStream = makeVar<ConnectingStream | null>(null);
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
const useConnectingStream = (streams?: Stream[]) => {
|
2024-04-20 04:34:43 +08:00
|
|
|
const connecting = useReactiveVar(connectingStream);
|
|
|
|
|
|
|
|
if (!connecting) return null;
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
const hasStream = streams && streams.find((s) => s.stream === connecting.stream);
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
if (hasStream) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return connecting;
|
|
|
|
};
|
|
|
|
|
2024-06-07 20:52:16 +08:00
|
|
|
const setConnectingStream = (stream: ConnectingStream | null) => {
|
2024-04-20 04:34:43 +08:00
|
|
|
connectingStream(stream);
|
|
|
|
};
|
|
|
|
|
2024-06-07 20:52:16 +08:00
|
|
|
const getConnectingStream = () => connectingStream();
|
|
|
|
|
2024-04-20 04:34:43 +08:00
|
|
|
export {
|
|
|
|
useVideoState,
|
|
|
|
setVideoState,
|
|
|
|
getVideoState,
|
|
|
|
useConnectingStream,
|
2024-06-07 20:52:16 +08:00
|
|
|
getConnectingStream,
|
2024-04-20 04:34:43 +08:00
|
|
|
setConnectingStream,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
useVideoState,
|
|
|
|
setVideoState,
|
|
|
|
getVideoState,
|
|
|
|
useConnectingStream,
|
2024-06-07 20:52:16 +08:00
|
|
|
getConnectingStream,
|
2024-04-20 04:34:43 +08:00
|
|
|
setConnectingStream,
|
|
|
|
};
|