@xcelsior/ui-chat 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useWebSocket.ts +18 -1
package/dist/index.mjs
CHANGED
|
@@ -11,6 +11,7 @@ function useWebSocket(config, externalWebSocket) {
|
|
|
11
11
|
const reconnectTimeoutRef = useRef(null);
|
|
12
12
|
const reconnectAttemptsRef = useRef(0);
|
|
13
13
|
const messageHandlerRef = useRef(null);
|
|
14
|
+
const abortedRef = useRef(false);
|
|
14
15
|
const maxReconnectAttempts = 5;
|
|
15
16
|
const reconnectDelay = 3e3;
|
|
16
17
|
const isUsingExternalWs = !!externalWebSocket;
|
|
@@ -43,6 +44,7 @@ function useWebSocket(config, externalWebSocket) {
|
|
|
43
44
|
};
|
|
44
45
|
}, []);
|
|
45
46
|
const connect = useCallback(() => {
|
|
47
|
+
if (abortedRef.current) return;
|
|
46
48
|
console.log("connecting to WebSocket...", config.currentUser, config.conversationId);
|
|
47
49
|
try {
|
|
48
50
|
if (wsRef.current) {
|
|
@@ -62,6 +64,10 @@ function useWebSocket(config, externalWebSocket) {
|
|
|
62
64
|
}
|
|
63
65
|
const ws = new WebSocket(url.toString());
|
|
64
66
|
ws.onopen = () => {
|
|
67
|
+
if (abortedRef.current) {
|
|
68
|
+
ws.close(1e3, "Effect cleaned up");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
65
71
|
console.log("WebSocket connected");
|
|
66
72
|
setIsConnected(true);
|
|
67
73
|
setError(null);
|
|
@@ -69,12 +75,14 @@ function useWebSocket(config, externalWebSocket) {
|
|
|
69
75
|
config.onConnectionChange?.(true);
|
|
70
76
|
};
|
|
71
77
|
ws.onerror = (event) => {
|
|
78
|
+
if (abortedRef.current) return;
|
|
72
79
|
console.error("WebSocket error:", event);
|
|
73
80
|
const err = new Error("WebSocket connection error");
|
|
74
81
|
setError(err);
|
|
75
82
|
config.onError?.(err);
|
|
76
83
|
};
|
|
77
84
|
ws.onclose = (event) => {
|
|
85
|
+
if (abortedRef.current) return;
|
|
78
86
|
console.log("WebSocket closed:", event.code, event.reason);
|
|
79
87
|
setIsConnected(false);
|
|
80
88
|
config.onConnectionChange?.(false);
|
|
@@ -123,6 +131,7 @@ function useWebSocket(config, externalWebSocket) {
|
|
|
123
131
|
);
|
|
124
132
|
const reconnect = useCallback(() => {
|
|
125
133
|
reconnectAttemptsRef.current = 0;
|
|
134
|
+
abortedRef.current = false;
|
|
126
135
|
connect();
|
|
127
136
|
}, [connect]);
|
|
128
137
|
useEffect(() => {
|
|
@@ -132,8 +141,10 @@ function useWebSocket(config, externalWebSocket) {
|
|
|
132
141
|
const cleanup = subscribeToMessage(externalWebSocket);
|
|
133
142
|
return cleanup;
|
|
134
143
|
}
|
|
144
|
+
abortedRef.current = false;
|
|
135
145
|
connect();
|
|
136
146
|
return () => {
|
|
147
|
+
abortedRef.current = true;
|
|
137
148
|
if (reconnectTimeoutRef.current) {
|
|
138
149
|
clearTimeout(reconnectTimeoutRef.current);
|
|
139
150
|
}
|