RSCG – Blazor.TSRuntime
| name | Blazor.TSRuntime |
| nuget | https://www.nuget.org/packages/Blazor.TSRuntime/ |
| link | https://github.com/BlackWhiteYoshi/Blazor.TSRuntime |
| author | Black White Yoshi |
Generating C# Code for Blazor from javascript files
This is how you can use Blazor.TSRuntime .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazor.TSRuntime" Version="1.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<AdditionalFiles Include="tsruntime.json" />
<AdditionalFiles Include="**/*.js" Exclude="bin/**;obj/**;Properties/**" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.8" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
using BlazorData;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.JSInterop;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddTSRuntime();
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
{
"invoke function": {
"sync enabled": false,
"trysync enabled": true,
"async enabled": false,
"name pattern": {
"pattern": "#function#",
"module transform": "first upper case",
"function transform": "first upper case",
"action transform": "none"
},
"type map": {
"number": {
"type": "TNumber",
"generic types": {
"name": "TNumber",
"constraint": "INumber<TNumber>"
}
},
"boolean": "bool",
"Uint8Array": "byte[]",
"HTMLElement": "ElementReference"
}
}
}
@page "/"
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
<a onclick="@SayHello">Click</a>
@code {
[Inject]
IJSRuntime JS { get; set; }
[Inject]
public required ITSRuntime TsRuntime { private get; init; }
public async Task SayHello()
{
Console.WriteLine("Hello from Blazor");
await TsRuntime.SayHello("Andrei");
}
}
/**
* Displays a greeting alert with the provided name.
* @param {string} name - The name to include in the greeting.
*/
export function SayHello(name) {
alert("Hello from JavaScript," + name);
}
The code that is generated is
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Numerics;
namespace Microsoft.JSInterop;
public partial interface ITSRuntime {
protected Task<IJSObjectReference> GetbootstrapModule();
/// <summary>
/// <para>Loads 'bootstrap' (/wwwroot/lib/bootstrap/dist/js/bootstrap.js) as javascript-module.</para>
/// <para>If already loading, it does not trigger a second loading and if already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when the module import have completed.</returns>
public Task PreloadBootstrap() => GetbootstrapModule();
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Numerics;
namespace Microsoft.JSInterop;
public partial interface ITSRuntime {
protected Task<IJSObjectReference> Getbootstrap_bundleModule();
/// <summary>
/// <para>Loads 'bootstrap_bundle' (/wwwroot/lib/bootstrap/dist/js/bootstrap.bundle.js) as javascript-module.</para>
/// <para>If already loading, it does not trigger a second loading and if already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when the module import have completed.</returns>
public Task PreloadBootstrap_bundle() => Getbootstrap_bundleModule();
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Numerics;
namespace Microsoft.JSInterop;
public partial interface ITSRuntime {
protected Task<IJSObjectReference> Getbootstrap_bundle_minModule();
/// <summary>
/// <para>Loads 'bootstrap_bundle_min' (/wwwroot/lib/bootstrap/dist/js/bootstrap.bundle.min.js) as javascript-module.</para>
/// <para>If already loading, it does not trigger a second loading and if already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when the module import have completed.</returns>
public Task PreloadBootstrap_bundle_min() => Getbootstrap_bundle_minModule();
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Numerics;
namespace Microsoft.JSInterop;
public partial interface ITSRuntime {
protected Task<IJSObjectReference> Getbootstrap_esmModule();
/// <summary>
/// <para>Loads 'bootstrap_esm' (/wwwroot/lib/bootstrap/dist/js/bootstrap.esm.js) as javascript-module.</para>
/// <para>If already loading, it does not trigger a second loading and if already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when the module import have completed.</returns>
public Task PreloadBootstrap_esm() => Getbootstrap_esmModule();
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Numerics;
namespace Microsoft.JSInterop;
public partial interface ITSRuntime {
protected Task<IJSObjectReference> Getbootstrap_esm_minModule();
/// <summary>
/// <para>Loads 'bootstrap_esm_min' (/wwwroot/lib/bootstrap/dist/js/bootstrap.esm.min.js) as javascript-module.</para>
/// <para>If already loading, it does not trigger a second loading and if already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when the module import have completed.</returns>
public Task PreloadBootstrap_esm_min() => Getbootstrap_esm_minModule();
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Numerics;
namespace Microsoft.JSInterop;
public partial interface ITSRuntime {
protected Task<IJSObjectReference> Getbootstrap_minModule();
/// <summary>
/// <para>Loads 'bootstrap_min' (/wwwroot/lib/bootstrap/dist/js/bootstrap.min.js) as javascript-module.</para>
/// <para>If already loading, it does not trigger a second loading and if already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when the module import have completed.</returns>
public Task PreloadBootstrap_min() => Getbootstrap_minModule();
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.JSInterop;
/// <summary>
/// <para>Interface for JS-interop.</para>
/// <para>It contains an invoke-method for every js-function, a preload-method for every module and a method to load all modules.</para>
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Blazor.TSRuntime", "1.0.1")]
public partial interface ITSRuntime {
/// <summary>
/// <para>Fetches all modules as javascript-modules.</para>
/// <para>If already loading, it doesn't trigger a second loading and if any already loaded, these are not loaded again, so if all already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when all module loading Tasks have completed.</returns>
public Task PreloadAllModules();
/// <summary>
/// <para>Invokes the specified JavaScript function synchronously.</para>
/// <para>If module is not loaded or synchronous is not supported, it fails with an exception.</para>
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="identifier">name of the javascript function</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <returns></returns>
protected TResult TSInvoke<TResult>(string identifier, object?[]? args);
/// <summary>
/// Invokes the specified JavaScript function synchronously when supported, otherwise asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="identifier">name of the javascript function</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeTrySync<TResult>(string identifier, object?[]? args, CancellationToken cancellationToken);
/// <summary>
/// Invokes the specified JavaScript function asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="identifier">name of the javascript function</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeAsync<TResult>(string identifier, object?[]? args, CancellationToken cancellationToken);
/// <summary>
/// <para>Invokes the specified JavaScript function in the specified module synchronously.</para>
/// <para>If module is not loaded or synchronous is not supported, it fails with an exception.</para>
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="moduleTask">The loading task of a module</param>
/// <param name="identifier">name of the javascript function</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <returns></returns>
protected TResult TSInvoke<TResult>(Task<IJSObjectReference> moduleTask, string identifier, object?[]? args);
/// <summary>
/// Invokes the specified JavaScript function in the specified module synchronously when supported, otherwise asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="moduleTask">The loading task of a module</param>
/// <param name="identifier">name of the javascript function</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeTrySync<TResult>(Task<IJSObjectReference> moduleTask, string identifier, object?[]? args, CancellationToken cancellationToken);
/// <summary>
/// Invokes the specified JavaScript function in the specified module asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="moduleTask">The loading task of a module</param>
/// <param name="identifier">name of the javascript function</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeAsync<TResult>(Task<IJSObjectReference> moduleTask, string identifier, object?[]? args, CancellationToken cancellationToken);
/// <summary>
/// <para>Invokes the specified JavaScript function synchronously.</para>
/// <para>If module is not loaded or synchronous is not supported, it fails with an exception.</para>
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TCallback"></typeparam>
/// <param name="identifier">name of the javascript function</param>
/// <param name="dotNetObjectReference">reference to a csharp object with callback functions</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <returns></returns>
protected TResult TSInvoke<TResult, TCallback>(string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args) where TCallback : class;
/// <summary>
/// Invokes the specified JavaScript function synchronously when supported, otherwise asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TCallback"></typeparam>
/// <param name="identifier">name of the javascript function</param>
/// <param name="dotNetObjectReference">reference to a csharp object with callback functions</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeTrySync<TResult, TCallback>(string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class;
/// <summary>
/// Invokes the specified JavaScript function asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TCallback"></typeparam>
/// <param name="identifier">name of the javascript function</param>
/// <param name="dotNetObjectReference">reference to a csharp object with callback functions</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeAsync<TResult, TCallback>(string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class;
/// <summary>
/// <para>Invokes the specified JavaScript function in the specified module synchronously.</para>
/// <para>If module is not loaded or synchronous is not supported, it fails with an exception.</para>
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TCallback"></typeparam>
/// <param name="moduleTask">The loading task of a module</param>
/// <param name="identifier">name of the javascript function</param>
/// <param name="dotNetObjectReference">reference to a csharp object with callback functions</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <returns></returns>
protected TResult TSInvoke<TResult, TCallback>(Task<IJSObjectReference> moduleTask, string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args) where TCallback : class;
/// <summary>
/// Invokes the specified JavaScript function in the specified module synchronously when supported, otherwise asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TCallback"></typeparam>
/// <param name="moduleTask">The loading task of a module</param>
/// <param name="identifier">name of the javascript function</param>
/// <param name="dotNetObjectReference">reference to a csharp object with callback functions</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeTrySync<TResult, TCallback>(Task<IJSObjectReference> moduleTask, string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class;
/// <summary>
/// Invokes the specified JavaScript function in the specified module asynchronously.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TCallback"></typeparam>
/// <param name="moduleTask">The loading task of a module</param>
/// <param name="identifier">name of the javascript function</param>
/// <param name="dotNetObjectReference">reference to a csharp object with callback functions</param>
/// <param name="args">parameter passing to the JS-function</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns></returns>
protected ValueTask<TResult> TSInvokeAsync<TResult, TCallback>(Task<IJSObjectReference> moduleTask, string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class;
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Numerics;
namespace Microsoft.JSInterop;
public partial interface ITSRuntime {
protected Task<IJSObjectReference> GetHomeModule();
/// <summary>
/// <para>Loads 'Home' (/Pages/Home.razor.js) as javascript-module.</para>
/// <para>If already loading, it does not trigger a second loading and if already loaded, it returns a completed task.</para>
/// </summary>
/// <returns>A Task that will complete when the module import have completed.</returns>
public Task PreloadHome() => GetHomeModule();
/// <summary>
/// <para>Displays a greeting alert with the provided name.</para>
/// <para>Invokes in module 'Home' the JS-function 'SayHello' synchronously when supported, otherwise asynchronously.</para>
/// </summary>
/// <param name="name">The name to include in the greeting.</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.</param>
/// <returns>A Task that will complete when the JS-Function have completed.</returns>
public async ValueTask SayHello(string name, CancellationToken cancellationToken = default) {
await TSInvokeTrySync<Infrastructure.IJSVoidResult>(GetHomeModule(), "SayHello", [name], cancellationToken);
}
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.JSInterop;
/// <summary>
/// <para>An implementation for <see cref="ITSRuntime"/>.</para>
/// <para>It manages JS-modules: It loads the modules, caches it in an array and disposing releases all modules.</para>
/// <para>
/// There are 7 modules available:<br />
/// - Home<br />
/// - bootstrap_bundle<br />
/// - bootstrap_bundle_min<br />
/// - bootstrap_esm<br />
/// - bootstrap_esm_min<br />
/// - bootstrap<br />
/// - bootstrap_min
/// </para>
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Blazor.TSRuntime", "1.0.1")]
public sealed class TSRuntime(IJSRuntime jsRuntime) : ITSRuntime, IDisposable, IAsyncDisposable {
private readonly CancellationTokenSource cancellationTokenSource = new();
Task<IJSObjectReference> ITSRuntime.GetHomeModule() => GetHomeModule();
private Task<IJSObjectReference>? HomeModule;
private Task<IJSObjectReference> GetHomeModule()
=> HomeModule switch {
Task<IJSObjectReference> { IsCompletedSuccessfully: true }
or Task<IJSObjectReference> { IsCompleted: false } => HomeModule,
_ => HomeModule = jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationTokenSource.Token, "/Pages/Home.razor.js").AsTask()
};
Task<IJSObjectReference> ITSRuntime.Getbootstrap_bundleModule() => Getbootstrap_bundleModule();
private Task<IJSObjectReference>? bootstrap_bundleModule;
private Task<IJSObjectReference> Getbootstrap_bundleModule()
=> bootstrap_bundleModule switch {
Task<IJSObjectReference> { IsCompletedSuccessfully: true }
or Task<IJSObjectReference> { IsCompleted: false } => bootstrap_bundleModule,
_ => bootstrap_bundleModule = jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationTokenSource.Token, "/wwwroot/lib/bootstrap/dist/js/bootstrap.bundle.js").AsTask()
};
Task<IJSObjectReference> ITSRuntime.Getbootstrap_bundle_minModule() => Getbootstrap_bundle_minModule();
private Task<IJSObjectReference>? bootstrap_bundle_minModule;
private Task<IJSObjectReference> Getbootstrap_bundle_minModule()
=> bootstrap_bundle_minModule switch {
Task<IJSObjectReference> { IsCompletedSuccessfully: true }
or Task<IJSObjectReference> { IsCompleted: false } => bootstrap_bundle_minModule,
_ => bootstrap_bundle_minModule = jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationTokenSource.Token, "/wwwroot/lib/bootstrap/dist/js/bootstrap.bundle.min.js").AsTask()
};
Task<IJSObjectReference> ITSRuntime.Getbootstrap_esmModule() => Getbootstrap_esmModule();
private Task<IJSObjectReference>? bootstrap_esmModule;
private Task<IJSObjectReference> Getbootstrap_esmModule()
=> bootstrap_esmModule switch {
Task<IJSObjectReference> { IsCompletedSuccessfully: true }
or Task<IJSObjectReference> { IsCompleted: false } => bootstrap_esmModule,
_ => bootstrap_esmModule = jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationTokenSource.Token, "/wwwroot/lib/bootstrap/dist/js/bootstrap.esm.js").AsTask()
};
Task<IJSObjectReference> ITSRuntime.Getbootstrap_esm_minModule() => Getbootstrap_esm_minModule();
private Task<IJSObjectReference>? bootstrap_esm_minModule;
private Task<IJSObjectReference> Getbootstrap_esm_minModule()
=> bootstrap_esm_minModule switch {
Task<IJSObjectReference> { IsCompletedSuccessfully: true }
or Task<IJSObjectReference> { IsCompleted: false } => bootstrap_esm_minModule,
_ => bootstrap_esm_minModule = jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationTokenSource.Token, "/wwwroot/lib/bootstrap/dist/js/bootstrap.esm.min.js").AsTask()
};
Task<IJSObjectReference> ITSRuntime.GetbootstrapModule() => GetbootstrapModule();
private Task<IJSObjectReference>? bootstrapModule;
private Task<IJSObjectReference> GetbootstrapModule()
=> bootstrapModule switch {
Task<IJSObjectReference> { IsCompletedSuccessfully: true }
or Task<IJSObjectReference> { IsCompleted: false } => bootstrapModule,
_ => bootstrapModule = jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationTokenSource.Token, "/wwwroot/lib/bootstrap/dist/js/bootstrap.js").AsTask()
};
Task<IJSObjectReference> ITSRuntime.Getbootstrap_minModule() => Getbootstrap_minModule();
private Task<IJSObjectReference>? bootstrap_minModule;
private Task<IJSObjectReference> Getbootstrap_minModule()
=> bootstrap_minModule switch {
Task<IJSObjectReference> { IsCompletedSuccessfully: true }
or Task<IJSObjectReference> { IsCompleted: false } => bootstrap_minModule,
_ => bootstrap_minModule = jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationTokenSource.Token, "/wwwroot/lib/bootstrap/dist/js/bootstrap.min.js").AsTask()
};
public Task PreloadAllModules() {
GetHomeModule();
Getbootstrap_bundleModule();
Getbootstrap_bundle_minModule();
Getbootstrap_esmModule();
Getbootstrap_esm_minModule();
GetbootstrapModule();
Getbootstrap_minModule();
return Task.WhenAll([HomeModule!, bootstrap_bundleModule!, bootstrap_bundle_minModule!, bootstrap_esmModule!, bootstrap_esm_minModule!, bootstrapModule!, bootstrap_minModule!]);
}
TResult ITSRuntime.TSInvoke<TResult>(string identifier, object?[]? args) => ((IJSInProcessRuntime)jsRuntime).Invoke<TResult>(identifier, args);
ValueTask<TResult> ITSRuntime.TSInvokeTrySync<TResult>(string identifier, object?[]? args, CancellationToken cancellationToken) {
if (jsRuntime is IJSInProcessRuntime jsInProcessRuntime)
return ValueTask.FromResult(jsInProcessRuntime.Invoke<TResult>(identifier, args));
else
return jsRuntime.InvokeAsync<TResult>(identifier, cancellationToken, args);
}
ValueTask<TResult> ITSRuntime.TSInvokeAsync<TResult>(string identifier, object?[]? args, CancellationToken cancellationToken)
=> jsRuntime.InvokeAsync<TResult>(identifier, cancellationToken, args);
TResult ITSRuntime.TSInvoke<TResult>(Task<IJSObjectReference> moduleTask, string identifier, object?[]? args) {
if (!moduleTask.IsCompletedSuccessfully)
throw new JSException("JS-module is not loaded. Use and await the Preload()-method to ensure the module is loaded.");
return ((IJSInProcessObjectReference)moduleTask.Result).Invoke<TResult>(identifier, args);
}
async ValueTask<TResult> ITSRuntime.TSInvokeTrySync<TResult>(Task<IJSObjectReference> moduleTask, string identifier, object?[]? args, CancellationToken cancellationToken) {
IJSObjectReference module = await moduleTask;
if (module is IJSInProcessObjectReference inProcessModule)
return inProcessModule.Invoke<TResult>(identifier, args);
else
return await module.InvokeAsync<TResult>(identifier, cancellationToken, args);
}
async ValueTask<TResult> ITSRuntime.TSInvokeAsync<TResult>(Task<IJSObjectReference> moduleTask, string identifier, object?[]? args, CancellationToken cancellationToken) {
IJSObjectReference module = await moduleTask;
return await module.InvokeAsync<TResult>(identifier, cancellationToken, args);
}
TResult ITSRuntime.TSInvoke<TResult, TCallback>(string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args) where TCallback : class => default; // no callbacks are used
ValueTask<TResult> ITSRuntime.TSInvokeTrySync<TResult, TCallback>(string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class => default; // no callbacks are used
ValueTask<TResult> ITSRuntime.TSInvokeAsync<TResult, TCallback>(string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class => default; // no callbacks are used
TResult ITSRuntime.TSInvoke<TResult, TCallback>(Task<IJSObjectReference> moduleTask, string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args) where TCallback : class => default; // no callbacks are used
ValueTask<TResult> ITSRuntime.TSInvokeTrySync<TResult, TCallback>(Task<IJSObjectReference> moduleTask, string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class => default; // no callbacks are used
ValueTask<TResult> ITSRuntime.TSInvokeAsync<TResult, TCallback>(Task<IJSObjectReference> moduleTask, string identifier, DotNetObjectReference<TCallback> dotNetObjectReference, object?[]? args, CancellationToken cancellationToken) where TCallback : class => default; // no callbacks are used
/// <summary>
/// Releases each module synchronously if possible, otherwise asynchronously per fire and forget.
/// </summary>
public void Dispose() {
if (cancellationTokenSource.IsCancellationRequested)
return;
cancellationTokenSource.Cancel();
cancellationTokenSource.Dispose();
if (HomeModule?.IsCompletedSuccessfully == true)
if (HomeModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else
_ = HomeModule.Result.DisposeAsync().Preserve();
HomeModule = null;
if (bootstrap_bundleModule?.IsCompletedSuccessfully == true)
if (bootstrap_bundleModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else
_ = bootstrap_bundleModule.Result.DisposeAsync().Preserve();
bootstrap_bundleModule = null;
if (bootstrap_bundle_minModule?.IsCompletedSuccessfully == true)
if (bootstrap_bundle_minModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else
_ = bootstrap_bundle_minModule.Result.DisposeAsync().Preserve();
bootstrap_bundle_minModule = null;
if (bootstrap_esmModule?.IsCompletedSuccessfully == true)
if (bootstrap_esmModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else
_ = bootstrap_esmModule.Result.DisposeAsync().Preserve();
bootstrap_esmModule = null;
if (bootstrap_esm_minModule?.IsCompletedSuccessfully == true)
if (bootstrap_esm_minModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else
_ = bootstrap_esm_minModule.Result.DisposeAsync().Preserve();
bootstrap_esm_minModule = null;
if (bootstrapModule?.IsCompletedSuccessfully == true)
if (bootstrapModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else
_ = bootstrapModule.Result.DisposeAsync().Preserve();
bootstrapModule = null;
if (bootstrap_minModule?.IsCompletedSuccessfully == true)
if (bootstrap_minModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else
_ = bootstrap_minModule.Result.DisposeAsync().Preserve();
bootstrap_minModule = null;
}
/// <summary>
/// <para>Releases each module synchronously if possible, otherwise asynchronously and returns a task that completes, when all module disposing tasks complete.</para>
/// <para>The asynchronous disposing tasks are happening in parallel.</para>
/// </summary>
/// <returns></returns>
public ValueTask DisposeAsync() {
if (cancellationTokenSource.IsCancellationRequested)
return ValueTask.CompletedTask;
cancellationTokenSource.Cancel();
cancellationTokenSource.Dispose();
List<Task> taskList = new(7);
if (HomeModule?.IsCompletedSuccessfully == true)
if (HomeModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else {
ValueTask valueTask = HomeModule.Result.DisposeAsync();
if (!valueTask.IsCompleted)
taskList.Add(valueTask.AsTask());
}
HomeModule = null;
if (bootstrap_bundleModule?.IsCompletedSuccessfully == true)
if (bootstrap_bundleModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else {
ValueTask valueTask = bootstrap_bundleModule.Result.DisposeAsync();
if (!valueTask.IsCompleted)
taskList.Add(valueTask.AsTask());
}
bootstrap_bundleModule = null;
if (bootstrap_bundle_minModule?.IsCompletedSuccessfully == true)
if (bootstrap_bundle_minModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else {
ValueTask valueTask = bootstrap_bundle_minModule.Result.DisposeAsync();
if (!valueTask.IsCompleted)
taskList.Add(valueTask.AsTask());
}
bootstrap_bundle_minModule = null;
if (bootstrap_esmModule?.IsCompletedSuccessfully == true)
if (bootstrap_esmModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else {
ValueTask valueTask = bootstrap_esmModule.Result.DisposeAsync();
if (!valueTask.IsCompleted)
taskList.Add(valueTask.AsTask());
}
bootstrap_esmModule = null;
if (bootstrap_esm_minModule?.IsCompletedSuccessfully == true)
if (bootstrap_esm_minModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else {
ValueTask valueTask = bootstrap_esm_minModule.Result.DisposeAsync();
if (!valueTask.IsCompleted)
taskList.Add(valueTask.AsTask());
}
bootstrap_esm_minModule = null;
if (bootstrapModule?.IsCompletedSuccessfully == true)
if (bootstrapModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else {
ValueTask valueTask = bootstrapModule.Result.DisposeAsync();
if (!valueTask.IsCompleted)
taskList.Add(valueTask.AsTask());
}
bootstrapModule = null;
if (bootstrap_minModule?.IsCompletedSuccessfully == true)
if (bootstrap_minModule.Result is IJSInProcessObjectReference inProcessModule)
inProcessModule.Dispose();
else {
ValueTask valueTask = bootstrap_minModule.Result.DisposeAsync();
if (!valueTask.IsCompleted)
taskList.Add(valueTask.AsTask());
}
bootstrap_minModule = null;
if (taskList.Count == 0)
return ValueTask.CompletedTask;
else
return new ValueTask(Task.WhenAll(taskList));
}
}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.JSInterop;
[System.CodeDom.Compiler.GeneratedCodeAttribute("Blazor.TSRuntime", "1.0.1")]
public static class TSRuntimeServiceExtension {
/// <summary>
/// Registers a scoped ITSRuntime with a TSRuntime as implementation and if available, registers the module interfaces with the same TSRuntime-object.
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddTSRuntime(this IServiceCollection services) {
services.AddScoped<ITSRuntime, TSRuntime>();
return services;
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Blazor.TSRuntime