Skip to content

Commit

Permalink
feat(butil): improve disposing of VisualViewport class of Butil #6984 (
Browse files Browse the repository at this point in the history
  • Loading branch information
msynk authored Feb 27, 2024
1 parent ca40340 commit 7efd194
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
60 changes: 43 additions & 17 deletions src/Butil/Bit.Butil/Publics/VisualViewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,21 @@ public async ValueTask<Guid> AddResize(Action handler)

return listenerId;
}

/// <summary>
/// Fired when the visual viewport is resized.
/// <br/>
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport/resize_event">https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport/resize_event</see>
/// </summary>
public async Task<Guid[]> RemoveResize(Action handler)
public async ValueTask<Guid[]> RemoveResize(Action handler)
{
var ids = VisualViewportListenersManager.RemoveListener(handler);

await RemoveResize(ids);

return ids;
}

/// <summary>
/// Fired when the visual viewport is resized.
/// <br/>
Expand All @@ -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);
}
Expand All @@ -162,19 +164,21 @@ public async ValueTask<Guid> AddScroll(Action handler)

return listenerId;
}

/// <summary>
/// Fired when the visual viewport is scrolled.
/// <br/>
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport/scroll_event">https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport/scroll_event</see>
/// </summary>
public async Task<Guid[]> RemoveScroll(Action handler)
public async ValueTask<Guid[]> RemoveScroll(Action handler)
{
var ids = VisualViewportListenersManager.RemoveListener(handler);

await RemoveScroll(ids);

return ids;
}

/// <summary>
/// Fired when the visual viewport is scrolled.
/// <br/>
Expand All @@ -186,32 +190,39 @@ public async ValueTask RemoveScroll(Guid id)

await RemoveScroll([id]);
}

private async ValueTask RemoveScroll(Guid[] ids)
{
foreach (var id in 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<Task>();

var resizeValueTask = RemoveAllResizes();
var scrollValueTask = RemoveAllScrolls();
var resizeValueTask = RemoveResizeFromJs(ids);
var scrollValueTask = RemoveScrollFromJs(ids);

if (resizeValueTask.IsCompleted is false)
{
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/visualViewport"
@implements IAsyncDisposable
@inject Bit.Butil.Console console
@inject Bit.Butil.VisualViewport visualViewport

Expand Down Expand Up @@ -113,4 +114,9 @@
var scale = await visualViewport.GetScale();
await console.Log("Scale:", scale);
}

public async ValueTask DisposeAsync()
{
await visualViewport.DisposeAsync();
}
}

0 comments on commit 7efd194

Please sign in to comment.