Skip to content

Commit cc110fc

Browse files
Christopher StamperChristopher Stamper
authored andcommitted
Finished reconnect functionality
1 parent 05b9768 commit cc110fc

File tree

5 files changed

+49
-46
lines changed

5 files changed

+49
-46
lines changed

src/app/RTKslices.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,24 +511,20 @@ export const mainSlice = createSlice({
511511
disconnected: (state) => {
512512
console.log('disconnected: ', current(state));
513513
state.connectionStatus = false;
514-
state.port = initialState.port;
514+
// state.port = initialState.port;
515515
console.log('disconnected: state end', current(state));
516516
},
517517
startReconnect: (state) => {
518518
console.log('startReconnect: ', current(state));
519519
state.reconnectRequested = true;
520+
state.port = initialState.port;
520521
console.log('startReconnect: state end', current(state));
521522
},
522523
endReconnect: (state) => {
523524
console.log('startReconnect: ', current(state));
524525
state.reconnectRequested = false;
525526
state.connectionStatus = true;
526527
console.log('startReconnect: state end', current(state));
527-
},
528-
firstInitialization: (state) => {
529-
console.log('firstInitialization: ', current(state));
530-
state.hasInitalized = true;
531-
console.log('firstInitialization: state end', current(state));
532528
}
533529
},
534530
})
@@ -563,7 +559,6 @@ export const {
563559
disconnected,
564560
startReconnect,
565561
endReconnect,
566-
firstInitialization
567562
} = mainSlice.actions
568563

569564

src/app/containers/ButtonsContainer.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
//importing useState from react to handle local state for button reconnect functionality
3-
import { useState } from 'react';
3+
import { useState, useEffect } from 'react';
44
// import { importSnapshots, toggleMode } from '../actions/actions';
55
// import { useStoreContext } from '../store';
66
import { Button } from '@mui/material';
@@ -61,23 +61,29 @@ function ButtonsContainer(): JSX.Element {
6161
const { mode: { paused }} = tabs[currentTab];
6262
//adding a local state using useState for the reconnect button functionality
6363
const [reconnectDialogOpen, setReconnectDialogOpen] = useState(false);
64+
const [disconnectedDialogOpen, setDisconnectedDialogOpen] = useState(false);
6465

6566
//logic for handling dialog box opening and closing
6667
const handleReconnectClick = () => {
67-
connectionStatus ? setReconnectDialogOpen(true) : dispatch(startReconnect());
68+
connectionStatus ? setReconnectDialogOpen(true) : setDisconnectedDialogOpen(true);
6869
}
6970

7071
const handleReconnectConfirm = () => {
7172
//reconnection logic here
7273
dispatch(startReconnect());
73-
setReconnectDialogOpen(false);
74+
// reconnectDialogOpen ? setReconnectDialogOpen(false) : setDisconnectedDialogOpen(false);
75+
handleReconnectCancel();
7476
}
7577

7678
const handleReconnectCancel = () => {
7779
//closing the dialog
78-
setReconnectDialogOpen(false);
80+
reconnectDialogOpen ? setReconnectDialogOpen(false) : setDisconnectedDialogOpen(false);
7981
}
8082

83+
useEffect(() => {
84+
if (!connectionStatus) setDisconnectedDialogOpen(true);
85+
}, [connectionStatus])
86+
8187
return (
8288
<div className='buttons-container'>
8389
<Button
@@ -138,7 +144,7 @@ function ButtonsContainer(): JSX.Element {
138144
</Button>
139145
</DialogActions>
140146
</Dialog>
141-
<Dialog open={!connectionStatus} onClose={handleReconnectCancel}>
147+
<Dialog open={disconnectedDialogOpen} onClose={handleReconnectCancel}>
142148
<DialogTitle>Reactime Disconnected</DialogTitle>
143149
<DialogContent>
144150
{/* //insert info here on current connection status */}

src/app/containers/MainContainer.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
setCurrentLocation,
1616
disconnected,
1717
endReconnect,
18-
firstInitialization,
1918
pause,
2019
} from '../RTKslices';
2120
import { useDispatch, useSelector } from 'react-redux';
@@ -58,10 +57,11 @@ function MainContainer(): JSX.Element {
5857
}
5958
};
6059

61-
const handleDisconnect = (): void => {
62-
console.log('unexpected port disconnect');
63-
64-
dispatch(disconnected());
60+
const handleDisconnect = (msg): void => {
61+
if (msg === 'portDisconnect') {
62+
console.log('unexpected port disconnect: ', msg);
63+
dispatch(disconnected());
64+
}
6565
}
6666

6767
useEffect(() => {
@@ -119,33 +119,26 @@ function MainContainer(): JSX.Element {
119119
}
120120
default:
121121
}
122-
return true; // we return true so that the connection stays open, otherwise the message channel will close
122+
// return true; // we return true so that the connection stays open, otherwise the message channel will close
123123
},
124124
);
125125

126-
if (currentPort.onDisconnect.hasListener(handleDisconnect))
127-
currentPort.onDisconnect.removeListener(handleDisconnect)
128126

127+
if (chrome.runtime.onMessage.hasListener(handleDisconnect))
128+
chrome.runtime.onMessage.removeListener(handleDisconnect);
129+
129130
// used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
130-
currentPort.onDisconnect.addListener(handleDisconnect);
131+
chrome.runtime.onMessage.addListener(handleDisconnect);
131132

132-
// setInterval(() => {
133+
// setTimeout(() => {
133134
// console.log('disconnecting')
134135
// currentPort.disconnect();
135-
// dispatch(disconnected());
136-
// }, 15000);
137-
138-
// console.log('Look for listeners bro: ', currentPort.onDisconnect.hasListener(handleDisconnect));
136+
// }, 30000);
139137

140138
if (currentPort) dispatch(setPort(currentPort)); // assign port to state so it could be used by other components
141139
if (!connectionStatus && reconnectRequested) dispatch(endReconnect());
142140
});
143141

