Skip to content

Commit b9d2639

Browse files
authored
Merge pull request #14 from oslabs-beta/feature/ellie
updated reconnection logic to further correct loss of port connection…
2 parents 3e4453a + 983f275 commit b9d2639

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/extension/background.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,18 @@ chrome.runtime.onConnect.addListener(async (port) => {
348348
portsArr.splice(i, 1);
349349
setTimeout(async () => {
350350
try {
351-
const response = await chrome.runtime.sendMessage({
352-
action: 'attemptReconnect',
353-
});
354-
if (response && response.success) {
351+
const newPort = chrome.runtime.connect({ name: 'reconnected' }); // Attempt to reconnect
352+
if (newPort) {
353+
portsArr.push(newPort); // Add the new port to the array
354+
newPort.onMessage.addListener((msg) => {
355+
console.log('Message received on reconnected port:', msg);
356+
});
355357
console.log('Port successfully reconnected');
358+
} else {
359+
console.warn('Failed to reconnect port');
356360
}
357361
} catch (error) {
358-
console.warn('Port reconnection failed:', error);
362+
console.warn('Port reconnection attempt failed:', error);
359363
}
360364
}, 1000);
361365
break;

src/extension/contentScript.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
// in Web Metrics tab of Reactime app.
33
import { onTTFB, onLCP, onFID, onFCP, onCLS, onINP } from 'web-vitals';
44

5+
function establishConnection() {
6+
const port = chrome.runtime.connect({ name: 'content-script' }); // Reconnect
7+
port.onMessage.addListener((msg) => {
8+
console.log('Received message from background:', msg);
9+
});
10+
11+
port.onDisconnect.addListener(() => {
12+
console.warn('Port disconnected, attempting to reconnect...');
13+
setTimeout(establishConnection, 1000); // Retry after 1 second
14+
});
15+
}
16+
17+
// Initially establish connection
18+
establishConnection();
19+
520
// Reactime application starts off with this file, and will send
621
// first message to background.js for initial tabs object set up.
722
// A "tabs object" holds the information of the current tab,

0 commit comments

Comments
 (0)