@@ -225,39 +225,65 @@ const disconnect = () => new Promise((resolve, reject) => {
225
225
return resolve ( ) ;
226
226
} ) ;
227
227
228
- const updateToken = token => new Promise ( ( resolve , reject ) => {
228
+ const updateToken = token => new Promise ( ( ( updateTokenResolve , updateTokenReject ) => {
229
229
if ( ! connection ) {
230
- return reject ( new Error ( 'disconnection failed: connection closed' ) ) ;
230
+ return updateTokenReject ( new Error ( 'disconnection failed: connection closed' ) ) ;
231
231
}
232
232
233
- try {
234
- // Disconnect to the connection using the old token
235
- connection . disconnect ( ) ;
236
- } catch ( error ) {
237
- // Ignore disconnection errors that comes out when Paho is reconnecting
238
- }
233
+ // Wrap the update token process into a single promise-returning function
234
+ const updateTokenPromise = ( ) => new Promise ( ( resolve ) => {
235
+ 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
+ }
239
241
240
- // Remove the connection
241
- connection = null ;
242
+ // Remove the connection
243
+ connection = null ;
242
244
243
- return resolve ( ) ;
244
- } )
245
- . then ( ( ) => {
246
- // Reconnect using the new token
247
- const reconnectOptions = Object . assign ( { } , connectionOptions , { token } ) ;
248
- return connect ( reconnectOptions ) ;
245
+ return resolve ( ) ;
249
246
} )
250
- . then ( ( ) => {
251
- // Re-subscribe to all topics subscribed before the reconnection
252
- Object . values ( subscribedTopics ) . forEach ( ( subscribeParams ) => {
253
- subscribe ( subscribeParams . topic , subscribeParams . cb ) ;
247
+ . then ( ( ) => {
248
+ // Reconnect using the new token
249
+ const reconnectOptions = Object . assign ( { } , connectionOptions , { token } ) ;
250
+ return connect ( reconnectOptions ) ;
251
+ } )
252
+ . then ( ( ) => {
253
+ // Re-subscribe to all topics subscribed before the reconnection
254
+ Object . values ( subscribedTopics ) . forEach ( ( subscribeParams ) => {
255
+ subscribe ( subscribeParams . topic , subscribeParams . cb ) ;
256
+ } ) ;
257
+
258
+ if ( typeof connectionOptions . onConnected === 'function' ) {
259
+ // Call the connection callback (with the reconnection param set to true)
260
+ connectionOptions . onConnected ( true ) ;
261
+ }
254
262
} ) ;
255
263
256
- if ( typeof connectionOptions . onConnected === 'function' ) {
257
- // Call the connection callback (with the reconnection param set to true)
258
- connectionOptions . onConnected ( true ) ;
259
- }
260
- } ) ;
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
+
280
+ // Try to refresh the token every 30 secs
281
+ updateTokenInterval = setInterval ( updateTokenIntervalFunction , 30000 ) ;
282
+
283
+ // Try immediately to refresh the token - if it fails the next
284
+ // tentative will be started by the setInterval
285
+ updateTokenIntervalFunction ( ) ;
286
+ } ) ) ;
261
287
262
288
const subscribe = ( topic , cb ) => new Promise ( ( resolve , reject ) => {
263
289
if ( ! connection ) {
0 commit comments