@chromahq/react 1.0.20 → 1.0.21
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 +23 -33
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +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 =
|
|
4
|
+
const BRIDGE_ENABLE_LOGS = true;
|
|
5
5
|
const CONFIG = {
|
|
6
6
|
RETRY_AFTER: 1e3,
|
|
7
7
|
MAX_RETRIES: 10,
|
|
@@ -67,11 +67,11 @@ function createBridgeInstance(deps) {
|
|
|
67
67
|
if (!pendingRequestsRef.current.has(id)) return;
|
|
68
68
|
pendingRequestsRef.current.delete(id);
|
|
69
69
|
consecutiveTimeoutsRef.current++;
|
|
70
|
-
|
|
70
|
+
{
|
|
71
71
|
console.warn(`[Bridge] Request timed out: ${key} (${timeoutDuration}ms)`);
|
|
72
72
|
}
|
|
73
73
|
if (consecutiveTimeoutsRef.current >= CONFIG.CONSECUTIVE_FAILURE_THRESHOLD) {
|
|
74
|
-
|
|
74
|
+
{
|
|
75
75
|
console.warn(
|
|
76
76
|
`[Bridge] ${consecutiveTimeoutsRef.current} consecutive timeouts, reconnecting...`
|
|
77
77
|
);
|
|
@@ -116,7 +116,7 @@ function createBridgeInstance(deps) {
|
|
|
116
116
|
};
|
|
117
117
|
const broadcast = (key, payload) => {
|
|
118
118
|
if (!portRef.current) {
|
|
119
|
-
|
|
119
|
+
{
|
|
120
120
|
console.warn("[Bridge] Cannot broadcast - disconnected");
|
|
121
121
|
}
|
|
122
122
|
return;
|
|
@@ -124,7 +124,7 @@ function createBridgeInstance(deps) {
|
|
|
124
124
|
try {
|
|
125
125
|
portRef.current.postMessage({ type: "broadcast", key, payload });
|
|
126
126
|
} catch (e) {
|
|
127
|
-
|
|
127
|
+
{
|
|
128
128
|
console.warn("[Bridge] Broadcast failed:", e);
|
|
129
129
|
}
|
|
130
130
|
}
|
|
@@ -184,11 +184,11 @@ function startHealthMonitor(deps) {
|
|
|
184
184
|
return;
|
|
185
185
|
}
|
|
186
186
|
consecutivePingFailuresRef.current++;
|
|
187
|
-
|
|
187
|
+
{
|
|
188
188
|
console.warn(`[Bridge] Ping failed (${consecutivePingFailuresRef.current}x)`);
|
|
189
189
|
}
|
|
190
190
|
if (consecutivePingFailuresRef.current >= CONFIG.CONSECUTIVE_FAILURE_THRESHOLD) {
|
|
191
|
-
|
|
191
|
+
{
|
|
192
192
|
console.warn("[Bridge] Service worker unresponsive, reconnecting...");
|
|
193
193
|
}
|
|
194
194
|
consecutivePingFailuresRef.current = 0;
|
|
@@ -290,7 +290,7 @@ const BridgeProvider = ({
|
|
|
290
290
|
try {
|
|
291
291
|
handler(message.payload);
|
|
292
292
|
} catch (err) {
|
|
293
|
-
|
|
293
|
+
{
|
|
294
294
|
console.warn("[Bridge] Event handler error:", err);
|
|
295
295
|
}
|
|
296
296
|
}
|
|
@@ -307,7 +307,7 @@ const BridgeProvider = ({
|
|
|
307
307
|
retryAfter,
|
|
308
308
|
CONFIG.MAX_RETRY_DELAY
|
|
309
309
|
);
|
|
310
|
-
|
|
310
|
+
{
|
|
311
311
|
console.log(
|
|
312
312
|
`[Bridge] Reconnecting in ${delay}ms (${retryCountRef.current}/${maxRetries})`
|
|
313
313
|
);
|
|
@@ -317,13 +317,13 @@ const BridgeProvider = ({
|
|
|
317
317
|
if (isMountedRef.current) connectFn();
|
|
318
318
|
}, delay);
|
|
319
319
|
} else {
|
|
320
|
-
|
|
320
|
+
{
|
|
321
321
|
console.warn(`[Bridge] Max retries reached. Cooldown: ${maxRetryCooldown}ms`);
|
|
322
322
|
}
|
|
323
323
|
clearTimeoutSafe(maxRetryCooldownRef);
|
|
324
324
|
maxRetryCooldownRef.current = setTimeout(() => {
|
|
325
325
|
if (!isMountedRef.current) return;
|
|
326
|
-
|
|
326
|
+
{
|
|
327
327
|
console.log("[Bridge] Cooldown complete, reconnecting...");
|
|
328
328
|
}
|
|
329
329
|
retryCountRef.current = 0;
|
|
@@ -342,7 +342,7 @@ const BridgeProvider = ({
|
|
|
342
342
|
CONFIG.SW_RESTART_RETRY_DELAY,
|
|
343
343
|
CONFIG.SW_RESTART_MAX_DELAY
|
|
344
344
|
);
|
|
345
|
-
|
|
345
|
+
{
|
|
346
346
|
console.log(
|
|
347
347
|
`[Bridge] Service worker not ready, retrying in ${delay}ms (attempt ${swRestartRetryCountRef.current})`
|
|
348
348
|
);
|
|
@@ -356,12 +356,6 @@ const BridgeProvider = ({
|
|
|
356
356
|
);
|
|
357
357
|
const connect = useCallback(() => {
|
|
358
358
|
if (isConnectingRef.current) return;
|
|
359
|
-
if (retryCountRef.current >= maxRetries) {
|
|
360
|
-
if (BRIDGE_ENABLE_LOGS) {
|
|
361
|
-
console.warn("[Bridge] Waiting for cooldown...");
|
|
362
|
-
}
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
359
|
isConnectingRef.current = true;
|
|
366
360
|
cleanup();
|
|
367
361
|
if (!chrome?.runtime?.connect) {
|
|
@@ -401,22 +395,16 @@ const BridgeProvider = ({
|
|
|
401
395
|
}
|
|
402
396
|
isConnectingRef.current = false;
|
|
403
397
|
const disconnectError = consumeRuntimeError();
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
handleError(new Error(disconnectError));
|
|
407
|
-
} else {
|
|
408
|
-
updateStatus("disconnected");
|
|
398
|
+
if (disconnectError && BRIDGE_ENABLE_LOGS) {
|
|
399
|
+
console.warn("[Bridge] Disconnect error:", disconnectError);
|
|
409
400
|
}
|
|
401
|
+
updateStatus("disconnected");
|
|
410
402
|
cleanup();
|
|
411
403
|
if (isMountedRef.current) {
|
|
412
|
-
if (
|
|
413
|
-
|
|
414
|
-
console.log("[Bridge] Service worker stopped, will retry until available...");
|
|
415
|
-
}
|
|
416
|
-
scheduleSwRestartReconnect(connect);
|
|
417
|
-
} else {
|
|
418
|
-
scheduleReconnect(connect);
|
|
404
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
405
|
+
console.log("[Bridge] Will retry until service worker is available...");
|
|
419
406
|
}
|
|
407
|
+
scheduleSwRestartReconnect(connect);
|
|
420
408
|
}
|
|
421
409
|
});
|
|
422
410
|
const triggerReconnect = () => {
|
|
@@ -498,17 +486,19 @@ const BridgeProvider = ({
|
|
|
498
486
|
if (isConnectingRef.current) return;
|
|
499
487
|
const currentStatus = statusRef.current;
|
|
500
488
|
const currentBridge = bridgeRef.current;
|
|
501
|
-
if (currentStatus === "disconnected" || currentStatus === "error") {
|
|
502
|
-
|
|
489
|
+
if (currentStatus === "disconnected" || currentStatus === "error" || currentStatus === "reconnecting") {
|
|
490
|
+
{
|
|
503
491
|
console.log("[Bridge] Tab visible, reconnecting...");
|
|
504
492
|
}
|
|
505
493
|
retryCountRef.current = 0;
|
|
494
|
+
swRestartRetryCountRef.current = 0;
|
|
495
|
+
isConnectingRef.current = false;
|
|
506
496
|
connect();
|
|
507
497
|
} else if (currentStatus === "connected" && currentBridge) {
|
|
508
498
|
currentBridge.ping().then((alive) => {
|
|
509
499
|
if (!isMountedRef.current) return;
|
|
510
500
|
if (!alive) {
|
|
511
|
-
|
|
501
|
+
{
|
|
512
502
|
console.warn("[Bridge] Tab visible but unresponsive, reconnecting...");
|
|
513
503
|
}
|
|
514
504
|
retryCountRef.current = 0;
|