Skip to content

Commit 9f557c8

Browse files
Christopher StamperChristopher Stamper
authored andcommitted
Made progress on fixing connection
1 parent 70d1b37 commit 9f557c8

File tree

5 files changed

+92
-41
lines changed

5 files changed

+92
-41
lines changed

src/app/FrontendTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export interface InitialStateProps {
143143
tabs: unknown;
144144
currentTabInApp: null | string;
145145
connectionStatus: boolean;
146-
reconnectRequested: boolean;
146+
connectRequested: boolean;
147147
}
148148

149149
export interface DiffProps {

src/app/RTKslices.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const initialState: InitialStateProps = { // we initialize what our initialState
99
tabs: {},
1010
currentTabInApp: null,
1111
connectionStatus: true,
12-
reconnectRequested: false,
12+
connectRequested: true,
1313
};
1414

1515
const findName = (index, obj) => {
@@ -489,12 +489,12 @@ export const mainSlice = createSlice({
489489
},
490490

491491
startReconnect: (state) => {
492-
state.reconnectRequested = true;
492+
state.connectRequested = true;
493493
state.port = initialState.port;
494494
},
495495

496-
endReconnect: (state) => {
497-
state.reconnectRequested = false;
496+
endConnect: (state) => {
497+
state.connectRequested = false;
498498
state.connectionStatus = true;
499499
}
500500

@@ -530,5 +530,5 @@ export const {
530530
deleteSeries,
531531
disconnected,
532532
startReconnect,
533-
endReconnect,
533+
endConnect,
534534
} = mainSlice.actions

src/app/containers/ButtonsContainer.tsx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,35 @@ function ButtonsContainer(): JSX.Element {
6464
}
6565

6666
const handleReconnectConfirm = () => {
67-
const maxRetries = 10;
68-
const retryInterval = 1000;
69-
const maxTimeout = 15000
70-
71-
const attemptReconnection = (retries: number, startTime: number) => {
72-
console.log('WORKING')
73-
if (retries <= maxRetries && Date.now() - startTime < maxTimeout) {
74-
console.log('HITTING IF');
75-
chrome.runtime.sendMessage({ action: 'attemptReconnect' }, (response) => {
76-
console.log('response: ', response);
77-
// if (response && response.success) {
78-
// console.log('SUCCESS')
79-
// } else {
80-
// console.log('Reconnect failed: ', !response && response.success);
67+
// const maxRetries = 10;
68+
// const retryInterval = 1000;
69+
// const maxTimeout = 15000
70+
71+
// const attemptReconnection = (retries: number, startTime: number) => {
72+
// console.log('WORKING')
73+
// if (retries <= maxRetries && Date.now() - startTime < maxTimeout) {
74+
// console.log('HITTING IF');
75+
// chrome.runtime.sendMessage({ action: 'attemptReconnect' }, (response) => {
76+
// console.log('response: ', response);
77+
// // if (response && response.success) {
78+
// // console.log('SUCCESS')
79+
// // } else {
80+
// // console.log('Reconnect failed: ', !response && response.success);
8181

82-
// setTimeout(() => {
83-
// console.log('trying!')
84-
// attemptReconnection(retries + 1, startTime);
85-
// }, retryInterval);
86-
// }
87-
});
88-
} else {
89-
console.log('Max tries completed');
90-
}
91-
}
82+
// // setTimeout(() => {
83+
// // console.log('trying!')
84+
// // attemptReconnection(retries + 1, startTime);
85+
// // }, retryInterval);
86+
// // }
87+
// });
88+
// } else {
89+
// console.log('Max tries completed');
90+
// }
91+
// }
9292

9393
handleReconnectCancel();
94-
attemptReconnection(1, Date.now());
95-
// dispatch(startReconnect);
94+
dispatch(startReconnect);
95+
// attemptReconnection(1, Date.now());
9696
}
9797

9898
const handleReconnectCancel = () => {

src/app/containers/MainContainer.tsx

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
noDev,
1515
setCurrentLocation,
1616
disconnected,
17-
endReconnect,
17+
endConnect,
1818
launchContentScript,
1919
} from '../RTKslices';
2020
import { useDispatch, useSelector } from 'react-redux';
@@ -31,9 +31,10 @@ function MainContainer(): JSX.Element {
3131
const currentTab = useSelector((state: any) => state.main.currentTab);
3232
const tabs = useSelector((state: any) => state.main.tabs);
3333
const port = useSelector((state: any) => state.main.port);
34-
const { connectionStatus, reconnectRequested } = useSelector((state: any) => state.main);
34+
const { connectionStatus, connectRequested } = useSelector((state: any) => state.main);
3535

3636
const [actionView, setActionView] = useState(true); // We create a local state 'actionView' and set it to true
37+
const [currentPort, setCurrentPort] = useState(null);
3738

3839
// this function handles Time Jump sidebar view
3940
const toggleActionContainer = () => {
@@ -58,11 +59,58 @@ function MainContainer(): JSX.Element {
5859
}
5960
}
6061

62+
const handleConnect = () => {
63+
const maxRetries = 10;
64+
const retryInterval = 1000;
65+
const maxTimeout = 15000
66+
67+
return new Promise((resolve) => {
68+
let port: chrome.runtime.Port;
69+
console.log('init port: ', port)
70+
71+
const attemptReconnection = (retries: number, startTime: number) => {
72+
// console.log('WORKING')
73+
if (retries <= maxRetries && Date.now() - startTime < maxTimeout) {
74+
if (retries === 1) port = chrome.runtime.connect();
75+
console.log('HITTING IF');
76+
chrome.runtime.sendMessage({ action: 'attemptReconnect' }, (response) => {
77+
if (response && response.success) {
78+
console.log('Reconnect Success: ', response.success);
79+
resolve(port);
80+
} else {
81+
console.log('Reconnect failed: ', !response && response.success);
82+
83+
setTimeout(() => {
84+
console.log('trying!')
85+
attemptReconnection(retries + 1, startTime);
86+
}, retryInterval);
87+
}
88+
});
89+
} else {
90+
console.log('PORT CONNECT FAILED');
91+
resolve(null);
92+
}
93+
};
94+
attemptReconnection(1, Date.now());
95+
});
96+
}
97+
6198
useEffect(() => {
62-
if (port) return; // only open port once so if it exists, do not run useEffect again
99+
if (!connectRequested) return; // only open port once so if it exists, do not run useEffect again
63100

64101
// 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.
65-
const currentPort = chrome.runtime.connect(); // we connect to our service worker
102+
103+
handleConnect()
104+
.then((port) => {
105+
if (port) {
106+
console.log('PORT SUCCESS: ', port)
107+
setCurrentPort(port);
108+
} else {
109+
console.log('PORT FAIL: ', port);
110+
}
111+
});
112+
113+
console.log('that port tho: ', currentPort);
66114

67115
// listen for a message containing snapshots from the /extension/build/background.js service worker
68116
currentPort.onMessage.addListener(
@@ -129,9 +177,7 @@ function MainContainer(): JSX.Element {
129177
if (currentPort)
130178
dispatch(setPort(currentPort));
131179

132-
if (!connectionStatus && reconnectRequested) {
133-
dispatch(endReconnect());
134-
}
180+
dispatch(endConnect());
135181
});
136182

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

src/extension/background.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
137137
This allows us to set up listener's for when we connect, message, and disconnect the script.
138138
*/
139139

140+
let portSuccessfullyConnected = false;
141+
140142
// Establishing incoming connection with Reactime.
141143
chrome.runtime.onConnect.addListener((port) => {
142144
/*
@@ -158,6 +160,9 @@ chrome.runtime.onConnect.addListener((port) => {
158160
159161
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.
160162
*/
163+
164+
console.log('Port: ', port);
165+
portSuccessfullyConnected = port ? true : false;
161166

162167
portsArr.push(port); // push each Reactime communication channel object to the portsArr
163168

@@ -291,9 +296,9 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
291296

292297
switch (action) {
293298
case 'attemptReconnect': {
294-
console.log('AYO');
299+
console.log('portConnection: ', portSuccessfullyConnected);
295300

296-
const success = true;
301+
const success = portSuccessfullyConnected;
297302
sendResponse({ success });
298303
break;
299304
}

0 commit comments

Comments
 (0)