@@ -5,25 +5,31 @@ import Debug from 'debug'
55import WebSocket from 'ws'
66
77import { Scaffold , counterId , pLimitFn , pOne } from './utils'
8+ import { Todo } from './types'
89
9- const wait = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
10+ const wait = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
1011
1112// add global support for pretty millisecond formatting with %n
13+ // @ts -expect-error
1214Debug . formatters . n = ( v ) => Debug . humanize ( v )
1315
1416export class ConnectionError extends Error {
15- constructor ( err , ...args ) {
17+ reason ?: Todo
18+
19+ constructor ( err : Todo , ...args : Todo [ ] ) {
1620 if ( err instanceof ConnectionError ) {
1721 return err
1822 }
1923
2024 if ( err && err . stack ) {
2125 const { message, stack } = err
26+ // @ts -expect-error
2227 super ( message , ...args )
2328 Object . assign ( this , err )
2429 this . stack = stack
2530 this . reason = err
2631 } else {
32+ // @ts -expect-error
2733 super ( err , ...args )
2834 if ( Error . captureStackTrace ) {
2935 Error . captureStackTrace ( this , this . constructor )
@@ -35,7 +41,7 @@ export class ConnectionError extends Error {
3541const openSockets = new Set ( )
3642const FORCE_CLOSED = Symbol ( 'FORCE_CLOSED' )
3743
38- async function OpenWebSocket ( url , opts , ...args ) {
44+ async function OpenWebSocket ( url : string , opts : Todo , ...args : Todo [ ] ) {
3945 return new Promise ( ( resolve , reject ) => {
4046 try {
4147 if ( ! url ) {
@@ -44,8 +50,9 @@ async function OpenWebSocket(url, opts, ...args) {
4450 throw err
4551 }
4652
53+ // @ts -expect-error
4754 const socket = process . browser ? new WebSocket ( url ) : new WebSocket ( url , opts , ...args )
48- let error
55+ let error : Todo
4956 Object . assign ( socket , {
5057 id : counterId ( 'ws' ) ,
5158 binaryType : 'arraybuffer' ,
@@ -57,31 +64,34 @@ async function OpenWebSocket(url, opts, ...args) {
5764 openSockets . delete ( socket )
5865 reject ( new ConnectionError ( error || 'socket closed' ) )
5966 } ,
60- onerror ( event ) {
67+ onerror ( event : Todo ) {
6168 error = new ConnectionError ( event . error || event )
6269 } ,
6370 } )
6471
6572 // attach debug
73+ // @ts -expect-error
6674 socket . debug = opts . debug . extend ( socket . id )
75+ // @ts -expect-error
6776 socket . debug . color = opts . debug . color // use existing colour
6877 } catch ( err ) {
6978 reject ( err )
7079 }
7180 } )
7281}
7382
74- async function CloseWebSocket ( socket ) {
83+ async function CloseWebSocket ( socket : Todo ) {
7584 return new Promise ( ( resolve , reject ) => {
7685 if ( ! socket || socket . readyState === WebSocket . CLOSED ) {
77- resolve ( )
86+ resolve ( undefined )
7887 return
7988 }
8089
8190 const waitThenClose = ( ) => (
8291 resolve ( CloseWebSocket ( socket ) )
8392 )
8493
94+ // @ts -expect-error
8595 if ( socket . readyState === WebSocket . OPENING ) {
8696 socket . addEventListener ( 'error' , waitThenClose )
8797 socket . addEventListener ( 'open' , waitThenClose )
@@ -110,17 +120,17 @@ const STATE = {
110120}
111121
112122/* eslint-disable no-underscore-dangle, no-param-reassign */
113- function SocketConnector ( connection ) {
114- let next
115- let socket
123+ function SocketConnector ( connection : Todo ) {
124+ let next : Todo
125+ let socket : Todo
116126 let startedConnecting = false
117127 let didCloseUnexpectedly = false
118128
119129 const onClose = ( ) => {
120130 didCloseUnexpectedly = true
121131 if ( ! next . pendingCount && ! next . activeCount ) {
122132 // if no pending actions run next & emit any errors
123- next ( ) . catch ( ( err ) => {
133+ next ( ) . catch ( ( err : Todo ) => {
124134 connection . emit ( 'error' , err )
125135 } )
126136 }
@@ -207,7 +217,7 @@ function SocketConnector(connection) {
207217 } ,
208218 // attach message handler
209219 ( ) => {
210- const onMessage = ( messageEvent , ...args ) => {
220+ const onMessage = ( messageEvent : Todo , ...args : Todo | [ ] ) => {
211221 connection . emit ( 'message' , messageEvent , ...args )
212222 }
213223 socket . addEventListener ( 'message' , onMessage )
@@ -275,22 +285,36 @@ const DEFAULT_MAX_RETRIES = 10
275285 */
276286
277287export default class Connection extends EventEmitter {
288+
289+ _debug : Todo
290+ options : Todo
291+ retryCount : Todo
292+ wantsState : Todo
293+ connectionHandles : Todo
294+ step : Todo
295+ socket ?: Todo
296+ didDisableAutoConnect ?: Todo
297+ isWaiting ?: Todo
298+ _isReconnecting : Todo
299+ _backoffTimeout : Todo
300+ sendID : Todo
301+
278302 static getOpen ( ) {
279303 return openSockets . size
280304 }
281305
282306 static async closeOpen ( ) {
283- return Promise . all ( [ ...openSockets ] . map ( async ( socket ) => {
307+ return Promise . all ( [ ...openSockets ] . map ( async ( socket : Todo ) => {
284308 socket [ FORCE_CLOSED ] = true // eslint-disable-line no-param-reassign
285309 return CloseWebSocket ( socket ) . catch ( ( err ) => {
286310 socket . debug ( err ) // ignore error
287311 } )
288312 } ) )
289313 }
290314
291- constructor ( options = { } , client ) {
315+ constructor ( options = { } , debug ?: Debug . Debugger ) {
292316 super ( )
293- this . _debug = client . debug . extend ( counterId ( this . constructor . name ) )
317+ this . _debug = ( debug !== undefined ) ? debug . extend ( counterId ( this . constructor . name ) ) : Debug ( `StreamrClient:: ${ counterId ( this . constructor . name ) } ` )
294318
295319 this . options = options
296320 this . options . autoConnect = ! ! this . options . autoConnect
@@ -303,19 +327,20 @@ export default class Connection extends EventEmitter {
303327 this . backoffWait = pLimitFn ( this . backoffWait . bind ( this ) )
304328 this . step = SocketConnector ( this )
305329 this . debug = this . debug . bind ( this )
330+ // @ts -expect-error
306331 this . maybeConnect = pOne ( this . maybeConnect . bind ( this ) )
307332 this . nextConnection = pOne ( this . nextConnection . bind ( this ) )
308333 this . nextDisconnection = pOne ( this . nextDisconnection . bind ( this ) )
309334 }
310335
311- debug ( ...args ) {
336+ debug ( ...args : Todo [ ] ) {
312337 if ( this . socket ) {
313338 return this . socket . debug ( ...args )
314339 }
315340 return this . _debug ( ...args )
316341 }
317342
318- emit ( event , ...args ) {
343+ emit ( event : Todo , ...args : Todo [ ] ) {
319344 if ( event === 'error' ) {
320345 let [ err ] = args
321346 const [ , ...rest ] = args
@@ -342,7 +367,7 @@ export default class Connection extends EventEmitter {
342367 return result
343368 }
344369
345- emitTransition ( event , ...args ) {
370+ emitTransition ( event : Todo , ...args : Todo [ ] ) {
346371 const prevWantsState = this . wantsState
347372 if ( prevWantsState === STATE . AUTO ) {
348373 return this . emit ( event , ...args )
@@ -426,25 +451,25 @@ export default class Connection extends EventEmitter {
426451
427452 this . isWaiting = true
428453 return new Promise ( ( resolve , reject ) => {
429- let onError
430- let onDone
454+ let onError : Todo
455+ let onDone : Todo
431456 const onConnected = ( ) => {
432457 this . off ( 'done' , onDone )
433458 this . off ( 'error' , onError )
434459 this . off ( '_error' , onError )
435- resolve ( )
460+ resolve ( undefined )
436461 }
437- onDone = ( err ) => {
462+ onDone = ( err : Todo ) => {
438463 this . off ( 'error' , onError )
439464 this . off ( '_error' , onError )
440465 this . off ( 'connected' , onConnected )
441466 if ( err ) {
442467 reject ( err )
443468 } else {
444- resolve ( )
469+ resolve ( undefined )
445470 }
446471 }
447- onError = ( err ) => {
472+ onError = ( err : Todo ) => {
448473 this . off ( 'done' , onDone )
449474 this . off ( 'connected' , onConnected )
450475 reject ( err )
@@ -504,7 +529,7 @@ export default class Connection extends EventEmitter {
504529 await this . step ( )
505530 }
506531
507- async needsConnection ( msg ) {
532+ async needsConnection ( msg ?: Todo ) {
508533 await this . maybeConnect ( )
509534 if ( ! this . isConnected ( ) ) {
510535 const { autoConnect, autoDisconnect } = this . options
@@ -550,12 +575,12 @@ export default class Connection extends EventEmitter {
550575 }
551576
552577 return new Promise ( ( resolve , reject ) => {
553- let onError
578+ let onError : Todo
554579 const onDisconnected = ( ) => {
555580 this . off ( 'error' , onError )
556- resolve ( )
581+ resolve ( undefined )
557582 }
558- onError = ( err ) => {
583+ onError = ( err : Todo ) => {
559584 this . off ( 'disconnected' , onDisconnected )
560585 reject ( err )
561586 }
@@ -576,7 +601,7 @@ export default class Connection extends EventEmitter {
576601 debug ( 'waiting %n' , timeout )
577602 this . _backoffTimeout = setTimeout ( ( ) => {
578603 debug ( 'waited %n' , timeout )
579- resolve ( )
604+ resolve ( undefined )
580605 } , timeout )
581606 } )
582607 }
@@ -585,7 +610,7 @@ export default class Connection extends EventEmitter {
585610 * Auto Connect/Disconnect counters.
586611 */
587612
588- async addHandle ( id ) {
613+ async addHandle ( id : Todo ) {
589614 if (
590615 this . connectionHandles . has ( id )
591616 && this . isConnected ( )
@@ -602,7 +627,7 @@ export default class Connection extends EventEmitter {
602627 * When no more handles and autoDisconnect is true, disconnect.
603628 */
604629
605- async removeHandle ( id ) {
630+ async removeHandle ( id : Todo ) {
606631 const hadConnection = this . connectionHandles . has ( id )
607632 this . connectionHandles . delete ( id )
608633 if ( hadConnection && this . _couldAutoDisconnect ( ) ) {
@@ -619,7 +644,7 @@ export default class Connection extends EventEmitter {
619644 )
620645 }
621646
622- async send ( msg ) {
647+ async send ( msg : Todo ) {
623648 this . sendID = this . sendID + 1 || 1
624649 const handle = `send${ this . sendID } `
625650 this . debug ( '(%s) send()' , this . getState ( ) )
@@ -638,19 +663,20 @@ export default class Connection extends EventEmitter {
638663 }
639664 }
640665
641- async _send ( msg ) {
666+ async _send ( msg : Todo ) {
642667 return new Promise ( ( resolve , reject ) => {
643668 this . debug ( '(%s) >> %o' , this . getState ( ) , msg )
644669 // promisify send
645670 const data = typeof msg . serialize === 'function' ? msg . serialize ( ) : msg
646671 // send callback doesn't exist with browser websockets, just resolve
647672 /* istanbul ignore next */
648673 this . emit ( '_send' , msg ) // for informational purposes
674+ // @ts -expect-error
649675 if ( process . browser ) {
650676 this . socket . send ( data )
651677 resolve ( data )
652678 } else {
653- this . socket . send ( data , ( err ) => {
679+ this . socket . send ( data , ( err : Todo ) => {
654680 /* istanbul ignore next */
655681 if ( err ) {
656682 reject ( new ConnectionError ( err ) )
@@ -727,9 +753,10 @@ export default class Connection extends EventEmitter {
727753 onDisconnecting = ( ) => { } ,
728754 onDisconnected = ( ) => { } ,
729755 onDone = ( ) => { } ,
756+ // @ts -expect-error
730757 onError,
731758 } ) {
732- let onDoneHandler
759+ let onDoneHandler : Todo
733760 const cleanUp = async ( ) => {
734761 this
735762 . off ( 'connecting' , onConnecting )
@@ -742,8 +769,10 @@ export default class Connection extends EventEmitter {
742769 }
743770 }
744771
745- onDoneHandler = async ( ...args ) => {
772+ onDoneHandler = async ( ...args : Todo [ ] ) => {
773+ // @ts -expect-error
746774 cleanUp ( ...args )
775+ // @ts -expect-error
747776 return onDone ( ...args )
748777 }
749778
@@ -762,4 +791,5 @@ export default class Connection extends EventEmitter {
762791 }
763792}
764793
794+ // @ts -expect-error
765795Connection . ConnectionError = ConnectionError
0 commit comments