SpawnDev.BlazorJS
2.5.7
See the version list below for details.
dotnet add package SpawnDev.BlazorJS --version 2.5.7
NuGet\Install-Package SpawnDev.BlazorJS -Version 2.5.7
<PackageReference Include="SpawnDev.BlazorJS" Version="2.5.7" />
paket add SpawnDev.BlazorJS --version 2.5.7
#r "nuget: SpawnDev.BlazorJS, 2.5.7"
// Install SpawnDev.BlazorJS as a Cake Addin #addin nuget:?package=SpawnDev.BlazorJS&version=2.5.7 // Install SpawnDev.BlazorJS as a Cake Tool #tool nuget:?package=SpawnDev.BlazorJS&version=2.5.7
SpawnDev.BlazorJS
Full Blazor WebAssembly and Javascript interop. Create, access properties, call methods, and add/remove event handlers of any Javascript objects the .Net way without writing Javascript.
SpawnDev.BlazorJS.WebWorkers is now in a separate repo here.
Supported .Net Versions
- Blazor WebAssembly .Net 6, 7, 8, and 9
- Tested VS Template: Blazor WebAssembly Standalone App
- Blazor United .Net 8 (in WebAssembly project only)
- Tested VS Template: Blazor Web App (Interactive WebAssembly mode without prerendering)
Features:
- Supports all web browser Web APIs
- If we missed anything, open an issue and it will be updated ASAP.
- Supports all web browser Javascript data types
- Over 350 strongly typed JSObject wrappers (listed here) included in BlazorJS including DOM, Crypto, WebGL, WebRTC, Atomics, TypedArrays, and Promises allow direct interaction with Javascript
- Use Javascript libraries in Blazor without writing any Javascript code
- BlazorJSRuntime wraps the default JSRuntime adding additional functionality
- Create new Javascript objects directly from Blazor
- Get and set Javascript object properties as well as access methods
- Easily pass .Net methods to Javascript using ActionEvent, Callback.Create or Callback.CreateOne methods
- Easily wrap your Javascript objects for direct manipulation from Blazor (No javascript required!)
- Create a class that inherits from JSObject and define the methods, properties, events, and constructors.
- Supports Promise, Union method parameters, and passing undefined to Javascript
- Supports Tuple, ValueTuple serialization to and from a Javascript Array
- Supports null-conditional member access operator ?. in JS interop
Issues and Feature requests
I'm here to help. If you find a bug or missing properties, methods, or Javascript objects please submit an issue here on GitHub. I will help as soon as possible.
BlazorJSRuntime
Getting started. Using BlazorJS requires 2 changes to your Program.cs.
- Add the BlazorJSRuntime service with builder.Services.AddBlazorJSRuntime()
- Initialize BlazorJSRuntime by calling builder.Build().BlazorJSRunAsync() instead of builder.Build().RunAsync()
- Supports null-conditional member access operator ?. in JS interop
// ... other usings
using SpawnDev.BlazorJS;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
// Add SpawnDev.BlazorJS.BlazorJSRuntime
builder.Services.AddBlazorJSRuntime();
// build and Init using BlazorJSRunAsync (instead of RunAsync)
await builder.Build().BlazorJSRunAsync();
Inject into components
[Inject]
BlazorJSRuntime JS { get; set; }
Examples uses
// Get and 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));
// Attach events
using var window = JS.Get<Window>("window");
window.OnOffline += Window_OnOffline;
// AddEventListener and RemoveEventListener are supported on all EventTarget objects
window.AddEventListener("resize", Window_OnResize, true);
window.RemoveEventListener("resize", Window_OnResize, true);
IMPORTANT NOTE - Async vs Sync Javascript calls
SpawnDev's BlazorJSRuntime behaves differently than Microsoft's Blazor JSRuntime. SpawnDev's BlazorJSRuntime is more of a 1 to 1 mapping to Javascript.
When calling Javascript methods that are not asynchronous and do not return a Promise you need to use the synchronous BlazorJSRuntime methods Call, CallVoid, or Get. Unlike the default Blazor JSRuntime which would allow the use of InvokeAsync, you must use the synchronous BlazorJSRuntime methods.
Use synchronous BlazorJSRuntime calls for synchronous Javascript methods. BlazorJSRuntime CallAsync would throw an error if used on the below Javascript method.
// Javascript
function AddNum(num1, num2){
return num1 + num2;
}
// C#
var total = JS.Call<int>("AddNum", 20, 22);
// total == 42 here
Use synchronous BlazorJSRuntime calls for asynchronous Javascript methods.
// Javascript
async function AddNum(num1, num2){
return num1 + num2;
}
// C#
var total = await JS.Call<Task<int>>("AddNum", 20, 22);
// total == 42 here
// C#
var totalPromise = JS.Call<Promise<int>>("AddNum", 20, 22);
var total = await totalPromise.ThenAsync();
// total == 42 here
Use asynchronous BlazorJSRuntime calls for asynchronous Javascript methods.
// Javascript
async function AddNum(num1, num2){
return num1 + num2;
}
// C#
var total = await JS.CallAsync<int>("AddNum", 20, 22);
// total == 42 here
Use asynchronous BlazorJSRuntime calls for methods that return a Promise.
// Javascript
function AddNum(num1, num2){
return new Promise((resolve, reject)=>{
resolve(num1 + num2);
});
}
// C#
var total = await JS.CallAsync<int>("AddNum", 20, 22);
// total == 42 here
NULL Conditional
The BlazorJSRuntime now supports null-conditional member access operator ?..
Note: The null-conditional member access operator ?. is also known as the Elvis operator.
Example
// Javascript
var fruit = {
name: 'apple',
color: 'red'
};
The below JS.Get
would throw an error because fruit.options
does not exist, and therefore we cannot access a property of it.
// C#
var size = JS.Get<int?>("fruit.options.size");
// never gets here due to error because `fruit.options` does not exist
Using a null conditional (the ?
in fruit.options?.size
) prevents the error by allowing fruit.options
to not exist (null or undefined.)
// C#
var size = JS.Get<int?>("fruit.options?.size");
// size == null here (default value for int?)
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
IJSInProcessObjectReference worker = JS.New("Worker", myWorkerScript);
ActionEvent
Used throughout the JSObject collection, ActionEvent allows a clean .Net style way to add and remove .Net callbacks for Javascript events.
With ActionEvent the operands += and -= can be used to attach and detach .Net callbacks to Javascript events. All reference handling is done automatically when events are added and removed.
Example taken from the Window JSObject class which inherits from EventTarget.
// This is how ActionEvent is implemented in the Window class
public ActionEvent<StorageEvent> OnStorage { get => new ActionEvent<StorageEvent>("storage", AddEventListener, RemoveEventListener); set { } }
Example event attach detach
void AttachEventHandlersExample()
{
using var window = JS.Get<Window>("window");
// If this is the first time Window_OnStorage has been attached to an event a .Net reference is automatically created and held for future use and removal
window.OnStorage += Window_OnStorage;
// the window JSObject reference can safely be disposed as the .Net reference is attached to Window_OnStorage internally
}
void DetachEventHandlersExample()
{
using var window = JS.Get<Window>("window");
// If this is the last reference of Window_OnStorage being removed then the .Net reference will automatically be disposed.
// IMPORTANT - detaching is important for preventing resource leaks. .Net references are only released when the reference count reaches zero (same number of -= as += used)
window.OnStorage -= Window_OnStorage;
}
void Window_OnStorage(StorageEvent storageEvent)
{
Console.WriteLine($"StorageEvent");
}
ActionEvent arguments are optional
Methods attached using JSEventCallbacks are strongly typed and, like Javascript, all arguments are optional. This can improve performance as unused variables will not be brought into Blazor during the event.
Example event attach detach (from above) without using any callback arguments.
void AttachEventHandlersExample()
{
using var window = JS.Get<Window>("window");
window.OnStorage += Window_OnStorage;
}
void DetachEventHandlersExample()
{
using var window = JS.Get<Window>("window");
window.OnStorage -= Window_OnStorage;
}
// The method below is not using the optional StorageEvent argument
void Window_OnStorage()
{
Console.WriteLine($"StorageEvent");
}
ActionEvent.On() and ActionEvent.Off()
ActionEvent has additional methods for attaching event handlers; On
and Off
. These methods provide support attaching event handlers with alternative parameter types.
Or use the On
and Off
methods:
void AttachEventHandlersExample()
{
using var window = JS.Get<Window>("window");
window.OnStorage.On(Window_OnStorage);
}
void DetachEventHandlersExample()
{
using var window = JS.Get<Window>("window");
window.OnStorage.Off(Window_OnStorage);
}
// The method below is not using the optional StorageEvent argument
void Window_OnStorage()
{
Console.WriteLine($"StorageEvent");
}
Event handler parameters
Parameters of event handlers may be omitted if not required.
FuncEvent
FuncEvent works just like ActionEvent but, as the name indicates, works with Func
methods instead of Action
methods to allow returning a value.
Action and Func serialization
BlazorJS supports serialization of both Func and Action types. Internally the BlazorJS.Callback object is used. Serialized and deserialized Action and Func objects must call their DisposeJS() extension method to dispose the auto created and associated Callback and/or Function objects.
Action test from BlazorJSUnitTests.cs
var tcs = new TaskCompletionSource<bool>();
var callback = () =>
{
tcs.TrySetResult(true);
};
JS.CallVoid("setTimeout", callback, 100);
await tcs.Task;
callback.DisposeJS();
Func<,> test from BlazorJSUnitTests.cs
int testValue = 42;
var origFunc = new Func<int, int>((val) =>
{
return val;
});
// set a global Javascript var to our Func<int>
// if this is the first time this Func is passed to Javascript a Callback will be created and associated to this Func for use in future serialization
// the auto created Callback must be disposed by calling the extension method Func.DisposeJS()
JS.Set("_funcCallback", origFunc);
// read back in our Func as an Func
// internally a Javascript Function reference is created and associated with this Func.
// the auto created Function must be disposed by calling the extension method Func.DisposeJS()
var readFunc = JS.Get<Func<int, int>>("_funcCallback");
var readVal = readFunc(testValue);
if (readVal != testValue) throw new Exception("Unexpected result");
// dispose the Function created and associated with the read Func
readFunc.DisposeJS();
// dispose the Callback created and associated with the original Func
origFunc.DisposeJS();
Callback
The Callback object is used to support Action and Func serialization. It can be used for a bit more control over the lifetime of you callbacks. Pass methods to Javascript using the Callback.Create and Callback.CreateOne methods. These methods use type arguments to set the types expected for incoming arguments (if any) and the expected return type (if any.) async methods are passed as Promises.
Pass lambda callbacks to Javascript
JS.Set("testCallback", Callback.Create((string strArg) => {
Console.WriteLine($"Javascript sent: {strArg}");
// this prints "Hello callback!"
}));
// in Javascript
testCallback('Hello callback!');
Pass method callbacks to Javascript
string SomeNetFn(string input){
return $"Recvd: {input}";
}
JS.Set("someNetFn", Callback.CreateOne(SomeNetFn));
// in Javascript
someNetFn('Hello callback!');
// prints
Recvd: Hello callback!
Pass async method callbacks to Javascript Under the hood, BlazorJS is returning a Promise to Javascript when the method is called
async Task<string> SomeNetFnAsync(string input)
{
await Task.Delay(1000);
return $"Recvd: {input}";
}
JS.Set("someNetFnAsync", Callback.CreateOne(SomeNetFnAsync));
// in Javascript
await someNetFnAsync('Hello callback!');
// prints
Recvd: Hello callback!
JSObjects
Over 350 Javascript types are ready to go in SpawnDev.BlazorJS. The classes are designed to match the Javascript Web API interfaces as closely as possible. Below are some examples.
HTMLVideoElement
From HTMLVideoElement on MDN:
Implemented by the <video> element, the HTMLVideoElement interface provides special properties and methods for manipulating video objects. It also inherits properties and methods of HTMLMediaElement and HTMLElement.
Example using SpawnDev.BlazorJS.JSObjects.HTMLVideoElement
From HTMLVideoElementExample.cs
@page "/HTMLVideoElementExample"
@implements IDisposable
<div>
<video style="width: 640px; height: 480px;" controls autoplay muted @ref=videoElRef></video>
</div>
<div>
Source: @videoName
</div>
<div>
Duration: @duration.ToString()
</div>
<div>
Metadata: @metadata
</div>
<div>
@foreach (var video in videos)
{
<button onclick="@(() => SetSource(video.Key, video.Value))">@video.Key</button>
}
</div>
<pre>
@((MarkupString)log)
</pre>
@code {
[Inject]
BlazorJSRuntime JS { get; set; }
ElementReference? videoElRef;
HTMLVideoElement? videoEl = null;
TimeSpan duration = TimeSpan.Zero;
string videoName = "";
string metadata = "";
string log = "";
Dictionary<string, string> videos = new Dictionary<string, string>
{
{ "Elephants Dream", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4" },
{ "Big Buck Bunny", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" },
{ "Tears Of Steel", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4" },
{ "Sintel", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4" },
{ "None", "" },
};
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
videoEl = (HTMLVideoElement)videoElRef!;
videoEl.OnLoadedMetadata += VideoEl_OnLoadedMetadata;
videoEl.OnAbort += VideoEl_OnAbort;
videoEl.OnError += VideoEl_OnError;
}
}
void SetSource(string name, string source)
{
if (videoEl == null) return;
Log($"SetSource: {name}");
videoName = name;
videoEl.Src = source;
StateHasChanged();
}
void VideoEl_OnLoadedMetadata()
{
Log("VideoEl_OnLoadedMetadata");
metadata = $"{videoEl!.VideoWidth}x{videoEl!.VideoHeight}";
duration = TimeSpan.FromSeconds(videoEl!.Duration ?? 0);
StateHasChanged();
}
void VideoEl_OnError()
{
Log("VideoEl_OnError");
}
void VideoEl_OnAbort()
{
Log("VideoEl_OnAbort");
metadata = $"{videoEl!.VideoWidth}x{videoEl!.VideoHeight}";
duration = TimeSpan.FromSeconds(videoEl!.Duration ?? 0);
StateHasChanged();
}
public void Dispose()
{
if (videoEl != null)
{
videoEl.OnLoadedMetadata -= VideoEl_OnLoadedMetadata;
videoEl.OnAbort -= VideoEl_OnAbort;
videoEl.OnError -= VideoEl_OnError;
videoEl.Dispose();
videoEl = null;
}
}
void Log(string message)
{
log += $"{message}<br/>";
}
}
HTMLCanvasElement
Example coming soon
Storage - LocalStorage, SessionStorage
[Inject]
BlazorJSRuntime JS { get; set; }
override void OnInitialized()
{
using Storage localStorage = JS.Get<Storage>("localStorage");
localStorage.SetItem("myKey", "myValue");
var myValue = localStorage.GetItem("myKey");
// myValue == "myValue"
}
IndexedDB
From IndexedDB on MDN:
IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.
The below code was written to test various features of the IndexedDB API and this code specifically tests using a Tuple as an ObjectStore key.
[Inject]
BlazorJSRuntime JS { get; set; }
public class Fruit
{
public (byte[], long) MyKey { get; set; }
public string Name { get; set; }
public string Color { get; set; }
}
override void OnInitialized()
{
var dbName = "garden_tuple";
var dbStoreName = "fruit";
// Get the global IDBFactory (equivalent to 'JS.Get<IDBFactory>("indexedDB")')
using var idbFactory = new IDBFactory();
var idb = await idbFactory.OpenAsync(dbName, 2, (evt) =>
{
// upgrade needed
using var request = evt.Target;
using var db = request.Result;
var stores = db.ObjectStoreNames;
if (!stores.Contains(dbStoreName))
{
using var store = db.CreateObjectStore<string, Fruit>(dbStoreName, new IDBObjectStoreCreateOptions { KeyPath = "name" });
store.CreateIndex<(byte[], long)>("tuple_index", "myKey");
}
});
// transaction
using var tx = idb.Transaction(dbStoreName, "readwrite");
using var objectStore = tx.ObjectStore<string, Fruit>(dbStoreName);
// add some data
await objectStore.PutAsync(new Fruit { Name = "apple", Color = "red", MyKey = (new byte[] { 1, 2, 3 }, 5) });
await objectStore.PutAsync(new Fruit { Name = "orange", Color = "orange", MyKey = (new byte[] { 1, 2, 5 }, 5) });
await objectStore.PutAsync(new Fruit { Name = "lemon", Color = "yellow", MyKey = (new byte[] { 1, 2, 5 }, 5) });
await objectStore.PutAsync(new Fruit { Name = "lime", Color = "green", MyKey = (new byte[] { 33, 33, 45 }, 5) });
// get an IDBIndex
using var myIndex = objectStore.Index<(byte[], long)>("tuple_index");
// create a range using ValueTuple type
using var range = IDBKeyRange<(byte[], long)>.Bound((new byte[] { 0, 0, 0 }, 0), (new byte[] { 5, 5, 5 }, long.MaxValue));
var included = range.Includes((new byte[] { 1, 2, 4 }, 5));
var cmpRet0 = idbFactory.Cmp<(byte[], long)>((new byte[] { 1, 2, 3 }, 6), (new byte[] { 1, 2, 3 }, 5));
var cmpRet1 = idbFactory.Cmp<(byte[], long)>((new byte[] { 1, 2, 3 }, 5), (new byte[] { 1, 2, 3 }, 5));
var cmpRet2 = idbFactory.Cmp<(byte[], long)>((new byte[] { 1, 2, 2 }, 5), (new byte[] { 1, 2, 3 }, 5));
var cmpRet3 = idbFactory.Cmp<(byte[], long)>((new byte[] { 1, 2, 4 }, 5), (new byte[] { 1, 2, 3 }, 4));
// getAll on IDBIndex using the above range
using var getAll = await myIndex.GetAllAsync(range);
// below prints "apple", "orange", "lemon"
// the "lime" entry's byte[] is outside of our range and therefore not included
foreach (var item in getAll.ToArray())
{
JS.Log(item.Name);
}
// get on IDBIndex. returns null if not found.
var get = await myIndex.GetAsync((new byte[] { 1, 2, 5 }, 5));
JS.Log("get", get);
// getAll on ObjectStore
var getAllStore = await objectStore.GetAllAsync();
JS.Log("getAllStore", getAllStore);
// IDBCursor iteration
using var cursor = await myIndex.OpenCursorAsync();
var hasData = cursor != null;
while (hasData)
{
var canCont1 = await cursor.CanContinue();
JS.Log("Entry", cursor!.Value);
hasData = await cursor!.ContinueAsync();
var canCont2 = await cursor.CanContinue();
var nmt = true;
}
JS.Log("Done");
}
Cache
From Cache on MDN
The Cache interface provides a persistent storage mechanism for Request / Response object pairs that are cached in long lived memory.
Example coming soon
TypedArray
SpawnDev.BlazorJS supports all TypedArray types.
Example coming soon
Atomics
From Atomics on MDN:
The Atomics namespace object contains static methods for carrying out atomic operations. They are used with SharedArrayBuffer and ArrayBuffer objects.
Example coming soon
JSObject Base Class
JSObjects are wrappers around IJSInProcessReference objects that can be passed to and from Javascript and allow strongly typed access to the underlying object.
JSObject type wrapper example
// create a class for your Javascript object that inherits from JSObject
public class Window : JSObject
{
// required constructor
public Window(IJSInProcessObjectReference _ref) : base(_ref) { }
public string Name { get => JSRef.Get<string>("name"); set => JSRef.Set("name", value); }
public void Alert(string msg = "") => JSRef.CallVoid(msg);
// ...
}
// use the JSObject class to interact with the Javascript object
public void JSObjectClassTest() {
var w = JS.Get<Window>("window");
var randName = Guid.NewGuid().ToString();
// directly set the window.name property
w.Name = randName;
// verify the read back
if (w.Name != randName) throw new Exception("Interface property set/get failed");
}
Use the extended functions of IJSInProcessObjectReference to work with Javascript objects or use the growing library of over 350 of the most common Javascript objects, including ones for Window, Document, Storage (localStorage and sessionStorage), WebGL, WebRTC, and more in SpawnDev.BlazorJS.JSObjects. JSObjects are wrappers around IJSInProcessObjectReference that allow strongly typed use.
Below shows a section of the SpawnDev.BlazorJS.JSObjects.Window class. Window's base type, EventTarget, inherits from JSObject.
public class Window : EventTarget {
// all JSObject types must have this constructor
public Window(IJSInProcessObjectReference _ref) : base(_ref) { }
// here is a property with both getter and setter
public string? Name { get => JSRef.Get<string>("name"); set => JSRef.Set("name", value); }
// here is a read only property that returns another JSObject type
public Storage LocalStorage => JSRef.Get<Storage>("localStorage");
// here are methods
public long SetTimeout(Callback callback, double delay) => JSRef.Call<long>("setTimeout", callback, delay);
public void ClearTimeout(long requestId) => JSRef.CallVoid("clearTimeout", requestId);
// ...
}
Below the JSObject derived Window class is used
// below the JSObject derived Window class is used
using var window = JS.Get<Window>("window");
var randName = Guid.NewGuid().ToString();
// set and get properties
window.Name = randName;
var name = window.Name;
// call methods
window.Alert("Hello!");
Promise
SpawnDev.BlazorJS.JSObjects.Promise - is a JSObject wrapper for the Javascript Promise class. Promises can be created in .Net to wrap async methods or Tasks. They are essentially Javascript's version of Task.
Create Promise from lambda method
var promise = new Promise(async () => {
await Task.Delay(5000);
});
// pass to Javascript api
Create Promise from lambda method with return value
var promise = new Promise<string>(async () => {
await Task.Delay(5000);
return "Hello world!";
});
// pass to Javascript api
Create Promise from Task
var taskSource = new TaskCompletionSource<string>();
var promise = new Promise<string>(taskSource.Task);
// pass to Javascript api
// then later resolve
taskSource.TrySetResult("Hello world!");
Below is a an example that uses Promises to utilize the Web Locks API (Note: The below code is designed to demonstrate the use of a Promise. This is not the recommended way of using LockManager. See WebWorkerService.Locks for more info.)
using var navigator = JS.Get<Navigator>("navigator");
using var locks = navigator.Locks;
Console.WriteLine($"lock: 1");
using var waitLock = locks.Request("my_lock", Callback.CreateOne((Lock lockObj) => new Promise(async () => {
Console.WriteLine($"lock acquired 3");
await Task.Delay(5000);
Console.WriteLine($"lock released 4");
})));
using var waitLock2 = locks.Request("my_lock", Callback.CreateOne((Lock lockObj) => new Promise(async () => {
Console.WriteLine($"lock acquired 5");
await Task.Delay(5000);
Console.WriteLine($"lock released 6");
})));
Console.WriteLine($"lock: 2");
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");
You can do this...
Create a custom Audio JSObject wrapper (Example only. Already exists.)
public class Audio : JSObject
{
// deserialization constructor
public Audio(IJSInProcessObjectReference _ref) : base(_ref) { }
// constructor that accepts a string url
public Audio(string url) : base(JS.New("Audio", url)) { }
// method decalaration
public void Play() => JSRef.CallVoid("play");
}
Then use the Audio JSObject
var audio = new Audio("https://some_audio_online");
audio.Play();
Union
Use the Union<T1, T2, ...> type with method parameters for strong typing while allowing unrelated types just like in TypeScript.
void UnionTypeTestMethod(string varName, Union<bool?, string?>? unionTypeValue)
{
JS.Set(varName, unionTypeValue);
}
var stringValue = "Hello world!";
UnionTypeTestMethod("_stringUnionValue", stringValue);
if (stringValue != JS.Get<string?>("_stringUnionValue")) throw new Exception("Unexpected result");
var boolValue = true;
UnionTypeTestMethod("_boolUnionValue", boolValue);
if (boolValue != JS.Get<bool?>("_boolUnionValue")) throw new Exception("Unexpected result");
Undefinable
Use Undefinable<T> type to pass undefined to Javascript
Some Javascript API calls may have optional parameters that behave differently depending on if you pass a null versus undefined. You can now retain strong typing on JSObject method calls and support passing undefined for JSObject parameters.
Undefinable<T> type.
Example from Test app unit tests
// an example method with a parameter that can also be null or undefined
// T of Undefinable<T> must be nullable
void MethodWithUndefinableParams(string varName, Undefinable<bool?>? window)
{
JS.Set(varName, window);
}
bool? w = false;
// test to show the value is passed normally
MethodWithUndefinableParams("_willBeDefined2", w);
bool? r = JS.Get<bool?>("_willBeDefined2");
if (r != w) throw new Exception("Unexpected result");
w = null;
// null defaults to passing as undefined
MethodWithUndefinableParams("_willBeUndefined2", w);
if (!JS.IsUndefined("_willBeUndefined2")) throw new Exception("Unexpected result");
// if you need to pass null to an Undefinable parameter use Undefinable<T?>.Null
MethodWithUndefinableParams("_willBeNull2", Undefinable<bool?>.Null);
if (JS.IsUndefined("_willBeNull2")) throw new Exception("Unexpected result");
// another way to pass undefined
MethodWithUndefinableParams("_willAlsoBeUndefined2", Undefinable<bool?>.Undefined);
if (!JS.IsUndefined("_willAlsoBeUndefined2")) throw new Exception("Unexpected result");
If using JSObjects you can also use JSObject.Undefined<T> to create an instance that will be passed to Javascript as undefined.
// Create an instance of the Window JSObject class that is revived in Javascript as undefined
var undefinedWindow = JSObject.Undefined<Window>();
// undefinedWindow is an instance of Window that is revived in Javascript as undefined
JS.Set("_undefinedWindow", undefinedWindow);
var isUndefined = JS.IsUndefined("_undefinedWindow");
// isUndefined == true here
SpawnDev.BlazorJS.WebWorkers
- SpawnDev.BlazorJS.WebWorkers has moved to its own repo: SpawnDev.BlazorJS.WebWorkers
Blazor Web App compatibility
.Net 8 introduced a new hosting model that allows mixing Blazor server render mode and Blazor WebAssembly render mode. Prerendering was also added to improve initial rendering times. "Prerendering is the process of initially rendering page content on the server without enabling event handlers for rendered controls."
One of the primary goals of SpawnDev.BlazorJS is to give Web API access to Blazor WebAssembly that mirrors Javascript's own Web API. This includes calling conventions. For example, a call that is synchronous in Javascript is synchronous in Blazor, an asynchronous call is asynchronous. To provide that, SpawnDev.BlazorJS requires access to Micorosft's IJSInProcessRuntime and IJSInProcessRuntime is only available in Blazor WebAssembly.
Compatible Blazor Web App
options:
Prerendering is not compatible with SpawnDev.BlazorJS because it runs on the server. So we need to let Blazor know that SpawnDev.BlazorJS components must be rendered only with WebAssembly. How this is done depends on your project settings.
Interactive render mode
- Auto (Server and WebAssembly)
or WebAssembly
Interactivity location
- Per page/component
In the Server project App.razor
:
<Routes />
In WebAssembly pages and components that require SpawnDev.BlazorJS:
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
Interactivity location
- Global
In the Server project App.razor
:
<Routes @rendermode="new InteractiveWebAssemblyRenderMode(prerender: false)" />
IDisposable
NOTE: The above code shows quick examples. Some objects implement IDisposable, such as JSObject, Callback, and IJSInProcessObjectReference types.
JSObject types will dispose of their IJSInProcessObjectReference object when their finalizer is called if not previously disposed.
Callback types must be disposed unless created with the Callback.CreateOne method, in which case they will dispose themselves after the first callback. Disposing a Callback prevents it from being called.
IJSInProcessObjectReference does not dispose of interop resources with a finalizer and MUST be disposed when no longer needed. Failing to dispose these will cause memory leaks.
IDisposable objects returned from a WebWorker or SharedWorker service are automatically disposed after the data has been sent to the calling thread.
Support for You
Issues can be reported here on GitHub. Create a new discussion to show off your projects and post your ideas. We are always here to help.
Support for Us
Sponsor us via Github Sponsors to give us more time to work on SpawnDev.BlazorJS.WebWorkers and other open source projects. Or buy us a cup of coffee via Paypal. All support is greatly appreciated! ♥
Thanks
Thank you to everyone who has helped support SpawnDev.BlazorJS and related projects financially, by filing issues, and by improving the code. Every little contribution helps!
Demos
BlazorJS and WebWorkers Demo
https://blazorjs.spawndev.com/
Current site under development using Blazor WASM
https://www.spawndev.com/
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. 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 is compatible. 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. net9.0 is compatible. |
-
net6.0
- Microsoft.AspNetCore.Components.WebAssembly (>= 6.0.25)
-
net7.0
- Microsoft.AspNetCore.Components.WebAssembly (>= 7.0.14)
-
net8.0
- Microsoft.AspNetCore.Components.WebAssembly (>= 8.0.1)
-
net9.0
- Microsoft.AspNetCore.Components.WebAssembly (>= 9.0.0-preview.6.24328.4)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on SpawnDev.BlazorJS:
Package | Downloads |
---|---|
SpawnDev.BlazorJS.WebWorkers
Call Services and static methods in separate threads with WebWorkers and SharedWebWorkers. Run Blazor WASM in the ServiceWorker. |
|
SpawnDev.BlazorJS.WebTorrents
WebTorrents in Blazor WebAssembly |
|
SpawnDev.BlazorJS.VisNetwork
VisNetwork in Blazor WebAssembly |
|
SpawnDev.BlazorJS.FFmpegWasm
SpawnDev.BlazorJS.FFmpegWasm is a Blazor WASM wrapper around ffmpeg.wasm and contains only the base ffmpeg.js and 814.ffmpeg.js files. |
|
SpawnDev.BlazorJS.SimplePeer
SimplePeer WebRTC video, voice, and data channels for Blazor WebAssembly |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.5.11 | 120 | 10/31/2024 |
2.5.10 | 648 | 10/9/2024 |
2.5.9 | 179 | 9/27/2024 |
2.5.8 | 2,800 | 8/13/2024 |
2.5.7 | 95 | 8/13/2024 |
2.5.6 | 79 | 8/8/2024 |
2.5.5 | 257 | 8/7/2024 |
2.5.4 | 125 | 8/6/2024 |
2.5.3 | 89 | 8/5/2024 |
2.5.2 | 93 | 8/5/2024 |
2.5.1 | 226 | 7/26/2024 |
2.5.0 | 108 | 7/26/2024 |
2.4.7 | 115 | 7/24/2024 |
2.4.6 | 212 | 7/22/2024 |
2.4.5 | 178 | 7/19/2024 |
2.4.4 | 138 | 7/18/2024 |
2.4.3 | 253 | 7/16/2024 |
2.4.2 | 96 | 7/15/2024 |
2.4.0 | 95 | 7/15/2024 |
2.3.8 | 101 | 7/14/2024 |
2.3.7 | 1,104 | 7/9/2024 |
2.3.6 | 123 | 7/8/2024 |
2.3.5 | 136 | 7/6/2024 |
2.3.4 | 292 | 7/4/2024 |
2.3.3 | 383 | 6/23/2024 |
2.3.2 | 317 | 6/16/2024 |
2.3.1 | 243 | 6/13/2024 |
2.3.0 | 178 | 6/12/2024 |
2.2.106 | 245 | 6/5/2024 |
2.2.105 | 285 | 5/31/2024 |
2.2.104 | 170 | 5/30/2024 |
2.2.103 | 158 | 5/29/2024 |
2.2.102 | 168 | 5/28/2024 |
2.2.101 | 137 | 5/22/2024 |
2.2.100 | 163 | 5/17/2024 |
2.2.99 | 124 | 5/17/2024 |
2.2.98 | 130 | 5/16/2024 |
2.2.97 | 137 | 5/15/2024 |
2.2.96 | 97 | 5/14/2024 |
2.2.95 | 120 | 5/13/2024 |
2.2.94 | 434 | 5/11/2024 |
2.2.93 | 130 | 5/7/2024 |
2.2.92 | 125 | 5/7/2024 |
2.2.91 | 148 | 5/3/2024 |
2.2.90 | 91 | 5/3/2024 |
2.2.89 | 79 | 5/2/2024 |
2.2.88 | 90 | 5/2/2024 |
2.2.87 | 309 | 4/26/2024 |
2.2.86 | 136 | 4/26/2024 |
2.2.85 | 419 | 4/18/2024 |
2.2.84 | 119 | 4/18/2024 |
2.2.83 | 152 | 4/16/2024 |
2.2.82 | 482 | 4/8/2024 |
2.2.81 | 133 | 4/8/2024 |
2.2.80 | 143 | 4/7/2024 |
2.2.79 | 137 | 4/6/2024 |
2.2.78 | 136 | 4/5/2024 |
2.2.77 | 146 | 4/5/2024 |
2.2.76 | 144 | 4/4/2024 |
2.2.75 | 127 | 4/4/2024 |
2.2.73 | 126 | 4/3/2024 |
2.2.72 | 130 | 4/3/2024 |
2.2.71 | 152 | 4/3/2024 |
2.2.70 | 129 | 4/2/2024 |
2.2.69 | 320 | 4/1/2024 |
2.2.68 | 159 | 3/29/2024 |
2.2.67 | 292 | 3/27/2024 |
2.2.66 | 183 | 3/24/2024 |
2.2.65 | 151 | 3/21/2024 |
2.2.64 | 210 | 3/11/2024 |
2.2.63 | 176 | 3/9/2024 |
2.2.62 | 141 | 3/7/2024 |
2.2.61 | 146 | 3/6/2024 |
2.2.60 | 137 | 3/6/2024 |
2.2.58 | 186 | 3/2/2024 |
2.2.57 | 663 | 2/24/2024 |
2.2.56 | 172 | 2/18/2024 |
2.2.55 | 129 | 2/17/2024 |
2.2.53 | 135 | 2/15/2024 |
2.2.52 | 148 | 2/15/2024 |
2.2.51 | 131 | 2/15/2024 |
2.2.50 | 120 | 2/13/2024 |
2.2.49 | 891 | 2/2/2024 |
2.2.48 | 1,394 | 12/29/2023 |
2.2.47 | 186 | 12/20/2023 |
2.2.46 | 198 | 12/15/2023 |
2.2.45 | 159 | 12/10/2023 |
2.2.44 | 139 | 12/10/2023 |
2.2.42 | 152 | 12/9/2023 |
2.2.41 | 155 | 12/9/2023 |
2.2.40 | 130 | 12/8/2023 |
2.2.38 | 1,184 | 11/21/2023 |
2.2.37 | 458 | 11/16/2023 |
2.2.36 | 127 | 11/16/2023 |
2.2.35 | 163 | 11/14/2023 |
2.2.34 | 113 | 11/13/2023 |
2.2.33 | 85 | 11/10/2023 |
2.2.32 | 103 | 11/10/2023 |
2.2.31 | 83 | 11/9/2023 |
2.2.28 | 97 | 11/7/2023 |
2.2.27 | 156 | 10/31/2023 |
2.2.26 | 212 | 10/22/2023 |
2.2.25 | 104 | 10/20/2023 |
2.2.24 | 92 | 10/20/2023 |
2.2.23 | 97 | 10/20/2023 |
2.2.22 | 90 | 10/20/2023 |
2.2.21 | 100 | 10/20/2023 |
2.2.20 | 95 | 10/19/2023 |
2.2.19 | 85 | 10/19/2023 |
2.2.18 | 87 | 10/19/2023 |
2.2.17 | 191 | 10/13/2023 |
2.2.16 | 484 | 10/12/2023 |
2.2.15 | 90 | 10/12/2023 |
2.2.14 | 133 | 10/5/2023 |
2.2.13 | 111 | 10/5/2023 |
2.2.12 | 88 | 10/5/2023 |
2.2.11 | 252 | 10/3/2023 |
2.2.10 | 218 | 9/18/2023 |
2.2.9 | 86 | 9/18/2023 |
2.2.8 | 278 | 9/14/2023 |
2.2.7 | 111 | 9/13/2023 |
2.2.6 | 6,497 | 9/6/2023 |
2.2.5 | 131 | 8/30/2023 |
2.2.4 | 165 | 8/26/2023 |
2.2.3 | 125 | 8/20/2023 |
2.2.2 | 112 | 8/18/2023 |
2.2.1 | 120 | 8/11/2023 |
2.2.0 | 206 | 7/17/2023 |
2.1.15 | 126 | 5/26/2023 |
2.1.14 | 105 | 5/20/2023 |
2.1.13 | 125 | 4/26/2023 |
2.1.12 | 187 | 4/21/2023 |
2.1.11 | 112 | 4/19/2023 |
2.1.10 | 119 | 4/19/2023 |
2.1.8 | 152 | 4/10/2023 |
2.1.7 | 173 | 3/27/2023 |
2.1.6 | 165 | 3/24/2023 |
2.1.5 | 137 | 3/23/2023 |
2.1.4 | 135 | 3/23/2023 |
2.1.3 | 137 | 3/23/2023 |
2.1.2 | 141 | 3/21/2023 |
2.1.0 | 137 | 3/21/2023 |
2.0.3 | 140 | 3/21/2023 |
2.0.2 | 137 | 3/20/2023 |
2.0.1 | 142 | 3/20/2023 |
2.0.0 | 143 | 3/20/2023 |
1.9.2 | 149 | 3/14/2023 |
1.8.1 | 136 | 3/11/2023 |
1.8.0 | 132 | 3/10/2023 |
1.7.1 | 139 | 3/10/2023 |
1.7.0 | 135 | 3/8/2023 |
1.6.4 | 152 | 3/1/2023 |
1.6.3 | 359 | 1/31/2023 |
1.6.2 | 373 | 1/24/2023 |
1.6.1 | 397 | 1/11/2023 |
1.6.0 | 392 | 1/11/2023 |
1.5.0 | 453 | 12/23/2022 |
1.4.0 | 407 | 12/20/2022 |
1.3.0 | 409 | 12/16/2022 |
1.2.7 | 352 | 12/16/2022 |
1.2.5 | 367 | 12/14/2022 |
1.2.4.1 | 350 | 12/13/2022 |
1.2.4 | 350 | 12/13/2022 |
1.2.3 | 348 | 12/13/2022 |
1.2.1 | 336 | 12/10/2022 |
1.2.0 | 337 | 12/10/2022 |
1.1.0 | 331 | 12/6/2022 |
1.0.0 | 335 | 12/6/2022 |