@chromahq/react 1.0.35 → 1.0.36
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.ts +6 -11
- package/dist/index.js +18 -10
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ interface Bridge {
|
|
|
8
8
|
off: (key: string, handler: (payload: unknown) => void) => void;
|
|
9
9
|
isConnected: boolean;
|
|
10
10
|
ping: () => Promise<boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* Pause health checks for the specified duration.
|
|
13
|
+
* Use this before calling a message that triggers heavy/blocking operations in the SW.
|
|
14
|
+
* @param durationMs - How long to pause health checks in milliseconds
|
|
15
|
+
*/
|
|
16
|
+
pauseHealthChecks: (durationMs: number) => void;
|
|
11
17
|
}
|
|
12
18
|
interface BridgeContextValue {
|
|
13
19
|
bridge: Bridge | null;
|
|
@@ -32,17 +38,6 @@ interface BridgeProviderProps {
|
|
|
32
38
|
onConnectionChange?: (status: ConnectionStatus) => void;
|
|
33
39
|
/** Callback when an error occurs */
|
|
34
40
|
onError?: (error: Error) => void;
|
|
35
|
-
/**
|
|
36
|
-
* Optional function to check if health checks should be paused.
|
|
37
|
-
* Return a timestamp (ms) until which health checks are paused, or null/0 if not paused.
|
|
38
|
-
* This allows service workers to pause health monitoring during heavy operations.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```tsx
|
|
42
|
-
* <BridgeProvider isHealthPaused={() => store.getState().healthPausedUntil}>
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
isHealthPausedUntil?: () => number | null | undefined;
|
|
46
41
|
}
|
|
47
42
|
declare const BridgeProvider: FC<BridgeProviderProps>;
|
|
48
43
|
|
package/dist/index.js
CHANGED
|
@@ -50,6 +50,7 @@ function createBridgeInstance(deps) {
|
|
|
50
50
|
isConnectedRef,
|
|
51
51
|
consecutiveTimeoutsRef,
|
|
52
52
|
reconnectionGracePeriodRef,
|
|
53
|
+
healthPausedUntilRef,
|
|
53
54
|
defaultTimeout,
|
|
54
55
|
onReconnectNeeded
|
|
55
56
|
} = deps;
|
|
@@ -167,6 +168,13 @@ function createBridgeInstance(deps) {
|
|
|
167
168
|
} catch {
|
|
168
169
|
return false;
|
|
169
170
|
}
|
|
171
|
+
},
|
|
172
|
+
pauseHealthChecks: (durationMs) => {
|
|
173
|
+
const pauseUntil = Date.now() + durationMs;
|
|
174
|
+
healthPausedUntilRef.current = pauseUntil;
|
|
175
|
+
{
|
|
176
|
+
console.log(`[Bridge] Health checks paused for ${Math.round(durationMs / 1e3)}s`);
|
|
177
|
+
}
|
|
170
178
|
}
|
|
171
179
|
};
|
|
172
180
|
return bridge;
|
|
@@ -176,11 +184,11 @@ function startHealthMonitor(deps) {
|
|
|
176
184
|
bridge,
|
|
177
185
|
pingIntervalRef,
|
|
178
186
|
consecutivePingFailuresRef,
|
|
187
|
+
healthPausedUntilRef,
|
|
179
188
|
pingInterval,
|
|
180
189
|
setIsServiceWorkerAlive,
|
|
181
190
|
onReconnectNeeded,
|
|
182
|
-
rejectAllPendingRequests
|
|
183
|
-
isHealthPausedUntil
|
|
191
|
+
rejectAllPendingRequests
|
|
184
192
|
} = deps;
|
|
185
193
|
clearIntervalSafe(pingIntervalRef);
|
|
186
194
|
consecutivePingFailuresRef.current = 0;
|
|
@@ -194,7 +202,7 @@ function startHealthMonitor(deps) {
|
|
|
194
202
|
}
|
|
195
203
|
return;
|
|
196
204
|
}
|
|
197
|
-
const pausedUntil =
|
|
205
|
+
const pausedUntil = healthPausedUntilRef.current;
|
|
198
206
|
if (pausedUntil && Date.now() < pausedUntil) {
|
|
199
207
|
{
|
|
200
208
|
const remainingMs = pausedUntil - Date.now();
|
|
@@ -207,7 +215,7 @@ function startHealthMonitor(deps) {
|
|
|
207
215
|
}
|
|
208
216
|
const alive = await bridge.ping();
|
|
209
217
|
if (!pingIntervalRef.current) return;
|
|
210
|
-
const pausedUntilAfterPing =
|
|
218
|
+
const pausedUntilAfterPing = healthPausedUntilRef.current;
|
|
211
219
|
if (pausedUntilAfterPing && Date.now() < pausedUntilAfterPing) {
|
|
212
220
|
consecutivePingFailuresRef.current = 0;
|
|
213
221
|
return;
|
|
@@ -241,8 +249,7 @@ const BridgeProvider = ({
|
|
|
241
249
|
maxRetryCooldown = CONFIG.MAX_RETRY_COOLDOWN,
|
|
242
250
|
defaultTimeout = CONFIG.DEFAULT_TIMEOUT,
|
|
243
251
|
onConnectionChange,
|
|
244
|
-
onError
|
|
245
|
-
isHealthPausedUntil
|
|
252
|
+
onError
|
|
246
253
|
}) => {
|
|
247
254
|
const [bridge, setBridge] = useState(null);
|
|
248
255
|
const [status, setStatus] = useState("connecting");
|
|
@@ -260,6 +267,7 @@ const BridgeProvider = ({
|
|
|
260
267
|
const errorCheckIntervalRef = useRef(null);
|
|
261
268
|
const consecutivePingFailuresRef = useRef(0);
|
|
262
269
|
const consecutiveTimeoutsRef = useRef(0);
|
|
270
|
+
const healthPausedUntilRef = useRef(0);
|
|
263
271
|
const reconnectionGracePeriodRef = useRef(false);
|
|
264
272
|
const pendingRequestsRef = useRef(/* @__PURE__ */ new Map());
|
|
265
273
|
const eventListenersRef = useRef(/* @__PURE__ */ new Map());
|
|
@@ -514,6 +522,7 @@ const BridgeProvider = ({
|
|
|
514
522
|
isConnectedRef,
|
|
515
523
|
consecutiveTimeoutsRef,
|
|
516
524
|
reconnectionGracePeriodRef,
|
|
525
|
+
healthPausedUntilRef,
|
|
517
526
|
defaultTimeout,
|
|
518
527
|
onReconnectNeeded: triggerReconnect
|
|
519
528
|
});
|
|
@@ -554,11 +563,11 @@ const BridgeProvider = ({
|
|
|
554
563
|
bridge: bridgeInstance,
|
|
555
564
|
pingIntervalRef,
|
|
556
565
|
consecutivePingFailuresRef,
|
|
566
|
+
healthPausedUntilRef,
|
|
557
567
|
pingInterval,
|
|
558
568
|
setIsServiceWorkerAlive,
|
|
559
569
|
onReconnectNeeded: triggerReconnect,
|
|
560
|
-
rejectAllPendingRequests
|
|
561
|
-
isHealthPausedUntil
|
|
570
|
+
rejectAllPendingRequests
|
|
562
571
|
});
|
|
563
572
|
} catch (e) {
|
|
564
573
|
isConnectingRef.current = false;
|
|
@@ -574,8 +583,7 @@ const BridgeProvider = ({
|
|
|
574
583
|
scheduleSwRestartReconnect,
|
|
575
584
|
defaultTimeout,
|
|
576
585
|
updateStatus,
|
|
577
|
-
pingInterval
|
|
578
|
-
isHealthPausedUntil
|
|
586
|
+
pingInterval
|
|
579
587
|
]);
|
|
580
588
|
const reconnect = useCallback(() => {
|
|
581
589
|
retryCountRef.current = 0;
|