9
9
type ServerMessage ,
10
10
} from './FluxMessages.js' ;
11
11
12
- let atmosphere : Atmosphere . Atmosphere ;
13
-
14
12
export enum State {
15
13
ACTIVE = 'active' ,
16
14
INACTIVE = 'inactive' ,
@@ -73,6 +71,15 @@ type EndpointInfo = {
73
71
reconnect ?( ) : ActionOnLostSubscription | void ;
74
72
} ;
75
73
74
+ const initAtmosphere = async ( ) => {
75
+ if ( ! import . meta. env . VITE_SW_CONTEXT ) {
76
+ return await import ( 'atmosphere.js' ) . then ( ( module ) => module . default ) ;
77
+ }
78
+ return undefined ;
79
+ } ;
80
+
81
+ const atmosphere : Atmosphere . Atmosphere | undefined = await initAtmosphere ( ) ;
82
+
76
83
/**
77
84
* A representation of the underlying persistent network connection used for subscribing to Flux type endpoint methods.
78
85
*/
@@ -91,17 +98,7 @@ export class FluxConnection extends EventTarget {
91
98
92
99
constructor ( connectPrefix : string , atmosphereOptions ?: Partial < Atmosphere . Request > ) {
93
100
super ( ) ;
94
- if ( ! import . meta. env [ 'VITE_SW_CONTEXT' ] ) {
95
- import ( 'atmosphere.js' )
96
- . then ( ( module ) => {
97
- atmosphere = module . default ;
98
- this . #connectWebsocket( connectPrefix . replace ( / c o n n e c t $ / u, '' ) , atmosphereOptions ?? { } ) ;
99
- } )
100
- . catch ( ( error ) => {
101
- // eslint-disable-next-line no-console
102
- console . error ( 'Failed to load atmosphere' , error ) ;
103
- } ) ;
104
- }
101
+ this . #connectWebsocket( connectPrefix . replace ( / c o n n e c t $ / u, '' ) , atmosphereOptions ?? { } ) ;
105
102
}
106
103
107
104
#resubscribeIfWasClosed( ) {
@@ -198,7 +195,7 @@ export class FluxConnection extends EventTarget {
198
195
const extraHeaders = self . document ? getCsrfTokenHeadersForEndpointRequest ( self . document ) : { } ;
199
196
const pushUrl = 'HILLA/push' ;
200
197
const url = prefix . length === 0 ? pushUrl : ( prefix . endsWith ( '/' ) ? prefix : `${ prefix } /` ) + pushUrl ;
201
- this . #socket = atmosphere . subscribe ?.( {
198
+ this . #socket = atmosphere ? .subscribe ?.( {
202
199
contentType : 'application/json; charset=UTF-8' ,
203
200
enableProtocol : true ,
204
201
transport : 'websocket' ,
0 commit comments