Skip to content

Commit 05b9768

Browse files
Christopher StamperChristopher Stamper
authored andcommitted
Made progress on disconnection issue on frontend
1 parent ba6a634 commit 05b9768

File tree

7 files changed

+87
-57
lines changed

7 files changed

+87
-57
lines changed

src/app/FrontendTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export interface InitialStateProps {
144144
currentTabInApp: null | string;
145145
connectionStatus: boolean;
146146
reconnectRequested: boolean;
147+
hasInitialized: boolean;
147148
}
148149

149150
export interface DiffProps {

src/app/RTKslices.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const initialState: InitialStateProps = { // we initialize what our initialState
1010
currentTabInApp: null,
1111
connectionStatus: true,
1212
reconnectRequested: false,
13+
hasInitialized: false,
1314
};
1415

1516
const findName = (index, obj) => {
@@ -510,6 +511,7 @@ export const mainSlice = createSlice({
510511
disconnected: (state) => {
511512
console.log('disconnected: ', current(state));
512513
state.connectionStatus = false;
514+
state.port = initialState.port;
513515
console.log('disconnected: state end', current(state));
514516
},
515517
startReconnect: (state) => {
@@ -522,6 +524,11 @@ export const mainSlice = createSlice({
522524
state.reconnectRequested = false;
523525
state.connectionStatus = true;
524526
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));
525532
}
526533
},
527534
})
@@ -556,6 +563,7 @@ export const {
556563
disconnected,
557564
startReconnect,
558565
endReconnect,
566+
firstInitialization
559567
} = mainSlice.actions
560568

561569

src/app/components/App.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ import theme from './theme';
1313
This is used to determine the proper tutorial to render when How To button is pressed.
1414
*/
1515

16-
const initialState: InitialStateProps = { // we initialize what our initialState is here
17-
port: null,
18-
currentTab: null,
19-
currentTitle: 'No Target',
20-
tabs: {},
21-
currentTabInApp: null,
22-
connectionStatus: true,
23-
reconnectRequested: false,
24-
};
16+
// const initialState: InitialStateProps = { // we initialize what our initialState is here
17+
// port: null,
18+
// currentTab: null,
19+
// currentTitle: 'No Target',
20+
// tabs: {},
21+
// currentTabInApp: null,
22+
// connectionStatus: true,
23+
// reconnectRequested: false,
24+
// };
2525

2626
function App(): JSX.Element {
2727
return (
2828
<ThemeProvider theme={theme}>
2929
<Router>
3030
{/* we wrap our application with the <Router> tag so that all components that are nested will have the react-router context */}
31-
<StoreContext.Provider value={useReducer(mainReducer, initialState)}>
31+
{/* <StoreContext.Provider value={useReducer(mainReducer, initialState)}> */}
3232
{/* we wrap our MainContainer with the provider so that we will be able to use the store context. We create our store by using useReducer and passing it into the value property */}
3333
<MainContainer />
34-
</StoreContext.Provider>
34+
{/* </StoreContext.Provider> */}
3535
</Router>
3636
</ThemeProvider>
3737
);

src/app/containers/ButtonsContainer.tsx

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function ButtonsContainer(): JSX.Element {
5757
const currentTab = useSelector((state: any) => state.main.currentTab);
5858
const tabs = useSelector((state: any)=>state.main.tabs);
5959
const currentTabInApp = useSelector((state: any)=> state.main.currentTabInApp);
60-
const connectionStatus = useSelector((state: any)=> state.main.connectionStatus);
60+
const {connectionStatus, hasInitialized} = useSelector((state: any)=> state.main);
6161
const { mode: { paused }} = tabs[currentTab];
6262
//adding a local state using useState for the reconnect button functionality
6363
const [reconnectDialogOpen, setReconnectDialogOpen] = useState(false);
@@ -123,19 +123,41 @@ function ButtonsContainer(): JSX.Element {
123123
Reconnect
124124
</Button>
125125
<Dialog open={reconnectDialogOpen} onClose={handleReconnectCancel}>
126-
<DialogTitle>Reconnect Confirmation</DialogTitle>
127-
<DialogContent>
128-
{/* //insert info here on current connection status */}
129-
</DialogContent>
130-
<DialogActions>
131-
<Button onClick={handleReconnectCancel} color="primary">
132-
Cancel
133-
</Button>
134-
<Button onClick={handleReconnectConfirm} color="primary">
135-
Confirm Reconnect
136-
</Button>
137-
</DialogActions>
138-
</Dialog>
126+
<DialogTitle>WARNING</DialogTitle>
127+
<DialogContent>
128+
{/* //insert info here on current connection status */}
129+
<h3>Status: {connectionStatus ? 'Connected' : 'Disconnected'}</h3>
130+
Reconnecting while Reactime is still connected to application will clear all current data. Are you sure you want to proceed with the reconnection?
131+
</DialogContent>
132+
<DialogActions>
133+
<Button onClick={() => handleReconnectCancel()} color="primary">
134+
Cancel
135+
</Button>
136+
<Button onClick={() => handleReconnectConfirm()} color="primary">
137+
Confirm Reconnect
138+
</Button>
139+
</DialogActions>
140+
</Dialog>
141+
<Dialog open={!connectionStatus} onClose={handleReconnectCancel}>
142+
<DialogTitle>Reactime Disconnected</DialogTitle>
143+
<DialogContent>
144+
{/* //insert info here on current connection status */}
145+
<h3>Status: {connectionStatus ? 'Connected' : 'Disconnected'}</h3>
146+
Reactime has unexpectedly disconnected from your application. To continue using Reactime, please reconnect.
147+
WARNING: Reconnecting will clear all data currently stored in Reactime, so consider downloading the data before proceeding with the reconnection, if needed.
148+
</DialogContent>
149+
<DialogActions>
150+
<Button onClick={() => handleReconnectCancel()} color="primary">
151+
Cancel
152+
</Button>
153+
<Button onClick={() => exportHandler(tabs[currentTab])} color="primary">
154+
Download
155+
</Button>
156+
<Button onClick={() => handleReconnectConfirm()} color="primary">
157+
Reconnect
158+
</Button>
159+
</DialogActions>
160+
</Dialog>
139161
</div>
140162
);
141163
}

src/app/containers/MainContainer.tsx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
setCurrentLocation,
1616
disconnected,
1717
endReconnect,
18+
firstInitialization,
1819
pause,
1920
} from '../RTKslices';
2021
import { useDispatch, useSelector } from 'react-redux';
@@ -37,7 +38,7 @@ function MainContainer(): JSX.Element {
3738
const currentTab = useSelector((state: any) => state.main.currentTab);
3839
const tabs = useSelector((state: any) => state.main.tabs);
3940
const port = useSelector((state: any) => state.main.port);
40-
const { connectionStatus, reconnectRequested } = useSelector((state: any) => state.main);
41+
const { connectionStatus, reconnectRequested, hasInitialized } = useSelector((state: any) => state.main);
4142

4243
const [actionView, setActionView] = useState(true); // We create a local state 'actionView' and set it to true
4344

@@ -57,20 +58,19 @@ function MainContainer(): JSX.Element {
5758
}
5859
};
5960

61+
const handleDisconnect = (): void => {
62+
console.log('unexpected port disconnect');
63+
64+
dispatch(disconnected());
65+
}
66+
6067
useEffect(() => {
6168
console.log('LOL: ', port);
6269
if (port) return; // only open port once so if it exists, do not run useEffect again
6370
console.log('Okie')
6471
// chrome.runtime allows our application to retrieve our service worker (our eventual bundles/background.bundle.js after running npm run build), details about the manifest, and allows us to listen and respond to events in our application lifecycle.
6572
const currentPort = chrome.runtime.connect(); // we connect to our service worker
6673
console.log('currentPort', currentPort);
67-
const keepAliveMainContainer = () => { // interval to keep connection to background.js alive
68-
console.log('Hi :))');
69-
currentPort.postMessage({
70-
action: 'keepAlive' // messages sent to port to keep connection alive
71-
});
72-
} // messages must happen within ~five minutes
73-
setInterval(keepAliveMainContainer, 30000);
7474

7575
// listen for a message containing snapshots from the /extension/build/background.js service worker
7676
currentPort.onMessage.addListener(
@@ -123,27 +123,28 @@ function MainContainer(): JSX.Element {
123123
},
124124
);
125125

126-
// chrome.runtime.connect().onDisconnect.addListener(() => { // used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
127-
currentPort.onDisconnect.addListener(() => { // used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
128-
console.log('this port is disconnecting line 52');
129-
130-
dispatch(disconnected());
131-
});
126+
if (currentPort.onDisconnect.hasListener(handleDisconnect))
127+
currentPort.onDisconnect.removeListener(handleDisconnect)
132128

133-
console.log('currentPort', currentPort);
134-
dispatch(setPort(currentPort)); // assign port to state so it could be used by other components
135-
// if (!connectionStatus && reconnectRequested) dispatch(endReconnect());
136-
});
129+
// used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
130+
currentPort.onDisconnect.addListener(handleDisconnect);
137131

138-
// useEffect(() => {
139-
// if (initialization) return;
140-
// chrome.runtime.connect().onDisconnect.addListener(() => { // used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
141-
// console.log('this port is disconnecting line 52');
142-
132+
// setInterval(() => {
133+
// console.log('disconnecting')
134+
// currentPort.disconnect();
143135
// dispatch(disconnected());
144-
// });
145-
// });
136+
// }, 15000);
137+
138+
// console.log('Look for listeners bro: ', currentPort.onDisconnect.hasListener(handleDisconnect));
146139

140+
if (currentPort) dispatch(setPort(currentPort)); // assign port to state so it could be used by other components
141+
if (!connectionStatus && reconnectRequested) dispatch(endReconnect());
142+
});
143+
144+
// useEffect(() => {
145+
// console.log('Onlys runs on initialzation');
146+
// dispatch(firstInitialization());
147+
// }, [])
147148

