-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuseConnectionEffect.tsx
175 lines (167 loc) · 5.99 KB
/
useConnectionEffect.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { type ChannelMembers, type EventId, isServerEvent, type ServerEvent, type UserStatus } from '@boluo/api';
import { webSocketUrlAtom } from '@boluo/common';
import { useAtomValue, useSetAtom, useStore } from 'jotai';
import { useCallback, useEffect } from 'react';
import { useSWRConfig } from 'swr';
import { isUuid } from '@boluo/utils';
import { PING, PONG } from '../const';
import { chatAtom, type ChatDispatch, connectionStateAtom } from '../state/chat.atoms';
import { type ConnectionState } from '../state/connection.reducer';
import { recordError } from '../error';
let lastPongTime = Date.now();
const RELOAD_TIMEOUT = 1000 * 60 * 30;
const createMailboxConnection = (baseUrl: string, id: string, token?: string | null, after?: EventId): WebSocket => {
const paramsObject: Record<string, string> = { mailbox: id };
if (token) paramsObject.token = token;
if (after) {
paramsObject.after = after.timestamp.toString();
paramsObject.seq = after.seq.toString();
}
const params = new URLSearchParams(paramsObject);
const url = `${baseUrl}/events/connect?${params.toString()}`;
return new WebSocket(url);
};
const connect = (
webSocketEndpoint: string,
mailboxId: string,
connectionState: ConnectionState,
after: EventId,
onEvent: (event: ServerEvent) => void,
dispatch: ChatDispatch,
token: string | undefined | null,
): WebSocket | null => {
if (!isUuid(mailboxId)) return null;
if (connectionState.type !== 'CLOSED') return null;
if (connectionState.countdown > 0) {
setTimeout(() => dispatch({ type: 'reconnectCountdownTick', payload: {} }), 1000);
return null;
}
if (Date.now() - lastPongTime > RELOAD_TIMEOUT) {
alert('Connection lost due to inactivity.');
dispatch({ type: 'resetChatState', payload: {} });
return null;
}
dispatch({ type: 'connecting', payload: { mailboxId } });
const newConnection = createMailboxConnection(webSocketEndpoint, mailboxId, token, after);
newConnection.onopen = (_) => {
console.info(`connection established for ${mailboxId}`);
dispatch({ type: 'connected', payload: { connection: newConnection, mailboxId } });
};
newConnection.onclose = (event) => {
console.info(`connection closed for ${mailboxId}`, event);
dispatch({ type: 'connectionClosed', payload: { mailboxId, random: Math.random() } });
};
newConnection.onmessage = (message: MessageEvent<unknown>) => {
const raw = message.data;
if (raw === PING) {
newConnection.send(PONG);
lastPongTime = Date.now();
return;
}
if (!raw || typeof raw !== 'string' || raw === PONG) return;
let event: unknown = null;
try {
event = JSON.parse(raw);
} catch {
return;
}
if (!isServerEvent(event)) return;
let eventList: ServerEvent[];
if (event.body.type === 'BATCH') {
eventList = event.body.encodedEvents.flatMap((encodedEvent) => {
try {
return [JSON.parse(encodedEvent) as ServerEvent];
} catch {
recordError('Failed to parse event', { event: encodedEvent });
return [];
}
});
} else {
eventList = [event];
}
for (const event of eventList) {
dispatch({ type: 'eventFromServer', payload: event });
onEvent(event);
}
};
return newConnection;
};
export const useConnectionEffect = (mailboxId: string, isTokenLoading: boolean, token: string | undefined | null) => {
const { mutate } = useSWRConfig();
const webSocketEndpoint = useAtomValue(webSocketUrlAtom);
const store = useStore();
const dispatch = useSetAtom(chatAtom);
const onEvent = useCallback(
(event: ServerEvent) => {
switch (event.body.type) {
case 'CHANNEL_DELETED':
void mutate(['/channels/by_space', event.mailbox]);
void mutate(['/channels/query', event.body.channelId]);
return;
case 'CHANNEL_EDITED':
void mutate(['/channels/by_space', event.mailbox]);
void mutate(['/channels/query', event.body.channelId], event.body.channel);
return;
case 'SPACE_UPDATED':
const { space } = event.body.spaceWithRelated;
void mutate(['/spaces/query', space.id], space);
void mutate(['/channels/by_space', space.id]);
return;
case 'MEMBERS':
const members = event.body.members;
void mutate<ChannelMembers>(['/channels/members', event.body.channelId], (channelMembers) => {
if (channelMembers != null) {
return { ...channelMembers, members };
}
});
return;
case 'STATUS_MAP':
void mutate<Record<string, UserStatus | undefined>>(
['/spaces/users_status', event.body.spaceId],
event.body.statusMap,
);
return;
case 'ERROR':
dispatch({ type: 'connectionError', payload: { mailboxId, code: event.body.code ?? 'UNEXPECTED' } });
return;
case 'BATCH':
case 'NEW_MESSAGE':
case 'MESSAGE_DELETED':
case 'MESSAGE_EDITED':
case 'MESSAGE_PREVIEW':
case 'INITIALIZED':
case 'APP_UPDATED':
return;
}
},
[dispatch, mailboxId, mutate],
);
useEffect(() => {
if (mailboxId === '') return;
if (isTokenLoading) return;
const chatState = store.get(chatAtom);
let ws: WebSocket | null = null;
const unsub = store.sub(connectionStateAtom, () => {
const chatState = store.get(chatAtom);
ws = connect(webSocketEndpoint, mailboxId, chatState.connection, chatState.lastEventId, onEvent, dispatch, token);
});
const handle = window.setTimeout(() => {
if (ws == null) {
ws = connect(
webSocketEndpoint,
mailboxId,
chatState.connection,
chatState.lastEventId,
onEvent,
dispatch,
token,
);
}
});
return () => {
window.clearTimeout(handle);
unsub();
if (ws) ws.close();
};
}, [onEvent, mailboxId, store, webSocketEndpoint, dispatch, token, isTokenLoading]);
};