@@ -52,7 +52,6 @@ export interface ConnectionStateErrored extends ConnectionState {
5252}
5353
5454export type AnyConnectionState =
55- | ConnectionState
5655 | ConnectionStateConnected
5756 | ConnectionStateConnecting
5857 | ConnectionStateDisconnected
@@ -77,7 +76,7 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
7776 async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
7877 this . emit ( "connection-requested" , this . state ) ;
7978
80- if ( this . state . tag == "connected" || this . state . tag == "connecting" ) {
79+ if ( this . state . tag === "connected" || this . state . tag = == "connecting" ) {
8180 await this . disconnect ( ) ;
8281 }
8382
@@ -126,18 +125,13 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
126125 }
127126
128127 async disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > {
129- if ( this . state . tag == "disconnected" ) {
130- return this . state as ConnectionStateDisconnected ;
131- }
132-
133- if ( this . state . tag == "errored" ) {
134- return this . state as ConnectionStateErrored ;
128+ if ( this . state . tag === "disconnected" || this . state . tag === "errored" ) {
129+ return this . state ;
135130 }
136131
137132 if ( this . state . tag == "connected" || this . state . tag == "connecting" ) {
138- const state = this . state as ConnectionStateConnecting | ConnectionStateConnected ;
139133 try {
140- await state . serviceProvider ?. close ( true ) ;
134+ await this . state . serviceProvider ?. close ( true ) ;
141135 } finally {
142136 this . changeState ( "connection-closed" , { tag : "disconnected" } ) ;
143137 }
@@ -150,9 +144,14 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
150144 return this . state ;
151145 }
152146
153- changeState < State extends AnyConnectionState > ( event : keyof ConnectionManagerEvents , newState : State ) : State {
147+ changeState < Event extends keyof ConnectionManagerEvents , State extends ConnectionManagerEvents [ Event ] [ 0 ] > (
148+ event : Event ,
149+ newState : State
150+ ) : State {
154151 this . state = newState ;
155- this . emit ( event , newState ) ;
152+ // TypeScript doesn't seem to be happy with the spread operator and generics
153+ // eslint-disable-next-line
154+ this . emit ( event , ...( [ newState ] as any ) ) ;
156155 return newState ;
157156 }
158157
0 commit comments