@@ -21,15 +21,27 @@ export interface HttpResponse {
21
21
headers ?: Headers
22
22
}
23
23
24
+ /**
25
+ * A listener that is called when the connection status is changed.
26
+ * The status 'true' indicates that the server was last reached successfully.
27
+ * The status 'false' indicates that the server could not be reached or
28
+ * returned a status code indicating that it cannot service requests (>= 502).
29
+ */
30
+ export type ConnectionStatusListener = ( ( status : boolean ) => void ) ;
31
+
24
32
/**
25
33
* Rest client helper to make request.
26
34
* The request response status code indicates if the request is successful.
27
35
* The json object in the response may be undefined when an error occurs.
28
36
*/
29
37
export class RestClient {
30
38
39
+ static status = false ;
40
+ static connectionStatusListeners : ConnectionStatusListener [ ] = [ ] ;
41
+
31
42
static get < T > ( url : string , parameters ?: Map < string , string > ) : Promise < TspClientResponse < Deserialized < T > > > ;
32
43
static get < T > ( url : string , parameters : Map < string , string > | undefined , normalizer : Normalizer < T > ) : Promise < TspClientResponse < T > > ;
44
+
33
45
/**
34
46
* Perform GET
35
47
* @template T is the expected type of the json object returned by this request
@@ -103,7 +115,10 @@ export class RestClient {
103
115
} ,
104
116
body : this . jsonStringify ( body )
105
117
} ) ;
118
+ const status = response . status <= 501 ;
119
+ this . updateConnectionStatus ( status ) ;
106
120
} catch ( err ) {
121
+ this . updateConnectionStatus ( false ) ;
107
122
return new TspClientResponse ( err . toString ( ) , 503 , 'Service Unavailable' ) ;
108
123
}
109
124
@@ -148,7 +163,7 @@ export class RestClient {
148
163
/**
149
164
* Stringify JS objects. Can stringify `BigInt` values.
150
165
*/
151
- protected static jsonStringify ( data : any ) : string {
166
+ protected static jsonStringify ( data : any ) : string {
152
167
return JSONBig . stringify ( data ) ;
153
168
}
154
169
@@ -159,4 +174,36 @@ export class RestClient {
159
174
protected static jsonParse ( text : string ) : any {
160
175
return JSONBig . parse ( text ) ;
161
176
}
177
+
178
+ /**
179
+ * Adds a connection status listener.
180
+ * The listener will immediately be called with the current status.
181
+ *
182
+ * @param listener The listener to add
183
+ */
184
+ public static addConnectionStatusListener ( listener : ConnectionStatusListener ) : void {
185
+ if ( ! this . connectionStatusListeners . includes ( listener ) ) {
186
+ this . connectionStatusListeners . push ( listener ) ;
187
+ }
188
+ listener ( this . status ) ;
189
+ }
190
+
191
+ /**
192
+ * Removes a connection status listener.
193
+ *
194
+ * @param listener The listener to remove
195
+ */
196
+ public static removeConnectionStatusListener ( listener : ConnectionStatusListener ) : void {
197
+ this . connectionStatusListeners = this . connectionStatusListeners . filter ( element => element !== listener ) ;
198
+ }
199
+
200
+ private static updateConnectionStatus ( status : boolean ) {
201
+ if ( this . status === status ) {
202
+ return ;
203
+ }
204
+ for ( const listener of this . connectionStatusListeners ) {
205
+ listener ( status ) ;
206
+ }
207
+ this . status = status ;
208
+ }
162
209
}
0 commit comments