1
1
import type { ReactiveControllerHost } from '@lit/reactive-element' ;
2
- import atmosphere from 'atmosphere.js' ;
2
+
3
+ import type Atmosphere from 'atmosphere.js' ;
4
+
3
5
import type { Subscription } from './Connect.js' ;
4
6
import { getCsrfTokenHeadersForEndpointRequest } from './CsrfUtils.js' ;
5
7
import {
@@ -9,6 +11,8 @@ import {
9
11
type ServerMessage ,
10
12
} from './FluxMessages.js' ;
11
13
14
+ let atmosphere : Atmosphere . Atmosphere ;
15
+
12
16
export enum State {
13
17
ACTIVE = 'active' ,
14
18
INACTIVE = 'inactive' ,
@@ -71,6 +75,14 @@ type EndpointInfo = {
71
75
reconnect ?( ) : ActionOnLostSubscription | void ;
72
76
} ;
73
77
78
+ interface ImportMetaEnv {
79
+ readonly SW_CONTEXT : boolean ;
80
+ }
81
+
82
+ interface ImportMeta {
83
+ readonly env : ImportMetaEnv ;
84
+ }
85
+
74
86
/**
75
87
* A representation of the underlying persistent network connection used for subscribing to Flux type endpoint methods.
76
88
*/
@@ -89,7 +101,17 @@ export class FluxConnection extends EventTarget {
89
101
90
102
constructor ( connectPrefix : string , atmosphereOptions ?: Partial < Atmosphere . Request > ) {
91
103
super ( ) ;
92
- this . #connectWebsocket( connectPrefix . replace ( / c o n n e c t $ / u, '' ) , atmosphereOptions ?? { } ) ;
104
+ // @ts -expect-error - vite environment variable
105
+ const meta : ImportMeta = import . meta;
106
+ if ( ! meta . env . SW_CONTEXT ) {
107
+ ( async ( ) => {
108
+ atmosphere = await import ( 'atmosphere.js' ) ;
109
+ this . #connectWebsocket( connectPrefix . replace ( / c o n n e c t $ / u, '' ) , atmosphereOptions ?? { } ) ;
110
+ } ) ( ) . catch ( ( e ) => {
111
+ // eslint-disable-next-line no-console
112
+ console . error ( 'Failed to load atmosphere.js' , e ) ;
113
+ } ) ;
114
+ }
93
115
}
94
116
95
117
#resubscribeIfWasClosed( ) {
0 commit comments