@chromahq/react 1.0.20 → 1.0.22
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 +62 -34
- 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
|
}
|
|
@@ -174,8 +174,16 @@ function startHealthMonitor(deps) {
|
|
|
174
174
|
} = deps;
|
|
175
175
|
clearIntervalSafe(pingIntervalRef);
|
|
176
176
|
consecutivePingFailuresRef.current = 0;
|
|
177
|
+
{
|
|
178
|
+
console.log(`[Bridge] Starting health monitor (ping every ${pingInterval}ms)`);
|
|
179
|
+
}
|
|
177
180
|
pingIntervalRef.current = setInterval(async () => {
|
|
178
|
-
if (!bridge.isConnected)
|
|
181
|
+
if (!bridge.isConnected) {
|
|
182
|
+
{
|
|
183
|
+
console.log("[Bridge] Health check skipped - not connected");
|
|
184
|
+
}
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
179
187
|
const alive = await bridge.ping();
|
|
180
188
|
if (!pingIntervalRef.current) return;
|
|
181
189
|
setIsServiceWorkerAlive(alive);
|
|
@@ -184,11 +192,11 @@ function startHealthMonitor(deps) {
|
|
|
184
192
|
return;
|
|
185
193
|
}
|
|
186
194
|
consecutivePingFailuresRef.current++;
|
|
187
|
-
|
|
195
|
+
{
|
|
188
196
|
console.warn(`[Bridge] Ping failed (${consecutivePingFailuresRef.current}x)`);
|
|
189
197
|
}
|
|
190
198
|
if (consecutivePingFailuresRef.current >= CONFIG.CONSECUTIVE_FAILURE_THRESHOLD) {
|
|
191
|
-
|
|
199
|
+
{
|
|
192
200
|
console.warn("[Bridge] Service worker unresponsive, reconnecting...");
|
|
193
201
|
}
|
|
194
202
|
consecutivePingFailuresRef.current = 0;
|
|
@@ -290,7 +298,7 @@ const BridgeProvider = ({
|
|
|
290
298
|
try {
|
|
291
299
|
handler(message.payload);
|
|
292
300
|
} catch (err) {
|
|
293
|
-
|
|
301
|
+
{
|
|
294
302
|
console.warn("[Bridge] Event handler error:", err);
|
|
295
303
|
}
|
|
296
304
|
}
|
|
@@ -307,7 +315,7 @@ const BridgeProvider = ({
|
|
|
307
315
|
retryAfter,
|
|
308
316
|
CONFIG.MAX_RETRY_DELAY
|
|
309
317
|
);
|
|
310
|
-
|
|
318
|
+
{
|
|
311
319
|
console.log(
|
|
312
320
|
`[Bridge] Reconnecting in ${delay}ms (${retryCountRef.current}/${maxRetries})`
|
|
313
321
|
);
|
|
@@ -317,13 +325,13 @@ const BridgeProvider = ({
|
|
|
317
325
|
if (isMountedRef.current) connectFn();
|
|
318
326
|
}, delay);
|
|
319
327
|
} else {
|
|
320
|
-
|
|
328
|
+
{
|
|
321
329
|
console.warn(`[Bridge] Max retries reached. Cooldown: ${maxRetryCooldown}ms`);
|
|
322
330
|
}
|
|
323
331
|
clearTimeoutSafe(maxRetryCooldownRef);
|
|
324
332
|
maxRetryCooldownRef.current = setTimeout(() => {
|
|
325
333
|
if (!isMountedRef.current) return;
|
|
326
|
-
|
|
334
|
+
{
|
|
327
335
|
console.log("[Bridge] Cooldown complete, reconnecting...");
|
|
328
336
|
}
|
|
329
337
|
retryCountRef.current = 0;
|
|
@@ -335,30 +343,43 @@ const BridgeProvider = ({
|
|
|
335
343
|
);
|
|
336
344
|
const scheduleSwRestartReconnect = useCallback(
|
|
337
345
|
(connectFn) => {
|
|
338
|
-
|
|
346
|
+
{
|
|
347
|
+
console.log("[Bridge] scheduleSwRestartReconnect called, isMounted:", isMountedRef.current);
|
|
348
|
+
}
|
|
349
|
+
if (!isMountedRef.current) {
|
|
350
|
+
{
|
|
351
|
+
console.log("[Bridge] Not mounted, skipping SW restart reconnect");
|
|
352
|
+
}
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
339
355
|
swRestartRetryCountRef.current++;
|
|
340
356
|
const delay = calculateBackoffDelay(
|
|
341
357
|
swRestartRetryCountRef.current,
|
|
342
358
|
CONFIG.SW_RESTART_RETRY_DELAY,
|
|
343
359
|
CONFIG.SW_RESTART_MAX_DELAY
|
|
344
360
|
);
|
|
345
|
-
|
|
361
|
+
{
|
|
346
362
|
console.log(
|
|
347
363
|
`[Bridge] Service worker not ready, retrying in ${delay}ms (attempt ${swRestartRetryCountRef.current})`
|
|
348
364
|
);
|
|
349
365
|
}
|
|
350
366
|
updateStatus("reconnecting");
|
|
351
367
|
reconnectTimeoutRef.current = setTimeout(() => {
|
|
368
|
+
{
|
|
369
|
+
console.log("[Bridge] SW restart timeout fired, isMounted:", isMountedRef.current);
|
|
370
|
+
}
|
|
352
371
|
if (isMountedRef.current) connectFn();
|
|
353
372
|
}, delay);
|
|
354
373
|
},
|
|
355
374
|
[updateStatus]
|
|
356
375
|
);
|
|
357
376
|
const connect = useCallback(() => {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
377
|
+
{
|
|
378
|
+
console.log("[Bridge] connect() called, isConnecting:", isConnectingRef.current);
|
|
379
|
+
}
|
|
380
|
+
if (isConnectingRef.current) {
|
|
381
|
+
{
|
|
382
|
+
console.log("[Bridge] Already connecting, skipping...");
|
|
362
383
|
}
|
|
363
384
|
return;
|
|
364
385
|
}
|
|
@@ -370,9 +391,15 @@ const BridgeProvider = ({
|
|
|
370
391
|
return;
|
|
371
392
|
}
|
|
372
393
|
try {
|
|
394
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
395
|
+
console.log("[Bridge] Attempting chrome.runtime.connect...");
|
|
396
|
+
}
|
|
373
397
|
const port = chrome.runtime.connect({ name: CONFIG.PORT_NAME });
|
|
374
398
|
const immediateError = consumeRuntimeError();
|
|
375
399
|
if (immediateError) throw new Error(immediateError);
|
|
400
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
401
|
+
console.log("[Bridge] Port created successfully:", port.name);
|
|
402
|
+
}
|
|
376
403
|
portRef.current = port;
|
|
377
404
|
let errorChecks = 0;
|
|
378
405
|
errorCheckIntervalRef.current = setInterval(() => {
|
|
@@ -397,25 +424,24 @@ const BridgeProvider = ({
|
|
|
397
424
|
port.onMessage.addListener(handleMessage);
|
|
398
425
|
port.onDisconnect.addListener(() => {
|
|
399
426
|
if (BRIDGE_ENABLE_LOGS) {
|
|
400
|
-
console.warn("[Bridge]
|
|
427
|
+
console.warn("[Bridge] *** onDisconnect FIRED ***");
|
|
401
428
|
}
|
|
402
429
|
isConnectingRef.current = false;
|
|
403
430
|
const disconnectError = consumeRuntimeError();
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
} else {
|
|
408
|
-
updateStatus("disconnected");
|
|
431
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
432
|
+
console.warn("[Bridge] Disconnect error:", disconnectError || "(none)");
|
|
433
|
+
console.warn("[Bridge] isMounted:", isMountedRef.current);
|
|
409
434
|
}
|
|
435
|
+
updateStatus("disconnected");
|
|
410
436
|
cleanup();
|
|
411
437
|
if (isMountedRef.current) {
|
|
412
|
-
if (
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
438
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
439
|
+
console.log("[Bridge] Scheduling SW restart reconnect...");
|
|
440
|
+
}
|
|
441
|
+
scheduleSwRestartReconnect(connect);
|
|
442
|
+
} else {
|
|
443
|
+
if (BRIDGE_ENABLE_LOGS) {
|
|
444
|
+
console.log("[Bridge] Not mounted, NOT scheduling reconnect");
|
|
419
445
|
}
|
|
420
446
|
}
|
|
421
447
|
});
|
|
@@ -498,17 +524,19 @@ const BridgeProvider = ({
|
|
|
498
524
|
if (isConnectingRef.current) return;
|
|
499
525
|
const currentStatus = statusRef.current;
|
|
500
526
|
const currentBridge = bridgeRef.current;
|
|
501
|
-
if (currentStatus === "disconnected" || currentStatus === "error") {
|
|
502
|
-
|
|
527
|
+
if (currentStatus === "disconnected" || currentStatus === "error" || currentStatus === "reconnecting") {
|
|
528
|
+
{
|
|
503
529
|
console.log("[Bridge] Tab visible, reconnecting...");
|
|
504
530
|
}
|
|
505
531
|
retryCountRef.current = 0;
|
|
532
|
+
swRestartRetryCountRef.current = 0;
|
|
533
|
+
isConnectingRef.current = false;
|
|
506
534
|
connect();
|
|
507
535
|
} else if (currentStatus === "connected" && currentBridge) {
|
|
508
536
|
currentBridge.ping().then((alive) => {
|
|
509
537
|
if (!isMountedRef.current) return;
|
|
510
538
|
if (!alive) {
|
|
511
|
-
|
|
539
|
+
{
|
|
512
540
|
console.warn("[Bridge] Tab visible but unresponsive, reconnecting...");
|
|
513
541
|
}
|
|
514
542
|
retryCountRef.current = 0;
|