@chromahq/react 1.0.17 → 1.0.18
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 +28 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,7 +13,10 @@ const CONFIG = {
|
|
|
13
13
|
MAX_ERROR_CHECKS: 10,
|
|
14
14
|
CONSECUTIVE_FAILURE_THRESHOLD: 2,
|
|
15
15
|
RECONNECT_DELAY: 100,
|
|
16
|
-
PORT_NAME: "chroma-bridge"
|
|
16
|
+
PORT_NAME: "chroma-bridge",
|
|
17
|
+
// Service worker restart retry settings (indefinite retries)
|
|
18
|
+
SW_RESTART_RETRY_DELAY: 500,
|
|
19
|
+
SW_RESTART_MAX_DELAY: 5e3
|
|
17
20
|
};
|
|
18
21
|
const calculateBackoffDelay = (attempt, baseDelay, maxDelay) => Math.min(baseDelay * Math.pow(2, attempt - 1), maxDelay);
|
|
19
22
|
const clearTimeoutSafe = (ref) => {
|
|
@@ -201,6 +204,7 @@ const BridgeProvider = ({
|
|
|
201
204
|
const reconnectTimeoutRef = useRef(null);
|
|
202
205
|
const maxRetryCooldownRef = useRef(null);
|
|
203
206
|
const triggerReconnectTimeoutRef = useRef(null);
|
|
207
|
+
const swRestartRetryCountRef = useRef(0);
|
|
204
208
|
const pingIntervalRef = useRef(null);
|
|
205
209
|
const errorCheckIntervalRef = useRef(null);
|
|
206
210
|
const consecutivePingFailuresRef = useRef(0);
|
|
@@ -306,6 +310,25 @@ const BridgeProvider = ({
|
|
|
306
310
|
},
|
|
307
311
|
[maxRetries, retryAfter, maxRetryCooldown, updateStatus]
|
|
308
312
|
);
|
|
313
|
+
const scheduleSwRestartReconnect = useCallback(
|
|
314
|
+
(connectFn) => {
|
|
315
|
+
if (!isMountedRef.current) return;
|
|
316
|
+
swRestartRetryCountRef.current++;
|
|
317
|
+
const delay = calculateBackoffDelay(
|
|
318
|
+
swRestartRetryCountRef.current,
|
|
319
|
+
CONFIG.SW_RESTART_RETRY_DELAY,
|
|
320
|
+
CONFIG.SW_RESTART_MAX_DELAY
|
|
321
|
+
);
|
|
322
|
+
console.log(
|
|
323
|
+
`[Bridge] Service worker not ready, retrying in ${delay}ms (attempt ${swRestartRetryCountRef.current})`
|
|
324
|
+
);
|
|
325
|
+
updateStatus("reconnecting");
|
|
326
|
+
reconnectTimeoutRef.current = setTimeout(() => {
|
|
327
|
+
if (isMountedRef.current) connectFn();
|
|
328
|
+
}, delay);
|
|
329
|
+
},
|
|
330
|
+
[updateStatus]
|
|
331
|
+
);
|
|
309
332
|
const connect = useCallback(() => {
|
|
310
333
|
if (isConnectingRef.current) return;
|
|
311
334
|
if (retryCountRef.current >= maxRetries) {
|
|
@@ -331,10 +354,10 @@ const BridgeProvider = ({
|
|
|
331
354
|
if (err) {
|
|
332
355
|
clearIntervalSafe(errorCheckIntervalRef);
|
|
333
356
|
if (err.includes("Receiving end does not exist")) {
|
|
334
|
-
console.warn("[Bridge]
|
|
357
|
+
console.warn("[Bridge] Service worker not ready (may be restarting)...");
|
|
335
358
|
cleanup();
|
|
336
359
|
isConnectingRef.current = false;
|
|
337
|
-
|
|
360
|
+
scheduleSwRestartReconnect(connect);
|
|
338
361
|
}
|
|
339
362
|
return;
|
|
340
363
|
}
|
|
@@ -386,6 +409,7 @@ const BridgeProvider = ({
|
|
|
386
409
|
setIsServiceWorkerAlive(true);
|
|
387
410
|
setError(null);
|
|
388
411
|
retryCountRef.current = 0;
|
|
412
|
+
swRestartRetryCountRef.current = 0;
|
|
389
413
|
consecutiveTimeoutsRef.current = 0;
|
|
390
414
|
isConnectingRef.current = false;
|
|
391
415
|
startHealthMonitor({
|
|
@@ -407,6 +431,7 @@ const BridgeProvider = ({
|
|
|
407
431
|
handleError,
|
|
408
432
|
handleMessage,
|
|
409
433
|
scheduleReconnect,
|
|
434
|
+
scheduleSwRestartReconnect,
|
|
410
435
|
defaultTimeout,
|
|
411
436
|
updateStatus,
|
|
412
437
|
pingInterval
|