11using Microsoft . AspNetCore . Components ;
2- using Microsoft . AspNetCore . Components . WebAssembly . Hosting ;
32using Microsoft . Extensions . DependencyInjection ;
43using SpawnDev . BlazorJS . JSObjects ;
54using SpawnDev . BlazorJS . Toolbox ;
@@ -60,6 +59,11 @@ public class WebWorkerService : IDisposable, IAsyncBackgroundService
6059 /// </summary>
6160 public bool InterConnectEnabled { get ; set ; } = true ;
6261 /// <summary>
62+ /// If true, the faux window environment created to allow Blazor to load is cleaned up after the Blazor app has loaded.<br />
63+ /// The default is true for compatibility with other libraries.
64+ /// </summary>
65+ public bool RestoreEnvironment { get ; set ; } = true ;
66+ /// <summary>
6367 /// A ServiceCallDispatcher that executes on this instance
6468 /// </summary>
6569 public ServiceCallDispatcher Local { get ; }
@@ -96,6 +100,10 @@ public class WebWorkerService : IDisposable, IAsyncBackgroundService
96100 /// </summary>
97101 public string WebWorkerJSScript { get ; } = "spawndev.blazorjs.webworkers.js" ;
98102 /// <summary>
103+ /// The script location used for new module worker instances
104+ /// </summary>
105+ public string WebWorkerModuleJSScript { get ; } = "spawndev.blazorjs.webworkers.module.js" ;
106+ /// <summary>
99107 /// The script used for instance interconnect shared worker instance (if used)
100108 /// </summary>
101109 public string WebWorkerInterconnectJSScript { get ; } = "spawndev.blazorjs.webworkers.interconnect.js" ;
@@ -171,11 +179,12 @@ public WebWorkerService(IWebAssemblyServices webAssemblyServices, BlazorJSRuntim
171179 WebWorkerSupported = ! JS . IsUndefined ( "Worker" ) ;
172180 SharedWebWorkerSupported = ! JS . IsUndefined ( "SharedWorker" ) ;
173181 ServiceWorkerSupported = ! JS . IsUndefined ( "ServiceWorkerRegistration" ) ;
174- AppBaseUri = JS . Get < string > ( "document.baseURI" ) ;
182+ AppBaseUri = JS . Get < string ? > ( "document? .baseURI" ) ?? JS . Get < string > ( "documentBaseURI ") ;
175183 var locationHref = JS . Get < string > ( "location.href" ) ;
176184 var locationUri = new Uri ( locationHref ) ;
177185 var workerScriptUri = new Uri ( new Uri ( AppBaseUri ) , WebWorkerJSScript ) ;
178186 WebWorkerJSScript = workerScriptUri . ToString ( ) ;
187+ WebWorkerModuleJSScript = new Uri ( new Uri ( AppBaseUri ) , WebWorkerModuleJSScript ) . ToString ( ) ;
179188 Locks = JS . Get < LockManager > ( "navigator.locks" ) ;
180189 LockManagerSupported = Locks != null ;
181190 var queryParams = HttpUtility . ParseQueryString ( locationUri . Query ) ;
@@ -192,6 +201,11 @@ public WebWorkerService(IWebAssemblyServices webAssemblyServices, BlazorJSRuntim
192201 // remove the instanceOwnerIdKey attribute from the url
193202 NavigationManager . NavigateTo ( newPath , false , true ) ;
194203 }
204+ if ( RestoreEnvironment && ! JS . IsWindow )
205+ {
206+ JS . Delete ( "window" ) ;
207+ JS . Delete ( "document" ) ;
208+ }
195209 Info = new AppInstanceInfo
196210 {
197211 OwnerId = instanceOwnerId ,
@@ -672,9 +686,10 @@ public async Task RegisterServiceWorker()
672686 {
673687 if ( JS . WindowThis != null )
674688 {
689+ var workerMode = ServiceWorkerConfig . Options ? . Type ;
675690 if ( string . IsNullOrEmpty ( ServiceWorkerConfig . ScriptURL ) )
676691 {
677- ServiceWorkerConfig . ScriptURL = WebWorkerJSScript ;
692+ ServiceWorkerConfig . ScriptURL = workerMode == "module" ? WebWorkerModuleJSScript : WebWorkerJSScript ;
678693 }
679694 var kvps = new Dictionary < string , string > ( ) ;
680695 if ( ServiceWorkerConfig . ImportServiceWorkerAssets )
@@ -792,6 +807,68 @@ public async Task<bool> UnregisterServiceWorker()
792807 var webWorker = new WebWorker ( worker , WebAssemblyServices ) ;
793808 return webWorker ;
794809 }
810+
811+ public async Task < WebWorker ? > GetWebWorker ( WebWorkerOptions webWorkerOptions )
812+ {
813+ var webWorker = GetWebWorkerSync ( webWorkerOptions ) ;
814+ if ( webWorker == null ) return null ;
815+ await webWorker . WhenReady ;
816+ return webWorker ;
817+ }
818+
819+ public WebWorker ? GetWebWorkerSync ( WebWorkerOptions webWorkerOptions )
820+ {
821+ if ( ! WebWorkerSupported ) return null ;
822+ webWorkerOptions ??= new WebWorkerOptions ( ) ;
823+ webWorkerOptions . WorkerOptions ??= new WorkerOptions ( ) ;
824+ webWorkerOptions . QueryParams ??= new Dictionary < string , string > ( ) ;
825+ if ( ! string . IsNullOrEmpty ( WorkerIndexHtml ) )
826+ {
827+ webWorkerOptions . QueryParams [ "indexHtml" ] = WorkerIndexHtml ;
828+ }
829+ if ( string . IsNullOrEmpty ( webWorkerOptions . ScriptUrl ) )
830+ {
831+ webWorkerOptions . ScriptUrl = webWorkerOptions . WorkerOptions . Type == "module" ? WebWorkerModuleJSScript : WebWorkerJSScript ;
832+ }
833+ if ( webWorkerOptions . QueryParams . Count > 0 )
834+ {
835+ webWorkerOptions . ScriptUrl += "?" + string . Join ( '&' , webWorkerOptions . QueryParams . Select ( o => $ "{ o . Key } ={ o . Value } ") ) ;
836+ }
837+ var worker = new Worker ( webWorkerOptions . ScriptUrl , webWorkerOptions . WorkerOptions ) ;
838+ var webWorker = new WebWorker ( worker , WebAssemblyServices ) ;
839+ return webWorker ;
840+ }
841+ public async Task < SharedWebWorker ? > GetSharedWebWorker ( SharedWebWorkerOptions webWorkerOptions )
842+ {
843+ var webWorker = GetSharedWebWorkerSync ( webWorkerOptions ) ;
844+ if ( webWorker == null ) return null ;
845+ await webWorker . WhenReady ;
846+ return webWorker ;
847+ }
848+ public SharedWebWorker ? GetSharedWebWorkerSync ( SharedWebWorkerOptions webWorkerOptions )
849+ {
850+ if ( ! WebWorkerSupported ) return null ;
851+ webWorkerOptions ??= new SharedWebWorkerOptions ( ) ;
852+ webWorkerOptions . WorkerOptions ??= new SharedWorkerOptions ( ) ;
853+ webWorkerOptions . QueryParams ??= new Dictionary < string , string > ( ) ;
854+ if ( ! string . IsNullOrEmpty ( WorkerIndexHtml ) )
855+ {
856+ webWorkerOptions . QueryParams [ "indexHtml" ] = WorkerIndexHtml ;
857+ }
858+ if ( string . IsNullOrEmpty ( webWorkerOptions . ScriptUrl ) )
859+ {
860+ webWorkerOptions . ScriptUrl = webWorkerOptions . WorkerOptions . Type == "module" ? WebWorkerModuleJSScript : WebWorkerJSScript ;
861+ }
862+ if ( webWorkerOptions . QueryParams . Count > 0 )
863+ {
864+ webWorkerOptions . ScriptUrl += "?" + string . Join ( '&' , webWorkerOptions . QueryParams . Select ( o => $ "{ o . Key } ={ o . Value } ") ) ;
865+ }
866+ webWorkerOptions . WorkerOptions . Name ??= "" ;
867+ var worker = new SharedWorker ( webWorkerOptions . ScriptUrl , webWorkerOptions . WorkerOptions ) ;
868+ var webWorker = new SharedWebWorker ( webWorkerOptions . WorkerOptions . Name , worker , WebAssemblyServices ) ;
869+ return webWorker ;
870+ }
871+
795872 /// <summary>
796873 /// Returns a new SharedWebWorker instance. If a SharedWorker already existed by this name SharedWebWorker will be connected to that instance.
797874 /// </summary>
0 commit comments