@chromahq/react 1.0.17 → 1.0.19

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.
Files changed (2) hide show
  1. package/dist/index.js +35 -3
  2. 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] Background not ready, retrying...");
357
+ console.warn("[Bridge] Service worker not ready (may be restarting)...");
335
358
  cleanup();
336
359
  isConnectingRef.current = false;
337
- scheduleReconnect(connect);
360
+ scheduleSwRestartReconnect(connect);
338
361
  }
339
362
  return;
340
363
  }
@@ -386,8 +409,16 @@ 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;
415
+ eventListenersRef.current.get("bridge:connected")?.forEach((handler) => {
416
+ try {
417
+ handler({ timestamp: Date.now() });
418
+ } catch (err) {
419
+ console.warn("[Bridge] bridge:connected handler error:", err);
420
+ }
421
+ });
391
422
  startHealthMonitor({
392
423
  bridge: bridgeInstance,
393
424
  pingIntervalRef,
@@ -407,6 +438,7 @@ const BridgeProvider = ({
407
438
  handleError,
408
439
  handleMessage,
409
440
  scheduleReconnect,
441
+ scheduleSwRestartReconnect,
410
442
  defaultTimeout,
411
443
  updateStatus,
412
444
  pingInterval
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromahq/react",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "React bindings for the Chroma Chrome extension framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",