| | | 1 | | using Microsoft.JSInterop; |
| | | 2 | | |
| | | 3 | | namespace Pomodoro.Web.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Implementation of infinite scroll JavaScript interop operations. |
| | | 7 | | /// Wraps IJSRuntime calls to enable mocking in unit tests. |
| | | 8 | | /// </summary> |
| | | 9 | | public class InfiniteScrollInterop : IInfiniteScrollInterop |
| | | 10 | | { |
| | | 11 | | private readonly IJSRuntime _jsRuntime; |
| | | 12 | | |
| | 18 | 13 | | public InfiniteScrollInterop(IJSRuntime jsRuntime) |
| | 18 | 14 | | { |
| | 18 | 15 | | _jsRuntime = jsRuntime; |
| | 18 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Checks if the Intersection Observer API is supported in the browser. |
| | | 20 | | /// </summary> |
| | | 21 | | public async Task<bool> IsSupportedAsync() |
| | 3 | 22 | | { |
| | | 23 | | try |
| | 3 | 24 | | { |
| | 3 | 25 | | return await _jsRuntime.InvokeAsync<bool>("infiniteScroll.isSupported"); |
| | | 26 | | } |
| | 1 | 27 | | catch (JSException) |
| | 1 | 28 | | { |
| | 1 | 29 | | return false; |
| | | 30 | | } |
| | 3 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Creates an Intersection Observer for the infinite scroll sentinel element. |
| | | 35 | | /// </summary> |
| | | 36 | | public async Task<bool> CreateObserverAsync( |
| | | 37 | | string sentinelId, |
| | | 38 | | DotNetObjectReference<object> dotNetRef, |
| | | 39 | | string containerId, |
| | | 40 | | string rootMargin, |
| | | 41 | | int timeoutMs) |
| | 4 | 42 | | { |
| | | 43 | | try |
| | 4 | 44 | | { |
| | 4 | 45 | | return await _jsRuntime.InvokeAsync<bool>( |
| | 4 | 46 | | "infiniteScroll.createObserver", |
| | 4 | 47 | | sentinelId, |
| | 4 | 48 | | dotNetRef, |
| | 4 | 49 | | containerId, |
| | 4 | 50 | | rootMargin, |
| | 4 | 51 | | timeoutMs); |
| | | 52 | | } |
| | 1 | 53 | | catch (JSException) |
| | 1 | 54 | | { |
| | 1 | 55 | | return false; |
| | | 56 | | } |
| | 4 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Destroys the Intersection Observer for a specific sentinel element. |
| | | 61 | | /// </summary> |
| | | 62 | | public async Task DestroyObserverAsync(string sentinelId) |
| | 3 | 63 | | { |
| | | 64 | | try |
| | 3 | 65 | | { |
| | 3 | 66 | | await _jsRuntime.InvokeVoidAsync("infiniteScroll.destroyObserver", sentinelId); |
| | 2 | 67 | | } |
| | 1 | 68 | | catch (JSException) |
| | 1 | 69 | | { |
| | | 70 | | // Silently handle errors during cleanup |
| | 1 | 71 | | } |
| | 3 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Destroys all Intersection Observers (fallback cleanup). |
| | | 76 | | /// </summary> |
| | | 77 | | public async Task DestroyAllObserversAsync() |
| | 2 | 78 | | { |
| | | 79 | | try |
| | 2 | 80 | | { |
| | 2 | 81 | | await _jsRuntime.InvokeVoidAsync("infiniteScroll.destroyAllObservers"); |
| | 1 | 82 | | } |
| | 1 | 83 | | catch (JSException) |
| | 1 | 84 | | { |
| | | 85 | | // Silently handle errors during cleanup |
| | 1 | 86 | | } |
| | 2 | 87 | | } |
| | | 88 | | } |