diff --git a/src/Butil/Bit.Butil/Publics/VisualViewport.cs b/src/Butil/Bit.Butil/Publics/VisualViewport.cs index faeffd11e8..28b44b7779 100644 --- a/src/Butil/Bit.Butil/Publics/VisualViewport.cs +++ b/src/Butil/Bit.Butil/Publics/VisualViewport.cs @@ -103,12 +103,13 @@ public async ValueTask AddResize(Action handler) return listenerId; } + /// /// Fired when the visual viewport is resized. ///
/// https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport/resize_event ///
- public async Task RemoveResize(Action handler) + public async ValueTask RemoveResize(Action handler) { var ids = VisualViewportListenersManager.RemoveListener(handler); @@ -116,6 +117,7 @@ public async Task RemoveResize(Action handler) return ids; } + /// /// Fired when the visual viewport is resized. ///
@@ -127,22 +129,22 @@ public async ValueTask RemoveResize(Guid id) await RemoveResize([id]); } + private async ValueTask RemoveResize(Guid[] ids) { + if (ids.Length == 0) return; + foreach (var id in ids) { _handlers.TryRemove(id, out _); } - await js.InvokeVoidAsync("BitButil.visualViewport.removeResize", ids); + await RemoveResizeFromJs(ids); } - public async ValueTask RemoveAllResizes() - { - var ids = _handlers.Select(h => h.Key).ToArray(); - - _handlers.Clear(); - VisualViewportListenersManager.RemoveListeners(ids); + private async ValueTask RemoveResizeFromJs(Guid[] ids) + { + if (OperatingSystem.IsBrowser() is false) return; await js.InvokeVoidAsync("BitButil.visualViewport.removeResize", ids); } @@ -162,12 +164,13 @@ public async ValueTask AddScroll(Action handler) return listenerId; } + /// /// Fired when the visual viewport is scrolled. ///
/// https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport/scroll_event ///
- public async Task RemoveScroll(Action handler) + public async ValueTask RemoveScroll(Action handler) { var ids = VisualViewportListenersManager.RemoveListener(handler); @@ -175,6 +178,7 @@ public async Task RemoveScroll(Action handler) return ids; } + /// /// Fired when the visual viewport is scrolled. ///
@@ -186,6 +190,7 @@ public async ValueTask RemoveScroll(Guid id) await RemoveScroll([id]); } + private async ValueTask RemoveScroll(Guid[] ids) { foreach (var id in ids) @@ -193,25 +198,31 @@ private async ValueTask RemoveScroll(Guid[] ids) _handlers.TryRemove(id, out _); } + await RemoveScrollFromJs(ids); + } + + private async ValueTask RemoveScrollFromJs(Guid[] ids) + { + if (OperatingSystem.IsBrowser() is false) return; + await js.InvokeVoidAsync("BitButil.visualViewport.removeScroll", ids); } - public async ValueTask RemoveAllScrolls() + + + public async ValueTask RemoveAllEventHandlers() { + if (_handlers.Count == 0) return; + var ids = _handlers.Select(h => h.Key).ToArray(); _handlers.Clear(); VisualViewportListenersManager.RemoveListeners(ids); - await js.InvokeVoidAsync("BitButil.visualViewport.removeScroll", ids); - } - - public async ValueTask DisposeAsync() - { var toAwait = new List(); - var resizeValueTask = RemoveAllResizes(); - var scrollValueTask = RemoveAllScrolls(); + var resizeValueTask = RemoveResizeFromJs(ids); + var scrollValueTask = RemoveScrollFromJs(ids); if (resizeValueTask.IsCompleted is false) { @@ -225,4 +236,19 @@ public async ValueTask DisposeAsync() await Task.WhenAll(toAwait); } + + public async ValueTask DisposeAsync() + { + await DisposeAsync(true); + + GC.SuppressFinalize(this); + } + + protected virtual async ValueTask DisposeAsync(bool disposing) + { + if (disposing) + { + await RemoveAllEventHandlers(); + } + } } diff --git a/src/Butil/Demo/Bit.Butil.Demo.Core/Pages/VisualViewportPage.razor b/src/Butil/Demo/Bit.Butil.Demo.Core/Pages/VisualViewportPage.razor index 3a80134e56..32edceed51 100644 --- a/src/Butil/Demo/Bit.Butil.Demo.Core/Pages/VisualViewportPage.razor +++ b/src/Butil/Demo/Bit.Butil.Demo.Core/Pages/VisualViewportPage.razor @@ -1,4 +1,5 @@ @page "/visualViewport" +@implements IAsyncDisposable @inject Bit.Butil.Console console @inject Bit.Butil.VisualViewport visualViewport @@ -113,4 +114,9 @@ var scale = await visualViewport.GetScale(); await console.Log("Scale:", scale); } + + public async ValueTask DisposeAsync() + { + await visualViewport.DisposeAsync(); + } } \ No newline at end of file