react-realtime-hooks 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.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;
@@ -1299,7 +1306,6 @@ var useWebSocket = (options) => {
1299
1306
  status: reconnect.status === "running" || reconnect.status === "scheduled" ? "reconnecting" : "connecting"
1300
1307
  }));
1301
1308
  let cancelled = false;
1302
- let detachListeners = null;
1303
1309
  queueMicrotask(() => {
1304
1310
  if (cancelled) {
1305
1311
  return;
@@ -1313,6 +1319,8 @@ var useWebSocket = (options) => {
1313
1319
  return;
1314
1320
  }
1315
1321
  const socket2 = new WebSocket(resolvedUrl, protocols);
1322
+ detachSocketListenersRef.current?.();
1323
+ detachSocketListenersRef.current = null;
1316
1324
  const socketEpoch = nextSocketEpochRef.current + 1;
1317
1325
  socketRef.current = socket2;
1318
1326
  socketKeyRef.current = nextSocketKey;
@@ -1346,7 +1354,7 @@ var useWebSocket = (options) => {
1346
1354
  socket2.addEventListener("message", handleSocketMessage);
1347
1355
  socket2.addEventListener("error", handleSocketError);
1348
1356
  socket2.addEventListener("close", handleSocketClose);
1349
- detachListeners = () => {
1357
+ detachSocketListenersRef.current = () => {
1350
1358
  socket2.removeEventListener("open", handleSocketOpen);
1351
1359
  socket2.removeEventListener("message", handleSocketMessage);
1352
1360
  socket2.removeEventListener("error", handleSocketError);
@@ -1355,7 +1363,6 @@ var useWebSocket = (options) => {
1355
1363
  });
1356
1364
  return () => {
1357
1365
  cancelled = true;
1358
- detachListeners?.();
1359
1366
  };
1360
1367
  }, [
1361
1368
  closeSocket,
@@ -1376,6 +1383,8 @@ var useWebSocket = (options) => {
1376
1383
  supported
1377
1384
  ]);
1378
1385
  useEffect(() => () => {
1386
+ detachSocketListenersRef.current?.();
1387
+ detachSocketListenersRef.current = null;
1379
1388
  controller.noteEffectInitiatedClose();
1380
1389
  socketKeyRef.current = null;
1381
1390
  activeSocketEpochRef.current = null;
@@ -1520,6 +1529,7 @@ var useEventSource = (options) => {
1520
1529
  [eventsDependency]
1521
1530
  );
1522
1531
  const eventSourceRef = useRef(null);
1532
+ const detachEventSourceListenersRef = useRef(null);
1523
1533
  const eventSourceKeyRef = useRef(null);
1524
1534
  const activeEventSourceEpochRef = useRef(null);
1525
1535
  const nextEventSourceEpochRef = useRef(0);
@@ -1557,6 +1567,8 @@ var useEventSource = (options) => {
1557
1567
  eventSourceRef.current = null;
1558
1568
  eventSourceKeyRef.current = null;
1559
1569
  activeEventSourceEpochRef.current = null;
1570
+ detachEventSourceListenersRef.current?.();
1571
+ detachEventSourceListenersRef.current = null;
1560
1572
  source.close();
1561
1573
  });
1562
1574
  const isActiveEventSourceEvent = useStableCallback((sourceEpoch) => {
@@ -1727,7 +1739,6 @@ var useEventSource = (options) => {
1727
1739
  status: reconnect.status === "running" || reconnect.status === "scheduled" ? "reconnecting" : "connecting"
1728
1740
  }));
1729
1741
  let cancelled = false;
1730
- let detachListeners = null;
1731
1742
  queueMicrotask(() => {
1732
1743
  if (cancelled) {
1733
1744
  return;
@@ -1777,7 +1788,7 @@ var useEventSource = (options) => {
1777
1788
  source.addEventListener(eventName, handler);
1778
1789
  }
1779
1790
  source.addEventListener("error", handleSourceError);
1780
- detachListeners = () => {
1791
+ detachEventSourceListenersRef.current = () => {
1781
1792
  source.removeEventListener("open", handleSourceOpen);
1782
1793
  source.removeEventListener("message", handleSourceMessage);
1783
1794
  for (const [eventName, handler] of namedEventHandlers) {
@@ -1788,7 +1799,6 @@ var useEventSource = (options) => {
1788
1799
  });
1789
1800
  return () => {
1790
1801
  cancelled = true;
1791
- detachListeners?.();
1792
1802
  };
1793
1803
  }, [
1794
1804
  closeEventSource,
@@ -1807,6 +1817,8 @@ var useEventSource = (options) => {
1807
1817
  supported
1808
1818
  ]);
1809
1819
  useEffect(() => () => {
1820
+ detachEventSourceListenersRef.current?.();
1821
+ detachEventSourceListenersRef.current = null;
1810
1822
  suppressReconnectRef.current = true;
1811
1823
  eventSourceKeyRef.current = null;
1812
1824
  activeEventSourceEpochRef.current = null;