@@ -72,7 +72,7 @@ class RequestPromise {
7272 reject ! : ( reason ?: any ) => void
7373 reconnect ?: ( ) => void
7474
75- // Required to proeprly handle rate limits
75+ // Required to properly handle rate limits
7676 sendData : ( ) => void = ( ) => { }
7777
7878 constructor (
@@ -92,14 +92,19 @@ class RequestPromise {
9292
9393const globalRPCHandler : RPCHandler = new RPCHandler ( )
9494
95+ interface OnConnectHandler {
96+ resolve : ( ) => void
97+ reject : ( err : Error ) => void
98+ }
99+
95100class Connection implements ClientConnection {
96101 private websocket : ClientSocket | null = null
97102 binaryMode = false
98103 compressionMode = false
99104 private readonly requests = new Map < ReqId , RequestPromise > ( )
100105 private lastId = 0
101106 private interval : number | undefined
102- private dialTimer : any | undefined
107+ private dialTimer : number | undefined
103108
104109 private sockets = 0
105110 private openAction : any
@@ -166,12 +171,13 @@ class Connection implements ClientConnection {
166171 }
167172
168173 private schedulePing ( socketId : number ) : void {
169- clearInterval ( this . interval )
170174 this . pingResponse = Date . now ( )
171175 const wsocket = this . websocket
172- const interval = setInterval ( ( ) => {
176+
177+ clearInterval ( this . interval )
178+ this . interval = setInterval ( ( ) => {
173179 if ( wsocket !== this . websocket ) {
174- clearInterval ( interval )
180+ clearInterval ( this . interval )
175181 return
176182 }
177183 if ( ! this . upgrading && this . pingResponse !== 0 && Date . now ( ) - this . pingResponse > hangTimeout ) {
@@ -203,14 +209,19 @@ class Connection implements ClientConnection {
203209 clearInterval ( this . interval )
204210 }
205211 } , pingTimeout )
206- this . interval = interval
207212 }
208213
209214 async close ( ) : Promise < void > {
210215 this . closed = true
211216 clearTimeout ( this . openAction )
212217 clearTimeout ( this . dialTimer )
213218 clearInterval ( this . interval )
219+ for ( const handler of this . onConnectHandlers ) {
220+ handler . reject ( new Error ( 'Connection closed' ) )
221+ }
222+ for ( const req of this . requests . values ( ) ) {
223+ req . reject ( new Error ( 'Connection closed' ) )
224+ }
214225 if ( this . websocket !== null ) {
215226 this . websocket . close ( 1000 )
216227 this . websocket = null
@@ -222,7 +233,7 @@ class Connection implements ClientConnection {
222233 }
223234
224235 delay = 0
225- onConnectHandlers : ( ( ) => void ) [ ] = [ ]
236+ onConnectHandlers : OnConnectHandler [ ] = [ ]
226237
227238 private waitOpenConnection ( ctx : MeasureContext ) : Promise < void > | undefined {
228239 if ( this . isConnected ( ) ) {
@@ -233,9 +244,10 @@ class Connection implements ClientConnection {
233244 'wait-connection' ,
234245 { } ,
235246 ( ctx ) =>
236- new Promise ( ( resolve ) => {
237- this . onConnectHandlers . push ( ( ) => {
238- resolve ( )
247+ new Promise ( ( resolve , reject ) => {
248+ this . onConnectHandlers . push ( {
249+ resolve,
250+ reject
239251 } )
240252 // Websocket is null for first time
241253 this . scheduleOpen ( ctx , false )
@@ -294,7 +306,7 @@ class Connection implements ClientConnection {
294306 if ( resp . error !== undefined ) {
295307 if ( resp . error ?. code === UNAUTHORIZED . code || resp . terminate === true ) {
296308 if (
297- resp . error . code !== platform . status . WorkspaceArchived ||
309+ resp . error . code !== platform . status . WorkspaceArchived &&
298310 resp . error . code !== platform . status . WorkspaceNotFound
299311 ) {
300312 Analytics . handleError ( new PlatformError ( resp . error ) )
@@ -350,7 +362,7 @@ class Connection implements ClientConnection {
350362
351363 // We need to clear dial timer, since we recieve hello response.
352364 clearTimeout ( this . dialTimer )
353- this . dialTimer = null
365+ this . dialTimer = undefined
354366 this . lastHash = ( resp as HelloResponse ) . lastHash
355367
356368 const serverVersion = helloResp . serverVersion
@@ -374,7 +386,7 @@ class Connection implements ClientConnection {
374386 // Notify all waiting connection listeners
375387 const handlers = this . onConnectHandlers . splice ( 0 , this . onConnectHandlers . length )
376388 for ( const h of handlers ) {
377- h ( )
389+ h . resolve ( )
378390 }
379391
380392 for ( const [ , v ] of this . requests . entries ( ) ) {
@@ -540,12 +552,10 @@ class Connection implements ClientConnection {
540552 return
541553 }
542554 this . websocket = wsocket
543- const opened = false
544-
545- if ( this . dialTimer != null ) {
555+ if ( this . dialTimer === undefined ) {
546556 this . dialTimer = setTimeout ( ( ) => {
547- this . dialTimer = null
548- if ( ! opened && ! this . closed ) {
557+ this . dialTimer = undefined
558+ if ( ! this . closed ) {
549559 void this . opt ?. onDialTimeout ?.( ) ?. catch ( ( err ) => {
550560 this . ctx . error ( 'failed to handle dial timeout' , { err } )
551561 } )
@@ -652,16 +662,15 @@ class Connection implements ClientConnection {
652662 ctx . withSync ( 'send-hello' , { } , ( ) => this . websocket ?. send ( this . rpcHandler . serialize ( helloRequest , false ) ) )
653663 }
654664
655- wsocket . onerror = ( event : any ) => {
665+ // FIX: remove undefined variable 'opened'
666+ wsocket . onerror = ( ) => {
656667 if ( this . websocket !== wsocket ) {
657668 return
658669 }
659670 if ( this . delay < 3 ) {
660671 this . delay += 1
661672 }
662- if ( opened ) {
663- console . error ( 'client websocket error:' , socketId , this . url , this . workspace , this . user )
664- }
673+ console . error ( 'client websocket error:' , socketId , this . url , this . workspace , this . user )
665674 }
666675 }
667676
@@ -761,7 +770,7 @@ class Connection implements ClientConnection {
761770
762771 getAccount ( ) : Promise < Account > {
763772 if ( this . account !== undefined ) {
764- return clone ( this . account )
773+ return Promise . resolve ( clone ( this . account ) )
765774 }
766775 return this . sendRequest ( { method : 'getAccount' , params : [ ] } )
767776 }
0 commit comments