@@ -15,6 +15,9 @@ import { authenticate, fetchWebSocket, startWebsocket } from './websockets.js';
15
15
/** Function called when a resource is updated or removed */
16
16
type Callback = ( resource : Resource ) => void ;
17
17
18
+ /** Returns True if the client has WebSocket support */
19
+ const supportsWebSockets = ( ) => typeof WebSocket !== 'undefined' ;
20
+
18
21
export enum StoreEvents {
19
22
/**
20
23
* Whenever `Resource.save()` is called, so only when the user of this library
@@ -167,7 +170,11 @@ export class Store {
167
170
// Use WebSocket if available, else use HTTP(S)
168
171
const ws = this . getWebSocketForSubject ( subject ) ;
169
172
170
- if ( ! opts . noWebSocket && WebSocket && ws ?. readyState === WebSocket . OPEN ) {
173
+ if (
174
+ ! opts . noWebSocket &&
175
+ supportsWebSockets ( ) &&
176
+ ws ?. readyState === WebSocket . OPEN
177
+ ) {
171
178
fetchWebSocket ( ws , subject ) ;
172
179
} else {
173
180
fetchResource (
@@ -433,13 +440,13 @@ export class Store {
433
440
434
441
this . serverUrl = url ;
435
442
// TODO This is not the right place
436
- this . openWebSocket ( url ) ;
443
+ supportsWebSockets ( ) && this . openWebSocket ( url ) ;
437
444
}
438
445
439
446
/** Opens a WebSocket for this Atomic Server URL */
440
447
public openWebSocket ( url : string ) {
441
448
// Check if we're running in a webbrowser
442
- if ( typeof window !== 'undefined' ) {
449
+ if ( supportsWebSockets ( ) ) {
443
450
if ( this . webSockets . has ( url ) ) {
444
451
return ;
445
452
}
0 commit comments