@chromahq/react 1.0.19 → 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 +65 -30
- 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 = 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
|
-
|
|
70
|
+
{
|
|
71
|
+
console.warn(`[Bridge] Request timed out: ${key} (${timeoutDuration}ms)`);
|
|
72
|
+
}
|
|
70
73
|
if (consecutiveTimeoutsRef.current >= CONFIG.CONSECUTIVE_FAILURE_THRESHOLD) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
{
|
|
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
|
-
|
|
119
|
+
{
|
|
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
|
-
|
|
127
|
+
{
|
|
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
|
-
|
|
187
|
+
{
|
|
188
|
+
console.warn(`[Bridge] Ping failed (${consecutivePingFailuresRef.current}x)`);
|
|
189
|
+
}
|
|
179
190
|
if (consecutivePingFailuresRef.current >= CONFIG.CONSECUTIVE_FAILURE_THRESHOLD) {
|
|
180
|
-
|
|
191
|
+
{
|
|
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
|
-
|
|
293
|
+
{
|
|
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
|
-
|
|
310
|
+
{
|
|
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
|
-
|
|
320
|
+
{
|
|
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
|
-
|
|
326
|
+
{
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
345
|
+
{
|
|
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();
|
|
@@ -331,10 +356,6 @@ const BridgeProvider = ({
|
|
|
331
356
|
);
|
|
332
357
|
const connect = useCallback(() => {
|
|
333
358
|
if (isConnectingRef.current) return;
|
|
334
|
-
if (retryCountRef.current >= maxRetries) {
|
|
335
|
-
console.warn("[Bridge] Waiting for cooldown...");
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
359
|
isConnectingRef.current = true;
|
|
339
360
|
cleanup();
|
|
340
361
|
if (!chrome?.runtime?.connect) {
|
|
@@ -354,7 +375,9 @@ const BridgeProvider = ({
|
|
|
354
375
|
if (err) {
|
|
355
376
|
clearIntervalSafe(errorCheckIntervalRef);
|
|
356
377
|
if (err.includes("Receiving end does not exist")) {
|
|
357
|
-
|
|
378
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
379
|
+
console.warn("[Bridge] Service worker not ready (may be restarting)...");
|
|
380
|
+
}
|
|
358
381
|
cleanup();
|
|
359
382
|
isConnectingRef.current = false;
|
|
360
383
|
scheduleSwRestartReconnect(connect);
|
|
@@ -367,17 +390,21 @@ const BridgeProvider = ({
|
|
|
367
390
|
}, CONFIG.ERROR_CHECK_INTERVAL);
|
|
368
391
|
port.onMessage.addListener(handleMessage);
|
|
369
392
|
port.onDisconnect.addListener(() => {
|
|
370
|
-
|
|
393
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
394
|
+
console.warn("[Bridge] Disconnected");
|
|
395
|
+
}
|
|
371
396
|
isConnectingRef.current = false;
|
|
372
397
|
const disconnectError = consumeRuntimeError();
|
|
373
|
-
if (disconnectError) {
|
|
374
|
-
|
|
375
|
-
} else {
|
|
376
|
-
updateStatus("disconnected");
|
|
398
|
+
if (disconnectError && BRIDGE_ENABLE_LOGS) {
|
|
399
|
+
console.warn("[Bridge] Disconnect error:", disconnectError);
|
|
377
400
|
}
|
|
401
|
+
updateStatus("disconnected");
|
|
378
402
|
cleanup();
|
|
379
403
|
if (isMountedRef.current) {
|
|
380
|
-
|
|
404
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
405
|
+
console.log("[Bridge] Will retry until service worker is available...");
|
|
406
|
+
}
|
|
407
|
+
scheduleSwRestartReconnect(connect);
|
|
381
408
|
}
|
|
382
409
|
});
|
|
383
410
|
const triggerReconnect = () => {
|
|
@@ -416,7 +443,9 @@ const BridgeProvider = ({
|
|
|
416
443
|
try {
|
|
417
444
|
handler({ timestamp: Date.now() });
|
|
418
445
|
} catch (err) {
|
|
419
|
-
|
|
446
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
447
|
+
console.warn("[Bridge] bridge:connected handler error:", err);
|
|
448
|
+
}
|
|
420
449
|
}
|
|
421
450
|
});
|
|
422
451
|
startHealthMonitor({
|
|
@@ -457,15 +486,21 @@ const BridgeProvider = ({
|
|
|
457
486
|
if (isConnectingRef.current) return;
|
|
458
487
|
const currentStatus = statusRef.current;
|
|
459
488
|
const currentBridge = bridgeRef.current;
|
|
460
|
-
if (currentStatus === "disconnected" || currentStatus === "error") {
|
|
461
|
-
|
|
489
|
+
if (currentStatus === "disconnected" || currentStatus === "error" || currentStatus === "reconnecting") {
|
|
490
|
+
{
|
|
491
|
+
console.log("[Bridge] Tab visible, reconnecting...");
|
|
492
|
+
}
|
|
462
493
|
retryCountRef.current = 0;
|
|
494
|
+
swRestartRetryCountRef.current = 0;
|
|
495
|
+
isConnectingRef.current = false;
|
|
463
496
|
connect();
|
|
464
497
|
} else if (currentStatus === "connected" && currentBridge) {
|
|
465
498
|
currentBridge.ping().then((alive) => {
|
|
466
499
|
if (!isMountedRef.current) return;
|
|
467
500
|
if (!alive) {
|
|
468
|
-
|
|
501
|
+
{
|
|
502
|
+
console.warn("[Bridge] Tab visible but unresponsive, reconnecting...");
|
|
503
|
+
}
|
|
469
504
|
retryCountRef.current = 0;
|
|
470
505
|
isConnectingRef.current = false;
|
|
471
506
|
cleanup();
|