@@ -15,6 +15,9 @@ import { authenticate, fetchWebSocket, startWebsocket } from './websockets.js';
1515/** Function called when a resource is updated or removed */
1616type Callback = ( resource : Resource ) => void ;
1717
18+ /** Returns True if the client has WebSocket support */
19+ const supportsWebSockets = ( ) => typeof WebSocket !== 'undefined' ;
20+
1821export enum StoreEvents {
1922 /**
2023 * Whenever `Resource.save()` is called, so only when the user of this library
@@ -167,7 +170,11 @@ export class Store {
167170 // Use WebSocket if available, else use HTTP(S)
168171 const ws = this . getWebSocketForSubject ( subject ) ;
169172
170- if ( ! opts . noWebSocket && WebSocket && ws ?. readyState === WebSocket . OPEN ) {
173+ if (
174+ ! opts . noWebSocket &&
175+ supportsWebSockets ( ) &&
176+ ws ?. readyState === WebSocket . OPEN
177+ ) {
171178 fetchWebSocket ( ws , subject ) ;
172179 } else {
173180 fetchResource (
@@ -433,13 +440,13 @@ export class Store {
433440
434441 this . serverUrl = url ;
435442 // TODO This is not the right place
436- this . openWebSocket ( url ) ;
443+ supportsWebSockets ( ) && this . openWebSocket ( url ) ;
437444 }
438445
439446 /** Opens a WebSocket for this Atomic Server URL */
440447 public openWebSocket ( url : string ) {
441448 // Check if we're running in a webbrowser
442- if ( typeof window !== 'undefined' ) {
449+ if ( supportsWebSockets ( ) ) {
443450 if ( this . webSockets . has ( url ) ) {
444451 return ;
445452 }
0 commit comments