Skip to content

Commit 35ed155

Browse files
committed
Version 3.1.0
1 parent d941cec commit 35ed155

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

SpawnDev.BlazorJS.WebWorkers.Demo/Services/UnitTestsService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ public async Task SharedWebWorkerTest()
671671
{
672672
if (!WebWorkerService.SharedWebWorkerSupported)
673673
{
674-
throw new UnsupportedTestException("SharedWorker not supported by browser.");
674+
throw new UnsupportedTestException("_sharedWorker not supported by browser.");
675675
}
676676

677677
var thisInstanceId = JS.InstanceId;
@@ -696,7 +696,7 @@ public async Task SharedWebWorkersByName()
696696
{
697697
if (!WebWorkerService.SharedWebWorkerSupported)
698698
{
699-
throw new UnsupportedTestException("SharedWorker not supported by browser.");
699+
throw new UnsupportedTestException("_sharedWorker not supported by browser.");
700700
}
701701
// workerA1 and workerA2 will refer to the same shared worker
702702
// workerB is a separate worker instance
@@ -712,8 +712,8 @@ public async Task SharedWebWorkersByName()
712712
var valueGetWorkerA1 = await mathServiceA1.GetValueTest();
713713
var valueGetWorkerA2 = await mathServiceA2.GetValueTest();
714714
if (valueGetWorkerA1 != valueSetWorkerA1) throw new Exception("Unexpected result");
715-
if (valueGetWorkerA2 != valueSetWorkerA1) throw new Exception("SharedWorker appears not shared");
716-
if (valueGetWorkerB == valueSetWorkerA1) throw new Exception("SharedWorker with different name unexpectedly same as first SharedWorker");
715+
if (valueGetWorkerA2 != valueSetWorkerA1) throw new Exception("_sharedWorker appears not shared");
716+
if (valueGetWorkerB == valueSetWorkerA1) throw new Exception("_sharedWorker with different name unexpectedly same as first _sharedWorker");
717717
}
718718
#endregion
719719

SpawnDev.BlazorJS.WebWorkers/SharedWebWorker.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,52 @@
22

33
namespace SpawnDev.BlazorJS.WebWorkers
44
{
5+
/// <summary>
6+
/// Provides a dispatcher for service calls using a shared web worker, enabling concurrent processing across
7+
/// multiple browser contexts.
8+
/// </summary>
9+
/// <remarks>Shared web workers are only supported in environments where the 'SharedWorker' API is
10+
/// available. Use the static 'Supported' property to determine if shared web workers can be utilized. The worker's
11+
/// name uniquely identifies it within the application context. This class manages the lifecycle of the shared
12+
/// worker and ensures proper resource cleanup by implementing IDisposable.</remarks>
513
public class SharedWebWorker : ServiceCallDispatcher, IDisposable
614
{
15+
/// <summary>
16+
/// Returns true if the SharedWorker API is supported in the current environment, allowing for the use of shared web workers. This property is initialized in the static constructor by checking if the 'SharedWorker' object is defined in the JavaScript environment. If it is undefined, it indicates that shared web workers are not supported, and the property will be set to false. This allows developers to conditionally use shared web workers based on the capabilities of the user's browser or environment.
17+
/// </summary>
718
public static bool Supported;
819
static SharedWebWorker()
920
{
1021
Supported = !JS.IsUndefined("SharedWorker");
1122
}
12-
SharedWorker _shareWorker { get; set; }
23+
/// <summary>
24+
/// The underlying SharedWorker instance that this SharedWebWorker manages. This property provides access to the shared worker, allowing for communication and control over its lifecycle. The SharedWorker is responsible for handling concurrent processing across multiple browser contexts, and this property allows developers to interact with it directly if needed. Proper management of the shared worker is crucial for ensuring efficient resource usage and preventing memory leaks, especially when disposing of the SharedWebWorker instance.
25+
/// </summary>
26+
SharedWorker _sharedWorker { get; set; }
27+
/// <summary>
28+
/// The name of the shared worker, which is used to identify it across different browser contexts. This name is
29+
/// </summary>
1330
public string Name { get; }
31+
/// <summary>
32+
/// Creates a new instance of the SharedWebWorker class, initializing it with the specified name, shared worker, and background service manager. The constructor sets up the communication channel with the shared worker and starts the message port if it is available. The shared worker is responsible for handling concurrent processing across multiple browser contexts, while the background service manager manages the services that can be called from the worker. Proper disposal of resources is handled to ensure efficient cleanup when the worker is no longer needed.
33+
/// </summary>
34+
/// <param name="name"></param>
35+
/// <param name="sharedWorker"></param>
36+
/// <param name="webAssemblyServices"></param>
1437
public SharedWebWorker(string name, SharedWorker sharedWorker, IBackgroundServiceManager webAssemblyServices) : base(webAssemblyServices, sharedWorker.Port)
1538
{
1639
Name = name;
17-
_shareWorker = sharedWorker;
40+
_sharedWorker = sharedWorker;
1841
if (_port is MessagePort port) port.Start();
1942
}
20-
43+
/// <summary>
44+
/// Disposes of the resources used by the SharedWebWorker instance, including terminating the shared worker and cleaning up the message port. This method ensures that all resources are properly released when the worker is no longer needed, preventing memory leaks and ensuring efficient resource management. The disposal process includes invoking any registered disposal events and handling exceptions that may occur during termination. After disposing of the shared worker and message port, it calls the base class's Dispose method to complete the cleanup process.
45+
/// </summary>
46+
/// <param name="disposing"></param>
2147
protected override void Dispose(bool disposing)
2248
{
2349
if (IsDisposed) return;
24-
_shareWorker?.Dispose();
50+
_sharedWorker?.Dispose();
2551
_port?.Dispose();
2652
base.Dispose(disposing);
2753
}

SpawnDev.BlazorJS.WebWorkers/SpawnDev.BlazorJS.WebWorkers.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7-
<Version>3.0.0</Version>
7+
<Version>3.1.0</Version>
88
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<EmbedAllSources>true</EmbedAllSources>
@@ -76,7 +76,7 @@
7676
</PropertyGroup>
7777

7878
<ItemGroup>
79-
<PackageReference Include="SpawnDev.BlazorJS" Version="3.0.0" />
79+
<PackageReference Include="SpawnDev.BlazorJS" Version="3.4.0" />
8080
</ItemGroup>
8181

8282
<ItemGroup>

SpawnDev.BlazorJS.WebWorkers/WebWorkerService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public WebWorkerService(IBackgroundServiceManager webAssemblyServices, BlazorJSR
176176
if (JS.IsBrowser)
177177
{
178178
WebWorkerSupported = !JS.IsUndefined("Worker");
179-
SharedWebWorkerSupported = !JS.IsUndefined("SharedWorker");
179+
SharedWebWorkerSupported = !JS.IsUndefined("_sharedWorker");
180180
ServiceWorkerSupported = !JS.IsUndefined("ServiceWorkerRegistration");
181181
AppBaseUri = JS.Get<string?>("document?.baseURI") ?? JS.Get<string>("documentBaseURI");
182182
var locationHref = JS.Get<string>("location.href");

0 commit comments

Comments
 (0)