144-
// useEffect(() => {
145-
// console.log('Onlys runs on initialzation');
146-
// dispatch(firstInitialization());
147-
// }, [])
148-
149142
// Error Page launch IF(Content script not launched OR RDT not installed OR Target not React app)
150143
if (
151144
!tabs[currentTab] ||

src/extension/background.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,14 @@ chrome.runtime.onConnect.addListener((port) => {
159159
Again, this port object is used for communication within your extension, not for communication with external ports or tabs in the Chrome browser. If you need to interact with specific tabs or external ports, you would use other APIs or methods, such as chrome.tabs or other Chrome Extension APIs.
160160
*/
161161

162+
console.log('ATTEMPTING CONNECTION');
162163
portsArr.push(port); // push each Reactime communication channel object to the portsArr
164+
console.log('portsArr: ', portsArr);
163165

164166
// On Reactime launch: make sure RT's active tab is correct
165167
if (portsArr.length > 0) {
166168
console.log('yo');
169+
console.log('activeTab: ', activeTab)
167170
portsArr.forEach((bg) => {// go through each port object (each Reactime instance)
168171
bg.postMessage({ // send passed in action object as a message to the current port
169172
action: 'changeTab',
@@ -183,9 +186,11 @@ chrome.runtime.onConnect.addListener((port) => {
183186

184187
// every time devtool is closed, remove the port from portsArr
185188
port.onDisconnect.addListener((e) => {
189+
console.log('PORT DISCONNECTED BACKGROUND');
186190
for (let i = 0; i < portsArr.length; i += 1) {
187191
if (portsArr[i] === e) {
188192
portsArr.splice(i, 1);
193+
chrome.runtime.sendMessage('portDisconnect');
189194
break;
190195
}
191196
}
@@ -218,7 +223,7 @@ chrome.runtime.onConnect.addListener((port) => {
218223
tabsObj[tabId].currParent = payload.currParent; // reset currParent to last state recorded
219224
tabsObj[tabId].currBranch = payload.currBranch; // reset currBranch to last state recorded
220225

221-
return true; // return true so that port remains open
226+
// return true; // return true so that port remains open
222227

223228
case 'emptySnap':
224229
tabsObj[tabId].snapshots = [tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1]]; // reset snapshots to page last state recorded
@@ -230,29 +235,29 @@ chrome.runtime.onConnect.addListener((port) => {
230235
tabsObj[tabId].index = 1; //reset index
231236
tabsObj[tabId].currParent = 0; // reset currParent
232237
tabsObj[tabId].currBranch = 1; // reset currBranch
233-
return true; // return true so that port remains open
238+
// return true; // return true so that port remains open
234239

235240
case 'setPause': // Pause = lock on tab
236241
tabsObj[tabId].mode.paused = payload;
237-
return true; // return true so that port remains open
242+
// return true; // return true so that port remains open
238243

239244
case 'launchContentScript':
240245
chrome.scripting.executeScript({
241246
target: { tabId },
242247
files: ['bundles/content.bundle.js'],
243248
});
244-
return true;
249+
// return true;
245250

246251
case 'jumpToSnap':
247252
chrome.tabs.sendMessage(tabId, msg);
248-
return true; // attempt to fix message port closing error, consider return Promise
253+
// return true; // attempt to fix message port closing error, consider return Promise
249254

250255
case 'toggleRecord':
251256
chrome.tabs.sendMessage(tabId, msg);
252-
return true;
257+
// return true;
253258

254259
default:
255-
return true;
260+
// return true;
256261
}
257262
});
258263
});
@@ -261,7 +266,7 @@ chrome.runtime.onConnect.addListener((port) => {
261266
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
262267
// AUTOMATIC MESSAGE SENT BY CHROME WHEN CONTENT SCRIPT IS FIRST LOADED: set Content
263268
if (request.type === 'SIGN_CONNECT') {
264-
return true;
269+
// return true;
265270
}
266271
const tabTitle = sender.tab.title;
267272
const tabId = sender.tab.id;
@@ -283,7 +288,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
283288
) {
284289
isReactTimeTravel = true;
285290
} else {
286-
return true;
291+
// return true;
287292
}
288293
// everytime we get a new tabId, add it to the object
289294
if (isReactTimeTravel && !(tabId in tabsObj)) {
@@ -392,7 +397,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
392397
default:
393398
break;
394399
}
395-
return true; // attempt to fix close port error
400+
// return true; // attempt to fix close port error
396401
});
397402

398403
// when tab is closed, remove the tabId from the tabsObj

src/extension/contentScript.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ window.addEventListener('message', (msg) => {
2020
chrome.runtime.sendMessage({ action: 'tabReload' });
2121
firstMessage = false;
2222
// const keepAliveContentScript = setInterval(() => { // interval to keep connection to service worker connection alive
23-
chrome.runtime.sendMessage({
24-
action: 'keepAlive' // messages sent to port to keep connection alive
25-
})
23+
// chrome.runtime.sendMessage({
24+
// action: 'keepAlive' // messages sent to port to keep connection alive
25+
// })
2626
// }, 295000) // messages must happen within five minutes
2727
}
2828

@@ -57,12 +57,15 @@ chrome.runtime.onMessage.addListener((request) => {
5757
}
5858
// After the jumpToSnap action has been sent back to background js,
5959
// it will send the same action to backend files (index.ts) for it execute the jump feature
60+
if (action === 'portDisconnect') console.log('RECEIVED PORT DISCONNECT MESSAGE contentScript')
6061
// '*' == target window origin required for event to be dispatched, '*' = no preference
61-
window.postMessage(request, '*');
62+
// window.postMessage(request, '*');
6263
}
63-
return true; // attempt to fix port closing console error
64+
// return true; // attempt to fix port closing console error
6465
});
6566

67+
68+
6669
// Performance metrics being calculated by the 'web-vitals' api and
6770
// sent as an object to background.js.
6871
// To learn more about Chrome web vitals, see https://web.dev/vitals/.
@@ -87,3 +90,4 @@ getCLS(gatherMetrics);
8790
// Send message to background.js for injecting the initial script
8891
// into the app's DOM.
8992
chrome.runtime.sendMessage({ action: 'injectScript' });
93+

0 commit comments

Comments
 (0)