KristofferStrube.Blazor.WebIDL 0.6.0

Prefix Reserved
dotnet add package KristofferStrube.Blazor.WebIDL --version 0.6.0                
NuGet\Install-Package KristofferStrube.Blazor.WebIDL -Version 0.6.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KristofferStrube.Blazor.WebIDL" Version="0.6.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KristofferStrube.Blazor.WebIDL --version 0.6.0                
#r "nuget: KristofferStrube.Blazor.WebIDL, 0.6.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install KristofferStrube.Blazor.WebIDL as a Cake Addin
#addin nuget:?package=KristofferStrube.Blazor.WebIDL&version=0.6.0

// Install KristofferStrube.Blazor.WebIDL as a Cake Tool
#tool nuget:?package=KristofferStrube.Blazor.WebIDL&version=0.6.0                

License: MIT GitHub issues GitHub forks GitHub stars NuGet Downloads (official NuGet)

Blazor.WebIDL

A Blazor wrapper for types and interfaces used in and defined in the standard WebIDL specification. Among these are declarations that define specific behavior for interfaces and the standard exception definition types used across all WebIDL based APIs.

Demo

The sample project can be demoed at https://kristofferstrube.github.io/Blazor.WebIDL/

On each page you can find the corresponding code for the example in the top right corner.

Exceptions

The specification defines the types and names for all the standard exceptions and the standard names for DomExceptions.

It also provides a way to make JSInterop calls that automatically throw these typed errors when a standard exception is thrown in JS.

Setting it up

In Programs.cs, we can inject a service to make Error Handling JSInterop easy in each of our pages/components. In Blazor WASM, we additionally need to call a function in Program.cs before being able to use Error Handling JSInterop. This is only needed for WASM, as JSInterop in WASM can return IJSObjectReferences synchronously.

var builder = WebAssemblyHostBuilder.CreateDefault(args);

// Setting up other services before this.
builder.Services.AddErrorHandlingJSRuntime();

var app = builder.Build();

// For Blazor WASM you need to call this to make Error Handling JSInterop.
await app.Services.SetupErrorHandlingJSInterop();

await app.RunAsync();

Error Handling JSInterop calls in a page.

This can be used to catch strongly typed JS errors from Blazor. An example could be trying to access the clipboard which can fail in many ways.

@using KristofferStrube.Blazor.WebIDL.Exceptions;
@inject IErrorHandlingJSRuntime ErrorHandlingJSRuntime
@inject ILogger Logger

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender) return;
        try
        {
            var result = await ErrorHandlingJSRuntime.InvokeAsync<string>("navigator.clipboard.readText");
            Console.WriteLine(result);
        }
        catch (NotAllowedErrorException exception)
        {
            Logger.LogWarning(exception, "The user has not given permission to read the clipboard.");
        }
        catch (DOMException exception)
        {
            Logger.LogError(exception, $"{exception.Name} (which is a DOMException): \"{exception.Message}\"");
        }
        catch (WebIDLException exception)
        {
            Logger.LogError(exception, $"{exception.GetType().Name}: \"{exception.Message}\"");
        }
    }
}

Standard behavior and method definitions

The standard WebIDL specification make definitions that are used across all API specifications that use WebIDL for defining their interfaces. Declarations like Iterable, Asynchronously iterable, Maplike, Setlike define expected behavior and methods that should apply to the interfaces that use these definitions. This wrapper defines C# interfaces for these declarations that have implementations for the expected methods. This makes it easy and safe to implement wrappers for interfaces that define i.e. Setlike behavior.

We have currently implemented 2 interfaces for the Setlike declaration called IReadonlySetlike and IReadWriteSetlike where IReadWriteSetlike inherits from IReadonlySetlike

They can be used like this to make a very simple wrapper for the JS Set type:

[IJSWrapperConverter]
public class Set : IReadWriteSetlike<Set>
{
    /// <inheritdoc/>
    public IJSObjectReference JSReference { get; }
    /// <inheritdoc/>
    public IJSRuntime JSRuntime { get; }
    /// <inheritdoc/>
    public bool DisposesJSReference { get; }

    public static async Task<Set> CreateAsync<T>(IJSRuntime jSRuntime, IEnumerable<T>? iterable = null)
    {
        var jSInstance = await jSRuntime.InvokeAsync<IJSObjectReference>("constructSet", iterable);
        return new Set(jSRuntime, jSInstance, new() { DisposesJSReference = true });
    }

    public Set(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options)
    {
        JSRuntime = jSRuntime;
        JSReference = jSReference;
        DisposesJSReference = options.DisposesJSReference;
    }

    /// <inheritdoc/>
    public async ValueTask DisposeAsync()
    {
        await IJSWrapper.DisposeJSReference(this);
        GC.SuppressFinalize(this);
    }
}

Issues

Feel free to open issues on the repository if you find any errors with the package or have wishes for features. Read CONTRIBUTING.md for details on how to best contribute to the repository.

This project is used in the following projects:

And it is planned to be used in these projects:

How this project was developed is explored in this article from my blog:

This repository was build with inspiration and help from the following series of articles:

Product 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on KristofferStrube.Blazor.WebIDL:

Package Downloads
KristofferStrube.Blazor.DOM

DOM API wrapper implementation for Blazor.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.0 587 10/23/2024
0.5.1 976 6/23/2024
0.5.0 7,463 3/9/2024
0.4.0 130 2/22/2024
0.3.0 192 1/4/2024
0.2.0 2,549 8/15/2023
0.1.0 1,070 6/21/2023
0.1.0-alpha.22 96 6/19/2023
0.1.0-alpha.21 81 6/8/2023
0.1.0-alpha.20 98 6/8/2023
0.1.0-alpha.19 84 6/7/2023
0.1.0-alpha.18 80 6/7/2023
0.1.0-alpha.17 112 5/31/2023
0.1.0-alpha.16 88 5/29/2023
0.1.0-alpha.15 80 5/29/2023
0.1.0-alpha.14 75 5/29/2023
0.1.0-alpha.13 77 5/29/2023
0.1.0-alpha.12 77 5/29/2023
0.1.0-alpha.11 81 5/28/2023
0.1.0-alpha.10 73 5/25/2023
0.1.0-alpha.9 98 5/20/2023
0.1.0-alpha.8 74 5/20/2023
0.1.0-alpha.7 82 5/20/2023
0.1.0-alpha.6 84 5/9/2023
0.1.0-alpha.5 97 4/12/2023
0.1.0-alpha.4 135 4/10/2023
0.1.0-alpha.3 99 4/10/2023
0.1.0-alpha.2 117 3/13/2023
0.1.0-alpha.1 30,485 2/28/2023