Skip to content

Commit 7425161

Browse files
committed
Rewrite the updateToken function as asyncFunction, prevent duplicate connections
1 parent 4dcdc37 commit 7425161

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ module.exports = {
33
"env": {
44
"browser": true,
55
"jest": true
6+
},
7+
"rules": {
8+
"no-await-in-loop": 0
69
}
710
};

src/index.js

+26-43
Original file line numberDiff line numberDiff line change
@@ -225,31 +225,24 @@ const disconnect = () => new Promise((resolve, reject) => {
225225
return resolve();
226226
});
227227

228-
const updateToken = token => new Promise(((updateTokenResolve, updateTokenReject) => {
229-
if (!connection) {
230-
return updateTokenReject(new Error('disconnection failed: connection closed'));
231-
}
232-
233-
// Wrap the update token process into a single promise-returning function
234-
const updateTokenPromise = () => new Promise((resolve) => {
228+
const updateToken = async function updateToken(token) {
229+
// This infinite loop will exit once the reconnection is successful -
230+
// and will pause between each reconnection tentative, every 5 secs.
231+
// eslint-disable-next-line no-constant-condition
232+
while (true) {
235233
try {
236-
// Disconnect to the connection using the old token
237-
connection.disconnect();
238-
} catch (error) {
239-
// Ignore disconnection errors that comes out when Paho is reconnecting
240-
}
234+
if (connection) {
235+
// Disconnect to the connection that is using the old token
236+
connection.disconnect();
241237

242-
// Remove the connection
243-
connection = null;
238+
// Remove the connection
239+
connection = null;
240+
}
244241

245-
return resolve();
246-
})
247-
.then(() => {
248242
// Reconnect using the new token
249243
const reconnectOptions = Object.assign({}, connectionOptions, { token });
250-
return connect(reconnectOptions);
251-
})
252-
.then(() => {
244+
await connect(reconnectOptions);
245+
253246
// Re-subscribe to all topics subscribed before the reconnection
254247
Object.values(subscribedTopics).forEach((subscribeParams) => {
255248
subscribe(subscribeParams.topic, subscribeParams.cb);
@@ -259,31 +252,21 @@ const updateToken = token => new Promise(((updateTokenResolve, updateTokenReject
259252
// Call the connection callback (with the reconnection param set to true)
260253
connectionOptions.onConnected(true);
261254
}
262-
});
263-
264-
let updateTokenInterval = null;
265-
266-
// It runs the token update. If it succeed, clears the interval and
267-
// exits updateToken.
268-
const updateTokenIntervalFunction = () => {
269-
updateTokenPromise()
270-
.then(() => {
271-
// Token update went well - exiting
272-
clearInterval(updateTokenInterval);
273-
return updateTokenResolve();
274-
})
275-
.catch(() => {
276-
// Ignore reconnection errors - keep trying to reconnect
277-
});
278-
};
279255

280-
// Try to refresh the token every 30 secs
281-
updateTokenInterval = setInterval(updateTokenIntervalFunction, 30000);
256+
// Exit the infinite loop
257+
return;
258+
} catch (error) {
259+
// Expose paho-mqtt errors
260+
// eslint-disable-next-line no-console
261+
console.error(error);
282262

283-
// Try immediately to refresh the token - if it fails the next
284-
// tentative will be started by the setInterval
285-
updateTokenIntervalFunction();
286-
}));
263+
// Something went wrong during the reconnection - retry in 5 secs.
264+
await new Promise((resolve) => {
265+
setTimeout(resolve, 5000);
266+
});
267+
}
268+
}
269+
};
287270

288271
const subscribe = (topic, cb) => new Promise((resolve, reject) => {
289272
if (!connection) {

0 commit comments

Comments
 (0)