|
2 | 2 |
|
3 | 3 | namespace SpawnDev.BlazorJS.WebWorkers |
4 | 4 | { |
| 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> |
5 | 13 | public class SharedWebWorker : ServiceCallDispatcher, IDisposable |
6 | 14 | { |
| 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> |
7 | 18 | public static bool Supported; |
8 | 19 | static SharedWebWorker() |
9 | 20 | { |
10 | 21 | Supported = !JS.IsUndefined("SharedWorker"); |
11 | 22 | } |
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> |
13 | 30 | 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> |
14 | 37 | public SharedWebWorker(string name, SharedWorker sharedWorker, IBackgroundServiceManager webAssemblyServices) : base(webAssemblyServices, sharedWorker.Port) |
15 | 38 | { |
16 | 39 | Name = name; |
17 | | - _shareWorker = sharedWorker; |
| 40 | + _sharedWorker = sharedWorker; |
18 | 41 | if (_port is MessagePort port) port.Start(); |
19 | 42 | } |
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> |
21 | 47 | protected override void Dispose(bool disposing) |
22 | 48 | { |
23 | 49 | if (IsDisposed) return; |
24 | | - _shareWorker?.Dispose(); |
| 50 | + _sharedWorker?.Dispose(); |
25 | 51 | _port?.Dispose(); |
26 | 52 | base.Dispose(disposing); |
27 | 53 | } |
|
0 commit comments