SpawnDev.BlazorJS.WebWorkers
1.2.4
See the version list below for details.
dotnet add package SpawnDev.BlazorJS.WebWorkers --version 1.2.4
NuGet\Install-Package SpawnDev.BlazorJS.WebWorkers -Version 1.2.4
<PackageReference Include="SpawnDev.BlazorJS.WebWorkers" Version="1.2.4" />
paket add SpawnDev.BlazorJS.WebWorkers --version 1.2.4
#r "nuget: SpawnDev.BlazorJS.WebWorkers, 1.2.4"
// Install SpawnDev.BlazorJS.WebWorkers as a Cake Addin #addin nuget:?package=SpawnDev.BlazorJS.WebWorkers&version=1.2.4 // Install SpawnDev.BlazorJS.WebWorkers as a Cake Tool #tool nuget:?package=SpawnDev.BlazorJS.WebWorkers&version=1.2.4
SpawnDev.BlazorJS
Supports .Net 7
An easy Javascript interop library desgined specifcally for client side Blazor.
Use Javascript libraries in Blazor without writing any Javascript code.
Alternative access to IJSRuntime JS is globally available without injection and is usable on the first line of Program.cs
Get and set global proeprties via JS.Set and JS.Get
Create new Javascript objects with JS.New
Get and set object properties via IJSInProcessObjectReference extended methods
Create Callbacks that can be sent to Javascript event listeners or assigned to javascript variables
Easily call Services in separate threads with WebWorkers and SharedWebWorkers
NOTE: The below code shows quick examples. Some objects implement IDisposable, such as all JSObject, IJSInProcessObjectReference, and Callback, and need to be disposed when no longer used.
Firefox note: Firefox does not support dynamic modules in workers, originally make BlazorJS.WebWorkers fail due to this limitation. I wrote a code that changes the scripts before they are loaded to workaround this limitation until Firefox finishes worker module intergration.
https://bugzilla.mozilla.org/show_bug.cgi?id=1540913#c6 https://bugzilla.mozilla.org/show_bug.cgi?id=1247687
JS
// Get Set
var innerHeight = JS.Get<int>("window.innerHeight");
JS.Set("document.title", "Hello World!");
// Call
var item = JS.Call<string?>("localStorage.getItem", "itemName");
JS.CallVoid("addEventListener", "resize", Callback.Create(() => Console.WriteLine("WindowResized"), _callBacks));
IJSInProcessObjectReference extended
// Get Set
var window = JS.Get<IJSInProcessObjectReference>("window");
window.Set("myVar", 5);
var myVar = window.Get<int>("myVar");
// Call
window.CallVoid("addEventListener", "resize", Callback.Create(() => Console.WriteLine("WindowResized")));
Create a new Javascript object
var worker = JS.New("Worker", myWorkerScript);
Pass callbacks to Javascript
JS.Set("testCallback", Callback.Create<string>((strArg) => {
Console.WriteLine($"Javascript sent: {strArg}");
// this prints "Hello callback!"
}));
// in Javascript
testCallback('Hello callback!');
JSObject
Use the extended functions of IJSInProcessObjectReference to work with Javascript objects or use the growing library of over 100 of the most common Javascript objects, including ones for Window, HTMLDocument, WebStorage (locaStorage and sessionStorage), WebGL, WebRTC, and more in SpawnDev.BlazorJS.JSObjects. JSObjects are wrappers around IJSInProcessObjectReference that allow strongly typed use.
Custom JSObjects
Implement your own JSObject classes for Javascript objects not already available in the BlazorJS.JSObjects library.
Instead of this (simple but not as reusable)
var audio = JS.New("Audio", "https://some_audio_online");
audio.CallVoid("play");
Do this...
Create a custom JSObject class
[JsonConverter(typeof(JSObjectConverter<Audio>))]
public class Audio : JSObject
{
public Audio(IJSInProcessObjectReference _ref) : base(_ref) { }
public Audio(string url) : base(JS.New("Audio", url)) { }
public void Play() => JSRef.CallVoid("play");
}
Then use your new object
var audio = new Audio("https://some_audio_online");
audio.Play();
SpawnDev.BlazorJS.WebWorkers
Run CPU intensive tasks on a dedicated worker or on a shared worker with WebWorkers!
Example WebWorkerService setup and usage
// Program.cs
...
using SpawnDev.BlazorJS;
using SpawnDev.BlazorJS.WebWorkers;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
if (JS.IsWindow)
{
// we can skip adding dom objects in non UI threads
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
}
// add services
builder.Services.AddSingleton((sp) => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// SpawnDev.BlazorJS.WebWorkers
builder.Services.AddSingleton<WebWorkerService>();
// app specific services...
builder.Services.AddSingleton<MathsService>();
// build
WebAssemblyHost host = builder.Build();
// init WebWorkerService
var workerService = host.Services.GetRequiredService<WebWorkerService>();
await workerService.InitAsync();
await host.RunAsync();
WebWorker
// Create a WebWorker
var webWorker = await workerService.GetWebWorker();
// Call a registered service on the worker thread with your arguments
// Action types can be passed for progress reporting
var result = await webWorker.InvokeAsync<MathsService, string>("CalculatePiWithActionProgress", piDecimalPlaces, new Action<int>((i) =>
{
piProgress = i;
StateHasChanged();
}));
SharedWebWorker
Calling GetSharedWebWorker in another window with the same sharedWorkerName will return the same SharedWebWorker
// Create or get SHaredWebWorker with the provided sharedWorkerName
var sharedWebWorker = await workerService.GetSharedWebWorker("workername");
// Just like WebWorker but shared
// Call a registered service on the worker thread with your arguments
var result = await sharedWebWorker.InvokeAsync<MathsService, string>("CalculatePiWithActionProgress", piDecimalPlaces, new Action<int>((i) =>
{
piProgress = i;
StateHasChanged();
}));
Send events
// Optionally listen for event messages
worker.OnMessage += (sender, msg) =>
{
if (msg.TargetName == "progress")
{
PiProgress msgData = msg.GetData<PiProgress>();
piProgress = msgData.Progress;
StateHasChanged();
}
};
// From SharedWebWorker or WebWorker threads send an event to conencted parents
workerService.SendEventToParents("progress", new PiProgress { Progress = piProgress });
// Or on send an event to a connected worker
webWorker.SendEvent("progress", new PiProgress { Progress = piProgress });
Inspired by Tewr's BlazorWorker implementation. Thank you! I wrote my implementation from scratch as I needed workers in .Net 7.
https://github.com/Tewr/BlazorWorker
BlazorJS and WebWorkers Demo
https://blazorjs.spawndev.com/
Buy me a coffee
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.0)
- SpawnDev.BlazorJS (>= 1.2.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SpawnDev.BlazorJS.WebWorkers:
Package | Downloads |
---|---|
SpawnDev.BlazorJS.PeerJS
PeerJS simplifies peer-to-peer data, video, and audio calls in Blazor WebAssembly |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.5.19 | 0 | 11/18/2024 |
2.5.18 | 21 | 11/17/2024 |
2.5.17 | 36 | 11/16/2024 |
2.5.16 | 31 | 11/15/2024 |
2.5.15 | 31 | 11/15/2024 |
2.5.14 | 33 | 11/14/2024 |
2.5.13 | 70 | 11/13/2024 |
2.5.12 | 44 | 11/10/2024 |
2.5.11 | 111 | 10/31/2024 |
2.5.10 | 177 | 10/9/2024 |
2.5.9 | 88 | 9/27/2024 |
2.5.8 | 509 | 8/13/2024 |
2.5.6 | 71 | 8/8/2024 |
2.5.5 | 107 | 8/7/2024 |
2.5.4 | 86 | 8/6/2024 |
2.5.3 | 75 | 8/5/2024 |
2.5.2 | 79 | 8/5/2024 |
2.5.1 | 116 | 7/26/2024 |
2.5.0 | 77 | 7/26/2024 |
2.4.7 | 91 | 7/24/2024 |
2.4.6 | 93 | 7/22/2024 |
2.4.5 | 150 | 7/19/2024 |
2.4.4 | 91 | 7/18/2024 |
2.4.3 | 110 | 7/16/2024 |
2.4.2 | 74 | 7/15/2024 |
2.4.0 | 70 | 7/15/2024 |
2.3.8 | 77 | 7/14/2024 |
2.3.7 | 121 | 7/9/2024 |
2.3.6 | 96 | 7/8/2024 |
2.3.5 | 93 | 7/6/2024 |
2.3.4 | 90 | 7/4/2024 |
2.3.3 | 130 | 6/23/2024 |
2.3.2 | 125 | 6/16/2024 |
2.3.1 | 217 | 6/13/2024 |
2.3.0 | 98 | 6/12/2024 |
2.2.106 | 110 | 6/5/2024 |
2.2.105 | 131 | 5/31/2024 |
2.2.104 | 114 | 5/30/2024 |
2.2.103 | 97 | 5/29/2024 |
2.2.102 | 109 | 5/28/2024 |
2.2.101 | 109 | 5/22/2024 |
2.2.100 | 146 | 5/17/2024 |
2.2.99 | 99 | 5/17/2024 |
2.2.98 | 113 | 5/16/2024 |
2.2.97 | 130 | 5/15/2024 |
2.2.96 | 85 | 5/14/2024 |
2.2.95 | 93 | 5/13/2024 |
2.2.94 | 98 | 5/11/2024 |
2.2.93 | 118 | 5/7/2024 |
2.2.92 | 109 | 5/7/2024 |
2.2.91 | 129 | 5/3/2024 |
2.2.90 | 84 | 5/3/2024 |
2.2.89 | 64 | 5/2/2024 |
2.2.88 | 70 | 5/2/2024 |
2.2.87 | 126 | 4/26/2024 |
2.2.86 | 108 | 4/26/2024 |
2.2.85 | 132 | 4/18/2024 |
2.2.84 | 119 | 4/18/2024 |
2.2.83 | 123 | 4/16/2024 |
2.2.82 | 146 | 4/8/2024 |
2.2.81 | 116 | 4/8/2024 |
2.2.80 | 134 | 4/7/2024 |
2.2.79 | 119 | 4/6/2024 |
2.2.78 | 121 | 4/5/2024 |
2.2.77 | 119 | 4/5/2024 |
2.2.76 | 110 | 4/4/2024 |
2.2.75 | 102 | 4/4/2024 |
2.2.73 | 92 | 4/3/2024 |
2.2.72 | 109 | 4/3/2024 |
2.2.71 | 107 | 4/3/2024 |
2.2.70 | 107 | 4/2/2024 |
2.2.69 | 270 | 4/1/2024 |
2.2.68 | 115 | 3/29/2024 |
2.2.67 | 149 | 3/27/2024 |
2.2.66 | 121 | 3/24/2024 |
2.2.65 | 119 | 3/21/2024 |
2.2.64 | 172 | 3/11/2024 |
2.2.63 | 121 | 3/9/2024 |
2.2.62 | 130 | 3/7/2024 |
2.2.61 | 125 | 3/6/2024 |
2.2.60 | 121 | 3/6/2024 |
2.2.58 | 173 | 3/2/2024 |
2.2.57 | 192 | 2/24/2024 |
2.2.56 | 141 | 2/18/2024 |
2.2.55 | 116 | 2/17/2024 |
2.2.53 | 125 | 2/15/2024 |
2.2.52 | 118 | 2/15/2024 |
2.2.51 | 118 | 2/15/2024 |
2.2.49 | 856 | 2/2/2024 |
2.2.48 | 1,363 | 12/29/2023 |
2.2.47 | 179 | 12/20/2023 |
2.2.46 | 124 | 12/15/2023 |
2.2.45 | 139 | 12/10/2023 |
2.2.44 | 128 | 12/10/2023 |
2.2.42 | 138 | 12/9/2023 |
2.2.41 | 130 | 12/9/2023 |
2.2.40 | 123 | 12/8/2023 |
2.2.38 | 1,115 | 11/21/2023 |
2.2.37 | 437 | 11/16/2023 |
2.2.36 | 98 | 11/16/2023 |
2.2.35 | 153 | 11/14/2023 |
2.2.34 | 119 | 11/13/2023 |
2.2.33 | 78 | 11/10/2023 |
2.2.32 | 93 | 11/10/2023 |
2.2.31 | 87 | 11/9/2023 |
2.2.28 | 99 | 11/7/2023 |
2.2.27 | 151 | 10/31/2023 |
2.2.26 | 168 | 10/22/2023 |
2.2.25 | 94 | 10/20/2023 |
2.2.24 | 97 | 10/20/2023 |
2.2.23 | 105 | 10/20/2023 |
2.2.22 | 103 | 10/20/2023 |
2.2.21 | 98 | 10/20/2023 |
2.2.20 | 86 | 10/19/2023 |
2.2.19 | 88 | 10/19/2023 |
2.2.18 | 90 | 10/19/2023 |
2.2.17 | 185 | 10/13/2023 |
2.2.16 | 495 | 10/12/2023 |
2.2.15 | 84 | 10/12/2023 |
2.2.14 | 115 | 10/5/2023 |
2.2.13 | 95 | 10/5/2023 |
2.2.12 | 93 | 10/5/2023 |
2.2.11 | 253 | 10/3/2023 |
2.2.10 | 167 | 9/18/2023 |
2.2.9 | 93 | 9/18/2023 |
2.2.8 | 262 | 9/14/2023 |
2.2.7 | 98 | 9/13/2023 |
2.2.6 | 6,537 | 9/6/2023 |
2.2.5 | 145 | 8/30/2023 |
2.2.4 | 152 | 8/26/2023 |
2.2.3 | 119 | 8/20/2023 |
2.2.2 | 105 | 8/18/2023 |
2.2.1 | 116 | 8/11/2023 |
2.2.0 | 206 | 7/17/2023 |
2.1.15 | 129 | 5/26/2023 |
2.1.14 | 109 | 5/20/2023 |
2.1.13 | 124 | 4/26/2023 |
2.1.12 | 194 | 4/21/2023 |
2.1.11 | 114 | 4/19/2023 |
2.1.10 | 134 | 4/19/2023 |
2.1.8 | 143 | 4/10/2023 |
2.1.7 | 166 | 3/27/2023 |
2.1.6 | 139 | 3/24/2023 |
2.1.5 | 139 | 3/23/2023 |
2.1.4 | 144 | 3/23/2023 |
2.1.3 | 145 | 3/23/2023 |
2.1.2 | 140 | 3/21/2023 |
2.1.0 | 142 | 3/21/2023 |
2.0.3 | 149 | 3/21/2023 |
2.0.2 | 139 | 3/20/2023 |
2.0.1 | 140 | 3/20/2023 |
2.0.0 | 150 | 3/20/2023 |
1.9.2 | 149 | 3/14/2023 |
1.8.1 | 142 | 3/11/2023 |
1.8.0 | 140 | 3/10/2023 |
1.7.1 | 143 | 3/10/2023 |
1.7.0 | 133 | 3/8/2023 |
1.6.4 | 149 | 3/1/2023 |
1.6.3 | 306 | 1/31/2023 |
1.6.2 | 318 | 1/24/2023 |
1.6.1 | 327 | 1/11/2023 |
1.6.0 | 327 | 1/11/2023 |
1.5.0 | 371 | 12/23/2022 |
1.4.0 | 321 | 12/20/2022 |
1.3.0 | 342 | 12/16/2022 |
1.2.7 | 348 | 12/16/2022 |
1.2.5 | 319 | 12/14/2022 |
1.2.4.1 | 328 | 12/13/2022 |
1.2.4 | 317 | 12/13/2022 |
1.2.3 | 319 | 12/13/2022 |