@@ -20,43 +20,47 @@ const PREVIEW_CHANNEL = 'preview-updates';
2020export class PreviewsStore {
2121 #availablePreviews = new Map < number , PreviewInfo > ( ) ;
2222 #webcontainer: Promise < WebContainer > ;
23- #broadcastChannel: BroadcastChannel ;
23+ #broadcastChannel? : BroadcastChannel ;
2424 #lastUpdate = new Map < string , number > ( ) ;
2525 #watchedFiles = new Set < string > ( ) ;
2626 #refreshTimeouts = new Map < string , NodeJS . Timeout > ( ) ;
2727 #REFRESH_DELAY = 300 ;
28- #storageChannel: BroadcastChannel ;
28+ #storageChannel? : BroadcastChannel ;
2929
3030 previews = atom < PreviewInfo [ ] > ( [ ] ) ;
3131
3232 constructor ( webcontainerPromise : Promise < WebContainer > ) {
3333 this . #webcontainer = webcontainerPromise ;
34- this . #broadcastChannel = new BroadcastChannel ( PREVIEW_CHANNEL ) ;
35- this . #storageChannel = new BroadcastChannel ( 'storage-sync-channel' ) ;
36-
37- // Listen for preview updates from other tabs
38- this . #broadcastChannel. onmessage = ( event ) => {
39- const { type, previewId } = event . data ;
40-
41- if ( type === 'file-change' ) {
42- const timestamp = event . data . timestamp ;
43- const lastUpdate = this . #lastUpdate. get ( previewId ) || 0 ;
44-
45- if ( timestamp > lastUpdate ) {
46- this . #lastUpdate. set ( previewId , timestamp ) ;
47- this . refreshPreview ( previewId ) ;
34+ this . #broadcastChannel = this . #maybeCreateChannel( PREVIEW_CHANNEL ) ;
35+ this . #storageChannel = this . #maybeCreateChannel( 'storage-sync-channel' ) ;
36+
37+ if ( this . #broadcastChannel) {
38+ // Listen for preview updates from other tabs
39+ this . #broadcastChannel. onmessage = ( event ) => {
40+ const { type, previewId } = event . data ;
41+
42+ if ( type === 'file-change' ) {
43+ const timestamp = event . data . timestamp ;
44+ const lastUpdate = this . #lastUpdate. get ( previewId ) || 0 ;
45+
46+ if ( timestamp > lastUpdate ) {
47+ this . #lastUpdate. set ( previewId , timestamp ) ;
48+ this . refreshPreview ( previewId ) ;
49+ }
4850 }
49- }
50- } ;
51+ } ;
52+ }
5153
52- // Listen for storage sync messages
53- this . #storageChannel. onmessage = ( event ) => {
54- const { storage, source } = event . data ;
54+ if ( this . #storageChannel) {
55+ // Listen for storage sync messages
56+ this . #storageChannel. onmessage = ( event ) => {
57+ const { storage, source } = event . data ;
5558
56- if ( storage && source !== this . _getTabId ( ) ) {
57- this . _syncStorage ( storage ) ;
58- }
59- } ;
59+ if ( storage && source !== this . _getTabId ( ) ) {
60+ this . _syncStorage ( storage ) ;
61+ }
62+ } ;
63+ }
6064
6165 // Override localStorage setItem to catch all changes
6266 if ( typeof window !== 'undefined' ) {
@@ -71,6 +75,29 @@ export class PreviewsStore {
7175 this . #init( ) ;
7276 }
7377
78+ #maybeCreateChannel( name : string ) : BroadcastChannel | undefined {
79+ if ( typeof globalThis === 'undefined' ) {
80+ return undefined ;
81+ }
82+
83+ const globalBroadcastChannel = (
84+ globalThis as typeof globalThis & {
85+ BroadcastChannel ?: typeof BroadcastChannel ;
86+ }
87+ ) . BroadcastChannel ;
88+
89+ if ( typeof globalBroadcastChannel !== 'function' ) {
90+ return undefined ;
91+ }
92+
93+ try {
94+ return new globalBroadcastChannel ( name ) ;
95+ } catch ( error ) {
96+ console . warn ( '[Preview] BroadcastChannel unavailable:' , error ) ;
97+ return undefined ;
98+ }
99+ }
100+
74101 // Generate a unique ID for this tab
75102 private _getTabId ( ) : string {
76103 if ( typeof window !== 'undefined' ) {
@@ -130,7 +157,7 @@ export class PreviewsStore {
130157 }
131158 }
132159
133- this . #storageChannel. postMessage ( {
160+ this . #storageChannel? .postMessage ( {
134161 type : 'storage-sync' ,
135162 storage,
136163 source : this . _getTabId ( ) ,
@@ -192,7 +219,7 @@ export class PreviewsStore {
192219 const timestamp = Date . now ( ) ;
193220 this . #lastUpdate. set ( previewId , timestamp ) ;
194221
195- this . #broadcastChannel. postMessage ( {
222+ this . #broadcastChannel? .postMessage ( {
196223 type : 'state-change' ,
197224 previewId,
198225 timestamp,
@@ -204,7 +231,7 @@ export class PreviewsStore {
204231 const timestamp = Date . now ( ) ;
205232 this . #lastUpdate. set ( previewId , timestamp ) ;
206233
207- this . #broadcastChannel. postMessage ( {
234+ this . #broadcastChannel? .postMessage ( {
208235 type : 'file-change' ,
209236 previewId,
210237 timestamp,
@@ -219,7 +246,7 @@ export class PreviewsStore {
219246 const timestamp = Date . now ( ) ;
220247 this . #lastUpdate. set ( previewId , timestamp ) ;
221248
222- this . #broadcastChannel. postMessage ( {
249+ this . #broadcastChannel? .postMessage ( {
223250 type : 'file-change' ,
224251 previewId,
225252 timestamp,
0 commit comments