@chromahq/react 1.0.18 → 1.0.20

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 +74 -22
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
2
  import { createContext, useState, useRef, useEffect, useCallback, useMemo, useContext } from 'react';
3
3
 
4
+ const BRIDGE_ENABLE_LOGS = typeof window !== "undefined" && window.__CHROMA_ENABLE_LOGS__ !== void 0 ? Boolean(window.__CHROMA_ENABLE_LOGS__) : true;
4
5
  const CONFIG = {
5
6
  RETRY_AFTER: 1e3,
6
7
  MAX_RETRIES: 10,
@@ -66,11 +67,15 @@ function createBridgeInstance(deps) {
66
67
  if (!pendingRequestsRef.current.has(id)) return;
67
68
  pendingRequestsRef.current.delete(id);
68
69
  consecutiveTimeoutsRef.current++;
69
- console.warn(`[Bridge] Request timed out: ${key} (${timeoutDuration}ms)`);
70
+ if (BRIDGE_ENABLE_LOGS) {
71
+ console.warn(`[Bridge] Request timed out: ${key} (${timeoutDuration}ms)`);
72
+ }
70
73
  if (consecutiveTimeoutsRef.current >= CONFIG.CONSECUTIVE_FAILURE_THRESHOLD) {
71
- console.warn(
72
- `[Bridge] ${consecutiveTimeoutsRef.current} consecutive timeouts, reconnecting...`
73
- );
74
+ if (BRIDGE_ENABLE_LOGS) {
75
+ console.warn(
76
+ `[Bridge] ${consecutiveTimeoutsRef.current} consecutive timeouts, reconnecting...`
77
+ );
78
+ }
74
79
  rejectAllPending("Bridge reconnecting due to timeouts");
75
80
  consecutiveTimeoutsRef.current = 0;
76
81
  onReconnectNeeded();
@@ -111,13 +116,17 @@ function createBridgeInstance(deps) {
111
116
  };
112
117
  const broadcast = (key, payload) => {
113
118
  if (!portRef.current) {
114
- console.warn("[Bridge] Cannot broadcast - disconnected");
119
+ if (BRIDGE_ENABLE_LOGS) {
120
+ console.warn("[Bridge] Cannot broadcast - disconnected");
121
+ }
115
122
  return;
116
123
  }
117
124
  try {
118
125
  portRef.current.postMessage({ type: "broadcast", key, payload });
119
126
  } catch (e) {
120
- console.warn("[Bridge] Broadcast failed:", e);
127
+ if (BRIDGE_ENABLE_LOGS) {
128
+ console.warn("[Bridge] Broadcast failed:", e);
129
+ }
121
130
  }
122
131
  };
123
132
  const on = (key, handler) => {
@@ -175,9 +184,13 @@ function startHealthMonitor(deps) {
175
184
  return;
176
185
  }
177
186
  consecutivePingFailuresRef.current++;
178
- console.warn(`[Bridge] Ping failed (${consecutivePingFailuresRef.current}x)`);
187
+ if (BRIDGE_ENABLE_LOGS) {
188
+ console.warn(`[Bridge] Ping failed (${consecutivePingFailuresRef.current}x)`);
189
+ }
179
190
  if (consecutivePingFailuresRef.current >= CONFIG.CONSECUTIVE_FAILURE_THRESHOLD) {
180
- console.warn("[Bridge] Service worker unresponsive, reconnecting...");
191
+ if (BRIDGE_ENABLE_LOGS) {
192
+ console.warn("[Bridge] Service worker unresponsive, reconnecting...");
193
+ }
181
194
  consecutivePingFailuresRef.current = 0;
182
195
  onReconnectNeeded();
183
196
  }
@@ -277,7 +290,9 @@ const BridgeProvider = ({
277
290
  try {
278
291
  handler(message.payload);
279
292
  } catch (err) {
280
- console.warn("[Bridge] Event handler error:", err);
293
+ if (BRIDGE_ENABLE_LOGS) {
294
+ console.warn("[Bridge] Event handler error:", err);
295
+ }
281
296
  }
282
297
  });
283
298
  }
@@ -292,17 +307,25 @@ const BridgeProvider = ({
292
307
  retryAfter,
293
308
  CONFIG.MAX_RETRY_DELAY
294
309
  );
295
- console.log(`[Bridge] Reconnecting in ${delay}ms (${retryCountRef.current}/${maxRetries})`);
310
+ if (BRIDGE_ENABLE_LOGS) {
311
+ console.log(
312
+ `[Bridge] Reconnecting in ${delay}ms (${retryCountRef.current}/${maxRetries})`
313
+ );
314
+ }
296
315
  updateStatus("reconnecting");
297
316
  reconnectTimeoutRef.current = setTimeout(() => {
298
317
  if (isMountedRef.current) connectFn();
299
318
  }, delay);
300
319
  } else {
301
- console.warn(`[Bridge] Max retries reached. Cooldown: ${maxRetryCooldown}ms`);
320
+ if (BRIDGE_ENABLE_LOGS) {
321
+ console.warn(`[Bridge] Max retries reached. Cooldown: ${maxRetryCooldown}ms`);
322
+ }
302
323
  clearTimeoutSafe(maxRetryCooldownRef);
303
324
  maxRetryCooldownRef.current = setTimeout(() => {
304
325
  if (!isMountedRef.current) return;
305
- console.log("[Bridge] Cooldown complete, reconnecting...");
326
+ if (BRIDGE_ENABLE_LOGS) {
327
+ console.log("[Bridge] Cooldown complete, reconnecting...");
328
+ }
306
329
  retryCountRef.current = 0;
307
330
  connectFn();
308
331
  }, maxRetryCooldown);
@@ -319,9 +342,11 @@ const BridgeProvider = ({
319
342
  CONFIG.SW_RESTART_RETRY_DELAY,
320
343
  CONFIG.SW_RESTART_MAX_DELAY
321
344
  );
322
- console.log(
323
- `[Bridge] Service worker not ready, retrying in ${delay}ms (attempt ${swRestartRetryCountRef.current})`
324
- );
345
+ if (BRIDGE_ENABLE_LOGS) {
346
+ console.log(
347
+ `[Bridge] Service worker not ready, retrying in ${delay}ms (attempt ${swRestartRetryCountRef.current})`
348
+ );
349
+ }
325
350
  updateStatus("reconnecting");
326
351
  reconnectTimeoutRef.current = setTimeout(() => {
327
352
  if (isMountedRef.current) connectFn();
@@ -332,7 +357,9 @@ const BridgeProvider = ({
332
357
  const connect = useCallback(() => {
333
358
  if (isConnectingRef.current) return;
334
359
  if (retryCountRef.current >= maxRetries) {
335
- console.warn("[Bridge] Waiting for cooldown...");
360
+ if (BRIDGE_ENABLE_LOGS) {
361
+ console.warn("[Bridge] Waiting for cooldown...");
362
+ }
336
363
  return;
337
364
  }
338
365
  isConnectingRef.current = true;
@@ -354,7 +381,9 @@ const BridgeProvider = ({
354
381
  if (err) {
355
382
  clearIntervalSafe(errorCheckIntervalRef);
356
383
  if (err.includes("Receiving end does not exist")) {
357
- console.warn("[Bridge] Service worker not ready (may be restarting)...");
384
+ if (BRIDGE_ENABLE_LOGS) {
385
+ console.warn("[Bridge] Service worker not ready (may be restarting)...");
386
+ }
358
387
  cleanup();
359
388
  isConnectingRef.current = false;
360
389
  scheduleSwRestartReconnect(connect);
@@ -367,17 +396,27 @@ const BridgeProvider = ({
367
396
  }, CONFIG.ERROR_CHECK_INTERVAL);
368
397
  port.onMessage.addListener(handleMessage);
369
398
  port.onDisconnect.addListener(() => {
370
- console.warn("[Bridge] Disconnected");
399
+ if (BRIDGE_ENABLE_LOGS) {
400
+ console.warn("[Bridge] Disconnected");
401
+ }
371
402
  isConnectingRef.current = false;
372
403
  const disconnectError = consumeRuntimeError();
373
- if (disconnectError) {
404
+ const isSwRestart = disconnectError?.includes("Receiving end does not exist");
405
+ if (disconnectError && !isSwRestart) {
374
406
  handleError(new Error(disconnectError));
375
407
  } else {
376
408
  updateStatus("disconnected");
377
409
  }
378
410
  cleanup();
379
411
  if (isMountedRef.current) {
380
- scheduleReconnect(connect);
412
+ if (isSwRestart) {
413
+ if (BRIDGE_ENABLE_LOGS) {
414
+ console.log("[Bridge] Service worker stopped, will retry until available...");
415
+ }
416
+ scheduleSwRestartReconnect(connect);
417
+ } else {
418
+ scheduleReconnect(connect);
419
+ }
381
420
  }
382
421
  });
383
422
  const triggerReconnect = () => {
@@ -412,6 +451,15 @@ const BridgeProvider = ({
412
451
  swRestartRetryCountRef.current = 0;
413
452
  consecutiveTimeoutsRef.current = 0;
414
453
  isConnectingRef.current = false;
454
+ eventListenersRef.current.get("bridge:connected")?.forEach((handler) => {
455
+ try {
456
+ handler({ timestamp: Date.now() });
457
+ } catch (err) {
458
+ if (BRIDGE_ENABLE_LOGS) {
459
+ console.warn("[Bridge] bridge:connected handler error:", err);
460
+ }
461
+ }
462
+ });
415
463
  startHealthMonitor({
416
464
  bridge: bridgeInstance,
417
465
  pingIntervalRef,
@@ -451,14 +499,18 @@ const BridgeProvider = ({
451
499
  const currentStatus = statusRef.current;
452
500
  const currentBridge = bridgeRef.current;
453
501
  if (currentStatus === "disconnected" || currentStatus === "error") {
454
- console.log("[Bridge] Tab visible, reconnecting...");
502
+ if (BRIDGE_ENABLE_LOGS) {
503
+ console.log("[Bridge] Tab visible, reconnecting...");
504
+ }
455
505
  retryCountRef.current = 0;
456
506
  connect();
457
507
  } else if (currentStatus === "connected" && currentBridge) {
458
508
  currentBridge.ping().then((alive) => {
459
509
  if (!isMountedRef.current) return;
460
510
  if (!alive) {
461
- console.warn("[Bridge] Tab visible but unresponsive, reconnecting...");
511
+ if (BRIDGE_ENABLE_LOGS) {
512
+ console.warn("[Bridge] Tab visible but unresponsive, reconnecting...");
513
+ }
462
514
  retryCountRef.current = 0;
463
515
  isConnectingRef.current = false;
464
516
  cleanup();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromahq/react",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "React bindings for the Chroma Chrome extension framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",