react-realtime-hooks 2.0.1 → 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/README.md +1 -0
- package/dist/index.cjs +37 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +37 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -937,6 +937,7 @@ var useWebSocket = (options) => {
|
|
|
937
937
|
[protocolsDependency]
|
|
938
938
|
);
|
|
939
939
|
const socketRef = useRef(null);
|
|
940
|
+
const detachSocketListenersRef = useRef(null);
|
|
940
941
|
const socketKeyRef = useRef(null);
|
|
941
942
|
const activeSocketEpochRef = useRef(null);
|
|
942
943
|
const closingSocketEpochRef = useRef(null);
|
|
@@ -1049,6 +1050,10 @@ var useWebSocket = (options) => {
|
|
|
1049
1050
|
socketKeyRef.current = null;
|
|
1050
1051
|
activeSocketEpochRef.current = null;
|
|
1051
1052
|
closingSocketEpochRef.current = config.trackClose ? socketEpoch : null;
|
|
1053
|
+
if (!config.trackClose) {
|
|
1054
|
+
detachSocketListenersRef.current?.();
|
|
1055
|
+
detachSocketListenersRef.current = null;
|
|
1056
|
+
}
|
|
1052
1057
|
if (isSocketActive(socket2)) {
|
|
1053
1058
|
socket2.close(config.code, config.reason);
|
|
1054
1059
|
}
|
|
@@ -1160,6 +1165,8 @@ var useWebSocket = (options) => {
|
|
|
1160
1165
|
if (!shouldHandleSocketClose(socketEpoch)) {
|
|
1161
1166
|
return;
|
|
1162
1167
|
}
|
|
1168
|
+
detachSocketListenersRef.current?.();
|
|
1169
|
+
detachSocketListenersRef.current = null;
|
|
1163
1170
|
if (activeSocketEpochRef.current === socketEpoch) {
|
|
1164
1171
|
socketRef.current = null;
|
|
1165
1172
|
socketKeyRef.current = null;
|
|
@@ -1230,10 +1237,12 @@ var useWebSocket = (options) => {
|
|
|
1230
1237
|
controller.noteUserCloseRequested();
|
|
1231
1238
|
reconnect.cancel();
|
|
1232
1239
|
heartbeat.stop();
|
|
1240
|
+
const socket2 = socketRef.current;
|
|
1241
|
+
const hasInFlightSocket = socket2 !== null && (socket2.readyState === WebSocket.CONNECTING || socket2.readyState === WebSocket.OPEN);
|
|
1233
1242
|
commitState((current) => ({
|
|
1234
1243
|
...current,
|
|
1235
1244
|
lastChangedAt: Date.now(),
|
|
1236
|
-
status: "closing"
|
|
1245
|
+
status: hasInFlightSocket ? "closing" : "closed"
|
|
1237
1246
|
}));
|
|
1238
1247
|
closeSocket({
|
|
1239
1248
|
code,
|
|
@@ -1297,12 +1306,21 @@ var useWebSocket = (options) => {
|
|
|
1297
1306
|
status: reconnect.status === "running" || reconnect.status === "scheduled" ? "reconnecting" : "connecting"
|
|
1298
1307
|
}));
|
|
1299
1308
|
let cancelled = false;
|
|
1300
|
-
let detachListeners = null;
|
|
1301
1309
|
queueMicrotask(() => {
|
|
1302
1310
|
if (cancelled) {
|
|
1303
1311
|
return;
|
|
1304
1312
|
}
|
|
1313
|
+
if (controller.hasManualCloseRequested()) {
|
|
1314
|
+
commitState((current) => ({
|
|
1315
|
+
...current,
|
|
1316
|
+
lastChangedAt: Date.now(),
|
|
1317
|
+
status: "closed"
|
|
1318
|
+
}));
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1305
1321
|
const socket2 = new WebSocket(resolvedUrl, protocols);
|
|
1322
|
+
detachSocketListenersRef.current?.();
|
|
1323
|
+
detachSocketListenersRef.current = null;
|
|
1306
1324
|
const socketEpoch = nextSocketEpochRef.current + 1;
|
|
1307
1325
|
socketRef.current = socket2;
|
|
1308
1326
|
socketKeyRef.current = nextSocketKey;
|
|
@@ -1336,7 +1354,7 @@ var useWebSocket = (options) => {
|
|
|
1336
1354
|
socket2.addEventListener("message", handleSocketMessage);
|
|
1337
1355
|
socket2.addEventListener("error", handleSocketError);
|
|
1338
1356
|
socket2.addEventListener("close", handleSocketClose);
|
|
1339
|
-
|
|
1357
|
+
detachSocketListenersRef.current = () => {
|
|
1340
1358
|
socket2.removeEventListener("open", handleSocketOpen);
|
|
1341
1359
|
socket2.removeEventListener("message", handleSocketMessage);
|
|
1342
1360
|
socket2.removeEventListener("error", handleSocketError);
|
|
@@ -1345,7 +1363,6 @@ var useWebSocket = (options) => {
|
|
|
1345
1363
|
});
|
|
1346
1364
|
return () => {
|
|
1347
1365
|
cancelled = true;
|
|
1348
|
-
detachListeners?.();
|
|
1349
1366
|
};
|
|
1350
1367
|
}, [
|
|
1351
1368
|
closeSocket,
|
|
@@ -1366,6 +1383,8 @@ var useWebSocket = (options) => {
|
|
|
1366
1383
|
supported
|
|
1367
1384
|
]);
|
|
1368
1385
|
useEffect(() => () => {
|
|
1386
|
+
detachSocketListenersRef.current?.();
|
|
1387
|
+
detachSocketListenersRef.current = null;
|
|
1369
1388
|
controller.noteEffectInitiatedClose();
|
|
1370
1389
|
socketKeyRef.current = null;
|
|
1371
1390
|
activeSocketEpochRef.current = null;
|
|
@@ -1510,6 +1529,7 @@ var useEventSource = (options) => {
|
|
|
1510
1529
|
[eventsDependency]
|
|
1511
1530
|
);
|
|
1512
1531
|
const eventSourceRef = useRef(null);
|
|
1532
|
+
const detachEventSourceListenersRef = useRef(null);
|
|
1513
1533
|
const eventSourceKeyRef = useRef(null);
|
|
1514
1534
|
const activeEventSourceEpochRef = useRef(null);
|
|
1515
1535
|
const nextEventSourceEpochRef = useRef(0);
|
|
@@ -1547,6 +1567,8 @@ var useEventSource = (options) => {
|
|
|
1547
1567
|
eventSourceRef.current = null;
|
|
1548
1568
|
eventSourceKeyRef.current = null;
|
|
1549
1569
|
activeEventSourceEpochRef.current = null;
|
|
1570
|
+
detachEventSourceListenersRef.current?.();
|
|
1571
|
+
detachEventSourceListenersRef.current = null;
|
|
1550
1572
|
source.close();
|
|
1551
1573
|
});
|
|
1552
1574
|
const isActiveEventSourceEvent = useStableCallback((sourceEpoch) => {
|
|
@@ -1717,11 +1739,18 @@ var useEventSource = (options) => {
|
|
|
1717
1739
|
status: reconnect.status === "running" || reconnect.status === "scheduled" ? "reconnecting" : "connecting"
|
|
1718
1740
|
}));
|
|
1719
1741
|
let cancelled = false;
|
|
1720
|
-
let detachListeners = null;
|
|
1721
1742
|
queueMicrotask(() => {
|
|
1722
1743
|
if (cancelled) {
|
|
1723
1744
|
return;
|
|
1724
1745
|
}
|
|
1746
|
+
if (manualCloseRef.current) {
|
|
1747
|
+
commitState((current) => ({
|
|
1748
|
+
...current,
|
|
1749
|
+
lastChangedAt: Date.now(),
|
|
1750
|
+
status: "closed"
|
|
1751
|
+
}));
|
|
1752
|
+
return;
|
|
1753
|
+
}
|
|
1725
1754
|
const source = new EventSource(resolvedUrl, {
|
|
1726
1755
|
withCredentials: options.withCredentials ?? false
|
|
1727
1756
|
});
|
|
@@ -1759,7 +1788,7 @@ var useEventSource = (options) => {
|
|
|
1759
1788
|
source.addEventListener(eventName, handler);
|
|
1760
1789
|
}
|
|
1761
1790
|
source.addEventListener("error", handleSourceError);
|
|
1762
|
-
|
|
1791
|
+
detachEventSourceListenersRef.current = () => {
|
|
1763
1792
|
source.removeEventListener("open", handleSourceOpen);
|
|
1764
1793
|
source.removeEventListener("message", handleSourceMessage);
|
|
1765
1794
|
for (const [eventName, handler] of namedEventHandlers) {
|
|
@@ -1770,7 +1799,6 @@ var useEventSource = (options) => {
|
|
|
1770
1799
|
});
|
|
1771
1800
|
return () => {
|
|
1772
1801
|
cancelled = true;
|
|
1773
|
-
detachListeners?.();
|
|
1774
1802
|
};
|
|
1775
1803
|
}, [
|
|
1776
1804
|
closeEventSource,
|
|
@@ -1789,6 +1817,8 @@ var useEventSource = (options) => {
|
|
|
1789
1817
|
supported
|
|
1790
1818
|
]);
|
|
1791
1819
|
useEffect(() => () => {
|
|
1820
|
+
detachEventSourceListenersRef.current?.();
|
|
1821
|
+
detachEventSourceListenersRef.current = null;
|
|
1792
1822
|
suppressReconnectRef.current = true;
|
|
1793
1823
|
eventSourceKeyRef.current = null;
|
|
1794
1824
|
activeEventSourceEpochRef.current = null;
|