@@ -2,6 +2,9 @@ import Auth from './auth.js'
22import ServerError from './serverError.js'
33const io = require ( 'socket.io-client' )
44const queue = require ( 'async-delay-queue' )
5+ const timeout = ( fn , s ) => {
6+ return new Promise ( resolve => setTimeout ( ( ) => resolve ( fn ( ) ) , s ) )
7+ }
58
69class Connection {
710 constructor ( options ) {
@@ -22,7 +25,7 @@ class Connection {
2225 /**
2326 * Socket.io client with currently stored tokens
2427 */
25- async setClient ( ) {
28+ async setClient ( skipListeners ) {
2629 let sioConfig = this . auth . access_token ? {
2730 query : 'bearer=' + this . auth . access_token ,
2831 reconnection : true
@@ -32,16 +35,24 @@ class Connection {
3235
3336 // Connect to parent namespace
3437 this . client = io . connect ( this . options . api_url + this . options . namespace , sioConfig )
35- this . client . on ( 'error' , ( ) => this . reload ( ) )
36- this . client . on ( 'connect_error' , ( ) => this . reload ( ) )
37- this . client . on ( 'disconnect' , ( ) => this . reload ( ) )
38- this . client . on ( 'connect' , ( ) => {
39- this . reconnecting = false
40- this . subscriptions . forEach ( sub => this . client . emit ( 'subscribe' , sub ) )
41- } )
42- this . client . on ( 'subscribed' , sub => {
43- if ( ! this . subscriptions . includes ( sub ) ) this . subscriptions . push ( sub )
44- } )
38+
39+ // Event listeners
40+ if ( ! skipListeners ) {
41+ this . client . on ( 'error' , ( ) => this . reload ( ) )
42+ this . client . on ( 'connect_error' , ( ) => this . reload ( ) )
43+ this . client . on ( 'disconnect' , ( ) => this . reload ( ) )
44+ this . client . on ( 'connect' , ( ) => {
45+ this . reconnecting = false
46+ this . subscriptions . forEach ( sub => this . client . emit ( 'subscribe' , sub ) )
47+ } )
48+ this . client . on ( 'subscribed' , sub => {
49+ if ( ! this . subscriptions . includes ( sub ) ) this . subscriptions . push ( sub )
50+ } )
51+ }
52+
53+ await timeout ( ( ) => {
54+ if ( ! this . client . connected ) this . setClient ( true )
55+ } , 10000 )
4556 }
4657
4758 /**
0 commit comments