1- // Licensed to the .NET Foundation under one or more agreements.
2- // The .NET Foundation licenses this file to you under the MIT license.
3-
41using Microsoft . JSInterop ;
52
63namespace Company . WebWorker1 ;
74
8- /// <summary>
9- /// Client for communicating with a Web Worker running .NET code.
10- /// </summary>
11- /// <remarks>
12- /// <para>
13- /// Worker methods are static methods marked with <c>[JSExport]</c> in a <c>static partial class</c>.
14- /// By default, they should be defined in the main application. The assembly name in
15- /// <c>dotnet-web-worker.js</c> must match the assembly containing the worker methods.
16- /// The project requires <c><AllowUnsafeBlocks>true</AllowUnsafeBlocks></c> in the .csproj file.
17- /// </para>
18- /// <para>
19- /// Due to <c>[JSExport]</c> limitations, worker methods can only return primitives or strings.
20- /// For complex types, serialize to JSON before returning—it will be automatically deserialized.
21- /// </para>
22- /// <para>
23- /// Example worker class (add this to your main app):
24- /// <code>
25- /// [SupportedOSPlatform("browser")]
26- /// public static partial class MyWorker
27- /// {
28- /// [JSExport]
29- /// public static string Process(string input) => $"Processed: {input}";
30- /// }
31- /// </code>
32- /// </para>
33- /// <para>
34- /// Example usage:
35- /// <code>
36- /// @inject IJSRuntime JSRuntime
37- ///
38- /// private WebWorkerClient? _worker;
39- ///
40- /// protected override async Task OnAfterRenderAsync(bool firstRender)
41- /// {
42- /// if (firstRender)
43- /// {
44- /// _worker = await WebWorkerClient.CreateAsync(JSRuntime);
45- /// }
46- /// }
47- ///
48- /// async Task CallWorker()
49- /// {
50- /// var result = await _worker!.InvokeAsync<string>("MyApp.MyWorker.Process", ["Hello"]);
51- /// }
52- ///
53- /// public async ValueTask DisposeAsync() => await (_worker?.DisposeAsync() ?? ValueTask.CompletedTask);
54- /// </code>
55- /// </para>
56- /// </remarks>
5+ // This class provides a client for communicating with a Web Worker running
6+ // .NET code. The associated JavaScript module is loaded on demand when the
7+ // worker is created.
8+ //
9+ // Worker methods are static methods marked with [JSExport] in a static partial
10+ // class. Due to [JSExport] limitations, worker methods can only return primitives
11+ // or strings. For complex types, serialize to JSON before returning — it will be
12+ // automatically deserialized.
13+ //
14+ // Example worker class:
15+ //
16+ // [SupportedOSPlatform("browser")]
17+ // public static partial class MyWorker
18+ // {
19+ // [JSExport]
20+ // public static string Process(string input) => $"Processed: {input}";
21+ // }
22+ //
23+ // Example usage:
24+ //
25+ // var worker = await WebWorkerClient.CreateAsync(JSRuntime);
26+ // var result = await worker.InvokeAsync<string>("MyApp.MyWorker.Process", ["Hello"]);
27+
5728public sealed class WebWorkerClient ( IJSObjectReference worker ) : IAsyncDisposable
5829{
59- /// <summary>
60- /// Creates and initializes a new .NET Web Worker client instance.
61- /// </summary>
62- /// <param name="jsRuntime">The JS runtime instance.</param>
63- /// <returns>A ready-to-use WebWorkerClient instance.</returns>
64- /// <exception cref="JSException">Thrown if the worker fails to initialize.</exception>
6530 public static async Task < WebWorkerClient > CreateAsync ( IJSRuntime jsRuntime )
6631 {
6732 await using var module = await jsRuntime . InvokeAsync < IJSObjectReference > (
@@ -72,24 +37,11 @@ public static async Task<WebWorkerClient> CreateAsync(IJSRuntime jsRuntime)
7237 return new WebWorkerClient ( workerRef ) ;
7338 }
7439
75- /// <summary>
76- /// Invokes a method on the worker and returns the result.
77- /// </summary>
78- /// <typeparam name="TResult">The type of the result.</typeparam>
79- /// <param name="method">Full method path: "Namespace.ClassName.MethodName"</param>
80- /// <param name="args">Arguments to pass to the method.</param>
81- /// <param name="cancellationToken">Token to cancel the operation.</param>
82- /// <returns>The result from the worker method.</returns>
83- /// <exception cref="OperationCanceledException">Thrown if the operation is canceled.</exception>
84- /// <exception cref="JSException">Thrown if the worker method throws an exception.</exception>
8540 public async Task < TResult > InvokeAsync < TResult > ( string method , object [ ] args , CancellationToken cancellationToken = default )
8641 {
8742 return await worker . InvokeAsync < TResult > ( "invoke" , cancellationToken , [ method, args ] ) ;
8843 }
8944
90- /// <summary>
91- /// Terminates the worker and releases resources.
92- /// </summary>
9345 public async ValueTask DisposeAsync ( )
9446 {
9547 try
@@ -98,7 +50,7 @@ public async ValueTask DisposeAsync()
9850 }
9951 catch ( JSDisconnectedException )
10052 {
101- // Circuit disconnected, worker is already gone
53+ // JS interop disconnected, worker is already gone
10254 }
10355
10456 await worker . DisposeAsync ( ) ;
0 commit comments