using Microsoft.JSInterop; namespace BlueWest.Razor.Library; // This class provides an example of how JavaScript functionality can be wrapped // in a .NET class for easy consumption. The associated JavaScript module is // loaded on demand when first needed. // // This class can be registered as scoped DI service and then injected into Blazor // components for use. public class ExampleJsInterop : IAsyncDisposable { private readonly Lazy> moduleTask; public ExampleJsInterop(IJSRuntime jsRuntime) { moduleTask = new(() => jsRuntime.InvokeAsync( "import", "./_content/BlueWest.Razor.Library/exampleJsInterop.js").AsTask()); } public async ValueTask Prompt(string message) { var module = await moduleTask.Value; return await module.InvokeAsync("showPrompt", message); } public async ValueTask DisposeAsync() { if (moduleTask.IsValueCreated) { var module = await moduleTask.Value; await module.DisposeAsync(); } } }