| | | 1 | | using Microsoft.JSInterop; |
| | | 2 | | |
| | | 3 | | namespace Nabs.Tests.UiUnitTestsComponents |
| | | 4 | | { |
| | | 5 | | // This class provides an example of how JavaScript functionality can be wrapped |
| | | 6 | | // in a .NET class for easy consumption. The associated JavaScript module is |
| | | 7 | | // loaded on demand when first needed. |
| | | 8 | | // |
| | | 9 | | // This class can be registered as scoped DI service and then injected into Blazor |
| | | 10 | | // components for use. |
| | | 11 | | |
| | | 12 | | public class ExampleJsInterop : IAsyncDisposable |
| | | 13 | | { |
| | | 14 | | private readonly Lazy<Task<IJSObjectReference>> moduleTask; |
| | | 15 | | |
| | | 16 | | public ExampleJsInterop(IJSRuntime jsRuntime) |
| | | 17 | | { |
| | 0 | 18 | | moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>( |
| | 0 | 19 | | "import", "./_content/Nabs.Tests.UiUnitTestsComponents/exampleJsInterop.js").AsTask()); |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async ValueTask<string> Prompt(string message) |
| | | 23 | | { |
| | 0 | 24 | | var module = await moduleTask.Value; |
| | 0 | 25 | | return await module.InvokeAsync<string>("showPrompt", message); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async ValueTask DisposeAsync() |
| | | 29 | | { |
| | 0 | 30 | | if (moduleTask.IsValueCreated) |
| | | 31 | | { |
| | 0 | 32 | | var module = await moduleTask.Value; |
| | 0 | 33 | | await module.DisposeAsync(); |
| | | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | } |
| | | 37 | | } |