@@ -225,31 +225,24 @@ const disconnect = () => new Promise((resolve, reject) => {
225
225
return resolve ( ) ;
226
226
} ) ;
227
227
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 ) {
235
233
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 ( ) ;
241
237
242
- // Remove the connection
243
- connection = null ;
238
+ // Remove the connection
239
+ connection = null ;
240
+ }
244
241
245
- return resolve ( ) ;
246
- } )
247
- . then ( ( ) => {
248
242
// Reconnect using the new token
249
243
const reconnectOptions = Object . assign ( { } , connectionOptions , { token } ) ;
250
- return connect ( reconnectOptions ) ;
251
- } )
252
- . then ( ( ) => {
244
+ await connect ( reconnectOptions ) ;
245
+
253
246
// Re-subscribe to all topics subscribed before the reconnection
254
247
Object . values ( subscribedTopics ) . forEach ( ( subscribeParams ) => {
255
248
subscribe ( subscribeParams . topic , subscribeParams . cb ) ;
@@ -259,31 +252,21 @@ const updateToken = token => new Promise(((updateTokenResolve, updateTokenReject
259
252
// Call the connection callback (with the reconnection param set to true)
260
253
connectionOptions . onConnected ( true ) ;
261
254
}
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
- } ;
279
255
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 ) ;
282
262
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
+ } ;
287
270
288
271
const subscribe = ( topic , cb ) => new Promise ( ( resolve , reject ) => {
289
272
if ( ! connection ) {
0 commit comments