react-realtime-hooks 1.4.2 → 2.0.0

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.cts CHANGED
@@ -76,6 +76,11 @@ interface ReconnectDelayContext {
76
76
  type ReconnectDelayStrategy = (context: ReconnectDelayContext) => number;
77
77
  interface UseReconnectOptions {
78
78
  enabled?: boolean;
79
+ /**
80
+ * Maximum number of reconnect attempts before the hook gives up and
81
+ * transitions to `"stopped"`. Default is `10` (changed from `null` in
82
+ * 2.0). Pass `null` explicitly to retry indefinitely.
83
+ */
79
84
  maxAttempts?: number | null;
80
85
  initialDelayMs?: number;
81
86
  maxDelayMs?: number;
@@ -107,7 +112,12 @@ type HeartbeatAckMatcher<TMessage> = (message: TMessage) => boolean;
107
112
  interface UseHeartbeatOptions<TOutgoing = unknown, TIncoming = TOutgoing> {
108
113
  enabled?: boolean;
109
114
  intervalMs: number;
110
- timeoutMs?: number;
115
+ /**
116
+ * Time to wait for an ack after a beat before flipping `hasTimedOut` to
117
+ * `true`. Default is `10_000` (10 seconds), changed from no default in
118
+ * 2.0. Pass `null` explicitly to disable the timeout entirely.
119
+ */
120
+ timeoutMs?: number | null;
111
121
  message?: TOutgoing | (() => TOutgoing);
112
122
  beat?: HeartbeatBeatFn;
113
123
  matchesAck?: HeartbeatAckMatcher<TIncoming>;
package/dist/index.d.ts CHANGED
@@ -76,6 +76,11 @@ interface ReconnectDelayContext {
76
76
  type ReconnectDelayStrategy = (context: ReconnectDelayContext) => number;
77
77
  interface UseReconnectOptions {
78
78
  enabled?: boolean;
79
+ /**
80
+ * Maximum number of reconnect attempts before the hook gives up and
81
+ * transitions to `"stopped"`. Default is `10` (changed from `null` in
82
+ * 2.0). Pass `null` explicitly to retry indefinitely.
83
+ */
79
84
  maxAttempts?: number | null;
80
85
  initialDelayMs?: number;
81
86
  maxDelayMs?: number;
@@ -107,7 +112,12 @@ type HeartbeatAckMatcher<TMessage> = (message: TMessage) => boolean;
107
112
  interface UseHeartbeatOptions<TOutgoing = unknown, TIncoming = TOutgoing> {
108
113
  enabled?: boolean;
109
114
  intervalMs: number;
110
- timeoutMs?: number;
115
+ /**
116
+ * Time to wait for an ack after a beat before flipping `hasTimedOut` to
117
+ * `true`. Default is `10_000` (10 seconds), changed from no default in
118
+ * 2.0. Pass `null` explicitly to disable the timeout entirely.
119
+ */
120
+ timeoutMs?: number | null;
111
121
  message?: TOutgoing | (() => TOutgoing);
112
122
  beat?: HeartbeatBeatFn;
113
123
  matchesAck?: HeartbeatAckMatcher<TIncoming>;
package/dist/index.js CHANGED
@@ -313,6 +313,7 @@ var DEFAULT_RECONNECT_OPTIONS = {
313
313
  enabled: true,
314
314
  initialDelayMs: 1e3,
315
315
  jitterRatio: 0.2,
316
+ maxAttempts: 10,
316
317
  maxDelayMs: 3e4,
317
318
  resetOnSuccess: true
318
319
  };
@@ -364,7 +365,7 @@ var normalizeReconnectOptions = (options) => {
364
365
  jitterRatio: sanitizeJitterRatio(
365
366
  options?.jitterRatio ?? DEFAULT_RECONNECT_OPTIONS.jitterRatio
366
367
  ),
367
- maxAttempts: sanitizeMaxAttempts(options?.maxAttempts),
368
+ maxAttempts: options?.maxAttempts === void 0 ? DEFAULT_RECONNECT_OPTIONS.maxAttempts : sanitizeMaxAttempts(options.maxAttempts),
368
369
  maxDelayMs,
369
370
  resetOnSuccess: options?.resetOnSuccess ?? DEFAULT_RECONNECT_OPTIONS.resetOnSuccess
370
371
  };
@@ -582,6 +583,7 @@ var useReconnect = (options = {}) => {
582
583
  status: state.status
583
584
  };
584
585
  };
586
+ var DEFAULT_HEARTBEAT_TIMEOUT_MS = 1e4;
585
587
  var createInitialState2 = (isRunning) => ({
586
588
  hasTimedOut: false,
587
589
  isRunning,
@@ -615,13 +617,14 @@ var useHeartbeat = (options) => {
615
617
  options.onTimeout?.();
616
618
  });
617
619
  const scheduleTimeout = useStableCallback(() => {
618
- if (options.timeoutMs === void 0) {
620
+ const timeoutMs = options.timeoutMs === void 0 ? DEFAULT_HEARTBEAT_TIMEOUT_MS : options.timeoutMs;
621
+ if (timeoutMs === null) {
619
622
  timeoutRef.current.cancel();
620
623
  return;
621
624
  }
622
625
  timeoutRef.current.schedule(() => {
623
626
  handleTimeout();
624
- }, options.timeoutMs);
627
+ }, timeoutMs);
625
628
  });
626
629
  const handleBeatSuccess = useStableCallback((performedAt) => {
627
630
  commitState((current) => ({
@@ -940,8 +943,9 @@ var useWebSocket = (options) => {
940
943
  const nextSocketEpochRef = useRef(0);
941
944
  const controller = useWebSocketController();
942
945
  const [openNonce, setOpenNonce] = useState(0);
946
+ const initialStatus = !supported || resolvedUrl === null ? "closed" : connect ? "connecting" : "idle";
943
947
  const [state, setState] = useState(
944
- () => createInitialState4(connect ? "connecting" : "idle")
948
+ () => createInitialState4(initialStatus)
945
949
  );
946
950
  const stateRef = useRef(state);
947
951
  useInsertionEffect(() => {
@@ -983,7 +987,7 @@ var useWebSocket = (options) => {
983
987
  startOnMount: false
984
988
  };
985
989
  if (heartbeatConfig !== null && heartbeatConfig.timeoutMs !== void 0) {
986
- heartbeatHookOptions.timeoutMs = heartbeatConfig.timeoutMs;
990
+ heartbeatHookOptions.timeoutMs = heartbeatConfig.timeoutMs ?? null;
987
991
  }
988
992
  if (heartbeatConfig !== null && heartbeatConfig.matchesAck !== void 0) {
989
993
  heartbeatHookOptions.matchesAck = heartbeatConfig.matchesAck;
@@ -1515,8 +1519,9 @@ var useEventSource = (options) => {
1515
1519
  const suppressReconnectRef = useRef(false);
1516
1520
  const terminalErrorRef = useRef(null);
1517
1521
  const [openNonce, setOpenNonce] = useState(0);
1522
+ const initialStatus = !supported || resolvedUrl === null ? "closed" : connect ? "connecting" : "idle";
1518
1523
  const [state, setState] = useState(
1519
- () => createInitialState5(connect ? "connecting" : "idle")
1524
+ () => createInitialState5(initialStatus)
1520
1525
  );
1521
1526
  const stateRef = useRef(state);
1522
1527
  useInsertionEffect(() => {