Skip to content

Commit 16365c1

Browse files
committed
updated reconnection logic
1 parent 78ef183 commit 16365c1

File tree

2 files changed

+98
-20
lines changed

2 files changed

+98
-20
lines changed

src/extension/build/manifest.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
"matches": ["<all_urls>"]
2525
}
2626
],
27-
"permissions": [
28-
"contextMenus",
29-
"tabs",
30-
"activeTab",
31-
"scripting",
32-
"system.display",
33-
"debugger",
34-
"alarms"
35-
],
27+
"permissions": ["contextMenus", "tabs", "activeTab", "scripting", "debugger", "alarms"],
3628
"host_permissions": ["<all_urls>"]
3729
}

src/extension/contentScript.ts

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,104 @@
11
// Web vital metrics calculated by 'web-vitals' npm package to be displayed
22
// in Web Metrics tab of Reactime app.
3+
import { current } from '@reduxjs/toolkit';
34
import { onTTFB, onLCP, onFID, onFCP, onCLS, onINP } from 'web-vitals';
45

5-
function establishConnection() {
6-
const port = chrome.runtime.connect({ name: 'keepAlivePort' }); // Reconnect if we lose connection to the port at any time
7-
port.onMessage.addListener((msg) => {
8-
console.log('Received message from background:', msg);
9-
});
6+
const MAX_RECONNECT_ATTEMPTS = 5;
7+
const INITIAL_RECONNECT_DELAY = 1000;
8+
const MAX_RECONNECT_DELAY = 16000;
109

11-
port.onDisconnect.addListener(() => {
12-
console.warn('Port disconnected, attempting to reconnect...');
13-
setTimeout(establishConnection, 1000); // Retry after 1 second
14-
});
10+
let currentPort = null;
11+
let isAttemptingReconnect = false;
12+
13+
function establishConnection(attemptNumber = 1) {
14+
console.log(`Establishing connection, attempt ${attemptNumber}`);
15+
16+
try {
17+
currentPort = chrome.runtime.connect({ name: 'keepAlivePort' });
18+
19+
console.log('Port created, setting up listeners');
20+
21+
currentPort.onMessage.addListener((msg) => {
22+
console.log('Port received message:', msg);
23+
});
24+
25+
currentPort.onDisconnect.addListener(() => {
26+
const error = chrome.runtime.lastError;
27+
console.log('Port disconnect triggered', error);
28+
29+
// Clear current port
30+
currentPort = null;
31+
32+
// Prevent multiple simultaneous reconnection attempts
33+
if (isAttemptingReconnect) {
34+
console.log('Already attempting to reconnect, skipping');
35+
return;
36+
}
37+
38+
isAttemptingReconnect = true;
39+
40+
// Calculate delay with exponential backoff
41+
const delay = Math.min(
42+
INITIAL_RECONNECT_DELAY * Math.pow(2, attemptNumber - 1),
43+
MAX_RECONNECT_DELAY,
44+
);
45+
46+
if (attemptNumber <= MAX_RECONNECT_ATTEMPTS) {
47+
console.log(
48+
`Will attempt reconnection ${attemptNumber}/${MAX_RECONNECT_ATTEMPTS} in ${delay}ms`,
49+
);
50+
51+
window.postMessage(
52+
{
53+
action: 'portDisconnect',
54+
payload: {
55+
attemptNumber,
56+
maxAttempts: MAX_RECONNECT_ATTEMPTS,
57+
nextRetryDelay: delay,
58+
},
59+
},
60+
'*',
61+
);
62+
63+
setTimeout(() => {
64+
isAttemptingReconnect = false;
65+
establishConnection(attemptNumber + 1);
66+
}, delay);
67+
} else {
68+
console.log('Max reconnection attempts reached');
69+
isAttemptingReconnect = false;
70+
71+
window.postMessage(
72+
{
73+
action: 'portDisconnect',
74+
payload: {
75+
autoReconnectFailed: true,
76+
message: 'Automatic reconnection failed. Please use the reconnect button.',
77+
},
78+
},
79+
'*',
80+
);
81+
}
82+
});
83+
84+
// Send initial test message
85+
currentPort.postMessage({ type: 'connectionTest' });
86+
console.log('Test message sent');
87+
} catch (error) {
88+
console.error('Error establishing connection:', error);
89+
isAttemptingReconnect = false;
90+
91+
// If immediate connection fails, try again
92+
if (attemptNumber <= MAX_RECONNECT_ATTEMPTS) {
93+
const delay = INITIAL_RECONNECT_DELAY;
94+
console.log(`Connection failed immediately, retrying in ${delay}ms`);
95+
setTimeout(() => establishConnection(attemptNumber + 1), delay);
96+
}
97+
}
1598
}
1699

17-
// Initially establish connection
100+
// Initial connection
101+
console.log('Starting initial connection');
18102
establishConnection();
19103

20104
// Reactime application starts off with this file, and will send
@@ -73,7 +157,8 @@ chrome.runtime.onMessage.addListener((request) => {
73157
// '*' == target window origin required for event to be dispatched, '*' = no preference
74158
window.postMessage(request, '*');
75159
}
76-
if (action === 'portDisconnect') {
160+
if (action === 'portDisconnect' && !currentPort && !isAttemptingReconnect) {
161+
console.log('Received disconnect message, initiating reconnection');
77162
// When we receive a port disconnection message, relay it to the window
78163
window.postMessage(
79164
{
@@ -89,6 +174,7 @@ chrome.runtime.onMessage.addListener((request) => {
89174
if (action === 'reinitialize') {
90175
window.postMessage(request, '*');
91176
}
177+
return true;
92178
}
93179
});
94180

0 commit comments

Comments
 (0)