| | | 1 | | using Microsoft.JSInterop; |
| | | 2 | | |
| | | 3 | | namespace Pomodoro.Web.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Service for JavaScript interop operations to enable mocking |
| | | 7 | | /// </summary> |
| | | 8 | | public interface IJSInteropService |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Invokes a JavaScript function that doesn't return a value |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="identifier">The identifier of the function to invoke</param> |
| | | 14 | | /// <param name="args">Arguments to pass to the function</param> |
| | | 15 | | Task InvokeVoidAsync(string identifier, params object[] args); |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Invokes a JavaScript function that returns a value |
| | | 19 | | /// </summary> |
| | | 20 | | /// <typeparam name="TValue">The return type</typeparam> |
| | | 21 | | /// <param name="identifier">The identifier of the function to invoke</param> |
| | | 22 | | /// <param name="args">Arguments to pass to the function</param> |
| | | 23 | | Task<TValue> InvokeAsync<TValue>(string identifier, params object[] args); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Default implementation of IJSInteropService that delegates to IJSRuntime |
| | | 28 | | /// </summary> |
| | | 29 | | public class JSInteropService : IJSInteropService |
| | | 30 | | { |
| | | 31 | | private readonly IJSRuntime _jsRuntime; |
| | | 32 | | |
| | 10 | 33 | | public JSInteropService(IJSRuntime jsRuntime) |
| | 10 | 34 | | { |
| | 10 | 35 | | _jsRuntime = jsRuntime; |
| | 10 | 36 | | } |
| | | 37 | | |
| | | 38 | | public Task InvokeVoidAsync(string identifier, params object[] args) |
| | 2 | 39 | | { |
| | 2 | 40 | | return _jsRuntime.InvokeVoidAsync(identifier, args).AsTask(); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | public Task<TValue> InvokeAsync<TValue>(string identifier, params object[] args) |
| | 5 | 44 | | { |
| | 5 | 45 | | return _jsRuntime.InvokeAsync<TValue>(identifier, args).AsTask(); |
| | 5 | 46 | | } |
| | | 47 | | } |