148149
// Error Page launch IF(Content script not launched OR RDT not installed OR Target not React app)
149150
if (

src/extension/background.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,18 @@ chrome.runtime.onConnect.addListener((port) => {
163163

164164
// On Reactime launch: make sure RT's active tab is correct
165165
if (portsArr.length > 0) {
166+
console.log('yo');
166167
portsArr.forEach((bg) => {// go through each port object (each Reactime instance)
167168
bg.postMessage({ // send passed in action object as a message to the current port
168169
action: 'changeTab',
169170
payload: { tabId: activeTab.id, title: activeTab.title },
170171
})
171-
const keepAliveServiceWorker = setInterval(() => { // interval used to keep connection to MainContainer alive
172-
bg.postMessage({
173-
action: 'keepAlive' // messages sent to port to keep connection alive
174-
})
175-
}, 295000) // messages must happen within five minutes
176172
});
177173
}
178174

179175
// send tabs obj to the connected devtools as soon as connection to devtools is made
180176
if (Object.keys(tabsObj).length > 0) {
177+
console.log('yuh');
181178
port.postMessage({
182179
action: 'initialConnectSnapshots',
183180
payload: tabsObj,
@@ -197,6 +194,7 @@ chrome.runtime.onConnect.addListener((port) => {
197194
// listen for message containing a snapshot from devtools and send it to contentScript -
198195
// (i.e. they're all related to the button actions on Reactime)
199196
port.onMessage.addListener((msg) => {
197+
console.log('pls: ', msg);
200198
// msg is action denoting a time jump in devtools
201199
// ---------------------------------------------------------------
202200
// message incoming from devTools should look like this:

src/extension/contentScript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ window.addEventListener('message', (msg) => {
1919
// One-time request tells the background script that the tab has reloaded.
2020
chrome.runtime.sendMessage({ action: 'tabReload' });
2121
firstMessage = false;
22-
const keepAliveContentScript = setInterval(() => { // interval to keep connection to service worker connection alive
22+
// const keepAliveContentScript = setInterval(() => { // interval to keep connection to service worker connection alive
2323
chrome.runtime.sendMessage({
2424
action: 'keepAlive' // messages sent to port to keep connection alive
2525
})
26-
}, 295000) // messages must happen within five minutes
26+
// }, 295000) // messages must happen within five minutes
2727
}
2828

2929
// After tabs object has been created from firstMessage, backend (linkFiber.ts)

0 commit comments

Comments
 (0)