Skip to content

Commit 6872595

Browse files
committed
feat: refactor state management in VideoRoomPage to improve local state updates and WebSocket communication
1 parent e3a5317 commit 6872595

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

web/src/pages/VideoRoomPage.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,28 @@ const VideoRoomPage = memo(function VideoRoomPage() {
8282
useEffect(() => {
8383
if (!token || !room || !artRef.current) return;
8484

85-
const sendState = () => {
85+
const updateLocalState = () => {
8686
const art = artInstance.current;
87-
const ws = wsRef.current;
88-
if (!art || !ws || ws.readyState !== WebSocket.OPEN || applyingRemoteRef.current) return;
87+
if (!art || applyingRemoteRef.current) return null;
8988
const video = art.video;
90-
const payload = {
91-
type: 'state',
89+
const state: VideoRoomState = {
9290
current_time: video.currentTime || 0,
9391
paused: video.paused,
92+
updated_at: new Date().toISOString(),
93+
};
94+
liveStateRef.current = state;
95+
setLiveState(state);
96+
return state;
97+
};
98+
99+
const sendState = () => {
100+
const ws = wsRef.current;
101+
const state = updateLocalState();
102+
if (!state || !ws || ws.readyState !== WebSocket.OPEN) return;
103+
const payload = {
104+
type: 'state',
105+
current_time: state.current_time,
106+
paused: state.paused,
94107
};
95108
ws.send(JSON.stringify(payload));
96109
};
@@ -145,7 +158,6 @@ const VideoRoomPage = memo(function VideoRoomPage() {
145158

146159
art.on('ready', applyLatestState);
147160
art.video.addEventListener('loadedmetadata', applyLatestState);
148-
art.video.addEventListener('canplay', applyLatestState);
149161
art.on('play', sendStateSoon);
150162
art.on('pause', sendStateSoon);
151163
art.on('seek', sendStateSoon);
@@ -172,7 +184,6 @@ const VideoRoomPage = memo(function VideoRoomPage() {
172184
sendTimerRef.current = null;
173185
}
174186
art.video.removeEventListener('loadedmetadata', applyLatestState);
175-
art.video.removeEventListener('canplay', applyLatestState);
176187
ws.close();
177188
art.destroy();
178189
wsRef.current = null;

0 commit comments

Comments
 (0)