react-realtime-hooks 1.4.1 → 1.4.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
@@ -1,4 +1,4 @@
1
- import { useSyncExternalStore, useRef, useState, useEffect, useMemo, useInsertionEffect, useCallback } from 'react';
1
+ import { useSyncExternalStore, useRef, useState, useEffect, useInsertionEffect, useMemo, useCallback } from 'react';
2
2
 
3
3
  // src/hooks/useOnlineStatus.ts
4
4
 
@@ -464,7 +464,9 @@ var useReconnect = (options = {}) => {
464
464
  () => createInitialState(normalizedOptions.enabled)
465
465
  );
466
466
  const stateRef = useRef(state);
467
- stateRef.current = state;
467
+ useInsertionEffect(() => {
468
+ stateRef.current = state;
469
+ });
468
470
  const commitState = (next) => {
469
471
  const resolved = typeof next === "function" ? next(stateRef.current) : next;
470
472
  stateRef.current = resolved;
@@ -597,7 +599,9 @@ var useHeartbeat = (options) => {
597
599
  () => createInitialState2(enabled && startOnMount)
598
600
  );
599
601
  const stateRef = useRef(state);
600
- stateRef.current = state;
602
+ useInsertionEffect(() => {
603
+ stateRef.current = state;
604
+ });
601
605
  const commitState = (next) => {
602
606
  const resolved = typeof next === "function" ? next(stateRef.current) : next;
603
607
  stateRef.current = resolved;
@@ -936,11 +940,14 @@ var useWebSocket = (options) => {
936
940
  const nextSocketEpochRef = useRef(0);
937
941
  const controller = useWebSocketController();
938
942
  const [openNonce, setOpenNonce] = useState(0);
943
+ const initialStatus = !supported || resolvedUrl === null ? "closed" : connect ? "connecting" : "idle";
939
944
  const [state, setState] = useState(
940
- () => createInitialState4(connect ? "connecting" : "idle")
945
+ () => createInitialState4(initialStatus)
941
946
  );
942
947
  const stateRef = useRef(state);
943
- stateRef.current = state;
948
+ useInsertionEffect(() => {
949
+ stateRef.current = state;
950
+ });
944
951
  const reconnectEnabled = options.reconnect !== false && supported && resolvedUrl !== null;
945
952
  const reconnect = useReconnect(
946
953
  options.reconnect === false ? { enabled: false } : {
@@ -1509,11 +1516,14 @@ var useEventSource = (options) => {
1509
1516
  const suppressReconnectRef = useRef(false);
1510
1517
  const terminalErrorRef = useRef(null);
1511
1518
  const [openNonce, setOpenNonce] = useState(0);
1519
+ const initialStatus = !supported || resolvedUrl === null ? "closed" : connect ? "connecting" : "idle";
1512
1520
  const [state, setState] = useState(
1513
- () => createInitialState5(connect ? "connecting" : "idle")
1521
+ () => createInitialState5(initialStatus)
1514
1522
  );
1515
1523
  const stateRef = useRef(state);
1516
- stateRef.current = state;
1524
+ useInsertionEffect(() => {
1525
+ stateRef.current = state;
1526
+ });
1517
1527
  const reconnectEnabled = options.reconnect !== false && supported && resolvedUrl !== null;
1518
1528
  const reconnect = useReconnect(
1519
1529
  options.reconnect === false ? { enabled: false } : {