@@ -59,25 +59,25 @@ export class TpyWs {
59
59
constructor (
60
60
tpyInstance : Tpy ,
61
61
deploymentID : string ,
62
- reconnectionTimeout = 250 ,
62
+ reconnectionTimeout = 250
63
63
) {
64
64
if ( ! tpyInstance || ! ( tpyInstance instanceof Tpy ) ) {
65
65
throw new TpyError (
66
66
"Missing or Invalid Required Parameter" ,
67
67
parametersPrompt (
68
68
! tpyInstance ? "missing" : "incompatible" ,
69
- "tpyInstance" ,
69
+ "tpyInstance"
70
70
) ,
71
71
"tpyInstance" ,
72
- tpyInstance ,
72
+ tpyInstance
73
73
) ;
74
74
}
75
75
if ( ! deploymentID ) {
76
76
throw new TpyError (
77
77
"Missing or Invalid Required Parameter" ,
78
78
parametersPrompt ( "missing" , "deploymentID" ) ,
79
79
"deploymentID" ,
80
- deploymentID ,
80
+ deploymentID
81
81
) ;
82
82
}
83
83
this . tpyClient = tpyInstance ;
@@ -124,20 +124,20 @@ export class TpyWs {
124
124
*/
125
125
on < T extends unknown [ ] > (
126
126
type : "message" ,
127
- callback : ( data : Unpacked < PylonWebSocket . Response < T > > ) => void ,
127
+ callback : ( data : Unpacked < PylonWebSocket . Response < T > > ) => void
128
128
) : EventEmitter ;
129
129
on < T extends unknown [ ] > ( type : messageTypes , callback : unknown ) {
130
130
if ( typeof callback != "function" ) {
131
131
throw new TpyError (
132
132
"Missing or Invalid Required Parameter" ,
133
133
parametersPrompt ( "incompatible" , "callback" ) ,
134
134
"callback" ,
135
- typeof callback ,
135
+ typeof callback
136
136
) ;
137
137
}
138
138
return this . eventEmitter . on (
139
139
type ,
140
- < ( data : Unpacked < PylonWebSocket . Response < T > > ) => void > callback ,
140
+ < ( data : Unpacked < PylonWebSocket . Response < T > > ) => void > callback
141
141
) ;
142
142
}
143
143
@@ -147,38 +147,46 @@ export class TpyWs {
147
147
async connect ( ) {
148
148
if ( ! this . tryToConnect ) return ;
149
149
this . _websocket = new WebSocket (
150
- ( await this . tpyClient . getDeployment ( this . deploymentID ) ) . workbench_url ,
150
+ ( await this . tpyClient . getDeployment ( this . deploymentID ) ) . workbench_url
151
151
) ;
152
- this . _websocket . onopen = this . onOpen . bind ( this ) ;
153
- this . _websocket . onclose = this . onClose . bind ( this ) ;
154
- this . _websocket . onerror = this . onError . bind ( this ) ;
155
- this . _websocket . onmessage = this . onMessage . bind ( this ) ;
156
- }
157
152
158
- private onOpen ( event : Event ) {
159
- this . eventEmitter . emit ( "open" , event ) ;
160
- }
153
+ this . _websocket . onopen = ( ( event : Event ) => {
154
+ this . eventEmitter . emit ( "open" , event ) ;
155
+ } ) . bind ( this ) ;
161
156
162
- private onError ( event : Event | ErrorEvent ) {
163
- if ( ! this . websocket ) return ;
164
- this . eventEmitter . emit ( "error" , event ) ;
157
+ this . _websocket . onclose = ( ( event : CloseEvent ) => {
158
+ this . eventEmitter . emit ( "close" , event ) ;
159
+ this . timedReconnect ( ) ;
165
160
166
- try {
167
- this . websocket . close ( ) ;
168
- // deno-lint-ignore no-empty
169
- } catch { }
161
+ // (async () => await this.timedReconnect())();
162
+ } ) . bind ( this ) ;
170
163
171
- setTimeout ( ( ) => {
172
- this . connect ( ) ;
173
- } , this . reconnectionTimout ) ;
174
- }
164
+ this . _websocket . onerror = ( ( event : Event | ErrorEvent ) => {
165
+ if ( ! this . websocket ) return ;
166
+ this . eventEmitter . emit ( "error" , event ) ;
167
+
168
+ try {
169
+ this . websocket . close ( ) ;
170
+ // deno-lint-ignore no-empty
171
+ } catch { }
172
+ this . timedReconnect ( ) ;
173
+ // (async () => await this.timedReconnect())();
174
+ } ) . bind ( this ) ;
175
175
176
- private onClose ( event : CloseEvent ) {
177
- this . eventEmitter . emit ( "close" , event ) ;
176
+ this . _websocket . onmessage = ( ( event : MessageEvent ) => {
177
+ this . eventEmitter . emit ( "message" , JSON . parse ( String ( event . data ) ) [ 0 ] ) ;
178
+ } ) . bind ( this ) ;
178
179
}
179
180
180
- private onMessage ( event : MessageEvent ) {
181
- this . eventEmitter . emit ( "message" , JSON . parse ( String ( event . data ) ) [ 0 ] ) ;
181
+ timedReconnect ( ) {
182
+ // Check once to spare resources
183
+ if ( ! this . tryToConnect ) return ;
184
+ setTimeout ( ( ) => {
185
+ ( async ( ) => {
186
+ // Check again to ensure it hasn't changed
187
+ if ( this . tryToConnect ) await this . connect ( ) ;
188
+ } ) ( ) ;
189
+ } , this . reconnectionTimout ) ;
182
190
}
183
191
184
192
/**
0 commit comments