1
1
// Web vital metrics calculated by 'web-vitals' npm package to be displayed
2
2
// in Web Metrics tab of Reactime app.
3
+ import { current } from '@reduxjs/toolkit' ;
3
4
import { onTTFB , onLCP , onFID , onFCP , onCLS , onINP } from 'web-vitals' ;
4
5
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 ;
10
9
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
+ }
15
98
}
16
99
17
- // Initially establish connection
100
+ // Initial connection
101
+ console . log ( 'Starting initial connection' ) ;
18
102
establishConnection ( ) ;
19
103
20
104
// Reactime application starts off with this file, and will send
@@ -73,7 +157,8 @@ chrome.runtime.onMessage.addListener((request) => {
73
157
// '*' == target window origin required for event to be dispatched, '*' = no preference
74
158
window . postMessage ( request , '*' ) ;
75
159
}
76
- if ( action === 'portDisconnect' ) {
160
+ if ( action === 'portDisconnect' && ! currentPort && ! isAttemptingReconnect ) {
161
+ console . log ( 'Received disconnect message, initiating reconnection' ) ;
77
162
// When we receive a port disconnection message, relay it to the window
78
163
window . postMessage (
79
164
{
@@ -89,6 +174,7 @@ chrome.runtime.onMessage.addListener((request) => {
89
174
if ( action === 'reinitialize' ) {
90
175
window . postMessage ( request , '*' ) ;
91
176
}
177
+ return true ;
92
178
}
93
179
} ) ;
94
180
0 commit comments