Skip to content

Commit 48e0146

Browse files
committed
Reduce number of failed connections
1 parent 3722329 commit 48e0146

4 files changed

Lines changed: 47 additions & 25 deletions

File tree

react_frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"@types/styled-components": "^5.1.34",
6363
"axios": "^1.12.0",
6464
"glob": "^8.0.3",
65-
"jderobot-commsmanager": "1.0.16",
65+
"jderobot-commsmanager": "1.0.24",
6666
"jderobot-ide-interface": "0.2.137",
6767
"jszip": "^3.10.1",
6868
"react-router-dom": "^7.12.0",

react_frontend/src/components/exercise/WebGUIContainer.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const connectApplication = (
4444
resizeRef?: MutableRefObject<HTMLImageElement | null>,
4545
resizeObserver?: ResizeObserver
4646
) => {
47-
const ref = useRef<NodeJS.Timer>();
47+
const intRef = useRef<number | null>(null);
4848

4949
const onStateChange = (message: ManagerMsg) => {
5050
const state = message.data.state as string;
@@ -95,19 +95,20 @@ export const connectApplication = (
9595
return;
9696
}
9797

98-
ref.current = setInterval(() => {
98+
const timer = window.setInterval(async () => {
9999
try {
100-
manager.send("gui", "start");
100+
await manager.send("gui", "start");
101101
} catch {
102-
end();
102+
window.clearInterval(timer);
103103
}
104104
}, 1000);
105+
intRef.current = timer;
105106
};
106107

107108
const end = () => {
108-
if (ref.current !== undefined) {
109-
clearInterval(ref.current);
110-
ref.current = undefined;
109+
if (intRef.current !== null) {
110+
window.clearInterval(intRef.current);
111+
intRef.current = null;
111112
}
112113
};
113114
};

react_frontend/src/components/layouts/ExerciseContainer.tsx

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const ExerciseContainer = ({
4747
multiLanguage: boolean;
4848
children: JSX.Element;
4949
}) => {
50+
const hasTriedToConnect = useRef(false);
51+
const timeoutRef = useRef<number | null>(null);
52+
const connectTimeoutRef = useRef<number | null>(null);
5053
const [manager, setManager] = useState<CommsManager | null>(null);
5154
const [universes, setUniverses] = useState<string[] | undefined>(undefined);
5255
const toolsList = getTools(manager, tools, children);
@@ -76,15 +79,26 @@ const ExerciseContainer = ({
7679
};
7780

7881
useEffect(() => {
79-
const manager = CommsManager.getInstance();
80-
setManager(manager);
8182
subscribe("CommsManagerStateChange", resetUniverse);
8283

8384
return () => {
8485
unsubscribe("CommsManagerStateChange", () => {});
85-
const currManager = CommsManager.getInstance();
86-
if (currManager) {
87-
currManager.disconnect();
86+
87+
if (hasTriedToConnect.current) {
88+
const currManager = CommsManager.getInstance();
89+
if (currManager) {
90+
currManager.disconnect();
91+
CommsManager.deleteInstance();
92+
setManager(null);
93+
}
94+
}
95+
96+
if (timeoutRef.current) {
97+
window.clearTimeout(timeoutRef.current);
98+
}
99+
100+
if (connectTimeoutRef.current) {
101+
window.clearTimeout(connectTimeoutRef.current);
88102
}
89103
};
90104
}, []);
@@ -93,13 +107,9 @@ const ExerciseContainer = ({
93107
desiredState?: string,
94108
callback?: () => void
95109
) => {
96-
console.log(manager?.getState(), CommsManager.getInstance().getState());
97-
if (!manager || manager?.getState() != "idle") {
98-
return;
99-
}
100110
try {
101111
const currManager = CommsManager.getInstance();
102-
console.log(currManager);
112+
hasTriedToConnect.current = true;
103113
await currManager.connect();
104114
getUniverseList(project);
105115
console.log("Connected!", currManager.getState());
@@ -110,15 +120,26 @@ const ExerciseContainer = ({
110120
}
111121
} catch {
112122
console.log("Connection failed, trying again!");
113-
setTimeout(connectWithRetry, 2000, desiredState, callback);
123+
timeoutRef.current = window.setTimeout(
124+
connectWithRetry,
125+
2000,
126+
desiredState,
127+
callback
128+
);
114129
}
115130
};
116131

117132
const waitManagerState = async (state: string, callback: () => void) => {
118-
if (manager?.getState() === state) {
133+
const currManager = CommsManager.getInstance();
134+
if (currManager?.getState() === state) {
119135
callback();
120136
} else {
121-
return setTimeout(waitManagerState, 100, state, callback);
137+
connectTimeoutRef.current = window.setTimeout(
138+
waitManagerState,
139+
100,
140+
state,
141+
callback
142+
);
122143
}
123144
};
124145

react_frontend/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,10 +3976,10 @@ jackspeak@^3.1.2:
39763976
optionalDependencies:
39773977
"@pkgjs/parseargs" "^0.11.0"
39783978

3979-
jderobot-commsmanager@1.0.16:
3980-
version "1.0.16"
3981-
resolved "https://registry.npmjs.org/jderobot-commsmanager/-/jderobot-commsmanager-1.0.16.tgz"
3982-
integrity sha512-rEMr1DRqcexrtvyzNvLsWW4+DOVj+V8l4eI8HP7IAWc25NbK+OtexXJZ1Fk+v+WSdffE8n0J0k17rIryzFEm3w==
3979+
jderobot-commsmanager@1.0.24:
3980+
version "1.0.24"
3981+
resolved "https://registry.yarnpkg.com/jderobot-commsmanager/-/jderobot-commsmanager-1.0.24.tgz#b6e7a0ff0e886ee051c4693ccfd0de54b8eca72e"
3982+
integrity sha512-mbdgWG+O6cWPxQs16jdTbVjHQ65/lANJyVCEgzr1FZKi9bHPNrJFjGQW0qqVksIzuVwr4+zmg2PCFcTIAe2seQ==
39833983
dependencies:
39843984
uuid "^11.1.0"
39853985

0 commit comments

Comments
 (0)