diff --git a/src/Blazorex/CanvasBase.cs b/src/Blazorex/CanvasBase.cs index 582ace3..e769bf5 100644 --- a/src/Blazorex/CanvasBase.cs +++ b/src/Blazorex/CanvasBase.cs @@ -5,8 +5,10 @@ namespace Blazorex { - public class CanvasBase : ComponentBase + public class CanvasBase : ComponentBase, IAsyncDisposable { + private bool _disposed = false; + protected override async Task OnInitializedAsync() { if (this.CanvasManager is null) @@ -110,5 +112,18 @@ public async ValueTask Resized(int width, int height) public IRenderContext RenderContext { get; private set; } #endregion Properties + + #region Disposing + public async ValueTask DisposeAsync() + { + if (!_disposed) + { + // Call javascript to delete this canvas Id from the _contexts array + await JSRuntime.InvokeVoidAsync("Blazorex.removeContext", Id); + + _disposed = true; + } + } + #endregion Disposing } } \ No newline at end of file diff --git a/src/Blazorex/wwwroot/blazorex.js b/src/Blazorex/wwwroot/blazorex.js index 625e43b..e4734be 100644 --- a/src/Blazorex/wwwroot/blazorex.js +++ b/src/Blazorex/wwwroot/blazorex.js @@ -79,7 +79,16 @@ window.Blazorex = (() => { } return result; - }; + }, + removeContext = (ctxId) => { + const ctx = _contexts[ctxId].context; + if (!ctx){ + return ; + } + + delete _contexts[ctxId]; + } + ; window.onkeyup = (e) => { for (let ctx in _contexts) { @@ -112,8 +121,12 @@ window.Blazorex = (() => { createImageData, putImageData, processBatch, - directCall + directCall, + removeContext }; })(); -window.requestAnimationFrame(Blazorex.onFrameUpdate); \ No newline at end of file +window.requestAnimationFrame(Blazorex.onFrameUpdate); + + +