@@ -52,7 +52,6 @@ export interface ConnectionStateErrored extends ConnectionState {
52
52
}
53
53
54
54
export type AnyConnectionState =
55
- | ConnectionState
56
55
| ConnectionStateConnected
57
56
| ConnectionStateConnecting
58
57
| ConnectionStateDisconnected
@@ -77,7 +76,7 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
77
76
async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
78
77
this . emit ( "connection-requested" , this . state ) ;
79
78
80
- if ( this . state . tag == "connected" || this . state . tag == "connecting" ) {
79
+ if ( this . state . tag === "connected" || this . state . tag = == "connecting" ) {
81
80
await this . disconnect ( ) ;
82
81
}
83
82
@@ -126,18 +125,13 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
126
125
}
127
126
128
127
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 ;
135
130
}
136
131
137
132
if ( this . state . tag == "connected" || this . state . tag == "connecting" ) {
138
- const state = this . state as ConnectionStateConnecting | ConnectionStateConnected ;
139
133
try {
140
- await state . serviceProvider ?. close ( true ) ;
134
+ await this . state . serviceProvider ?. close ( true ) ;
141
135
} finally {
142
136
this . changeState ( "connection-closed" , { tag : "disconnected" } ) ;
143
137
}
@@ -150,9 +144,14 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
150
144
return this . state ;
151
145
}
152
146
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 {
154
151
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 ) ) ;
156
155
return newState ;
157
156
}
158
157
0 commit comments