From d720ad3f885b30e3421619ce3af6bd5adae21867 Mon Sep 17 00:00:00 2001 From: iancoetzer Date: Thu, 27 Jun 2024 08:40:11 +0200 Subject: [PATCH 1/4] Implement routines to remove a CanvasBase 'Id' from the _contexts array. In order to prevent javascript firing managed instance methods on window events for canvas objects that are no longer in scope. --- src/Blazorex/CanvasBase.cs | 25 ++++++++++++++++++++++++- src/Blazorex/wwwroot/blazorex.js | 16 ++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Blazorex/CanvasBase.cs b/src/Blazorex/CanvasBase.cs index 582ace3..4148d0f 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, IDisposable { + private bool _disposed = false; + protected override async Task OnInitializedAsync() { if (this.CanvasManager is null) @@ -110,5 +112,26 @@ public async ValueTask Resized(int width, int height) public IRenderContext RenderContext { get; private set; } #endregion Properties + + #region Disposing + protected virtual void Dispose(bool disposing) + { + if (!_disposed) + { + if (disposing) + { + // Call javascript to delete this canvas Id from the _contexts array + await JSRuntime.InvokeVoidAsync("Blazorex.removeContext", Id); + } + _disposed = true; + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + #endregion Disposing } } \ No newline at end of file diff --git a/src/Blazorex/wwwroot/blazorex.js b/src/Blazorex/wwwroot/blazorex.js index 625e43b..2c08a32 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) { @@ -116,4 +125,7 @@ window.Blazorex = (() => { }; })(); -window.requestAnimationFrame(Blazorex.onFrameUpdate); \ No newline at end of file +window.requestAnimationFrame(Blazorex.onFrameUpdate); + + + From d6f95a37894ad09cf8067ee497727fc84fcecf4e Mon Sep 17 00:00:00 2001 From: iancoetzer Date: Thu, 27 Jun 2024 09:09:01 +0200 Subject: [PATCH 2/4] Changed CanvasBase class to rather implement IAsyncDisposable --- src/Blazorex/CanvasBase.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/Blazorex/CanvasBase.cs b/src/Blazorex/CanvasBase.cs index 4148d0f..acd7b8a 100644 --- a/src/Blazorex/CanvasBase.cs +++ b/src/Blazorex/CanvasBase.cs @@ -5,7 +5,7 @@ namespace Blazorex { - public class CanvasBase : ComponentBase, IDisposable + public class CanvasBase : ComponentBase, IAsyncDisposable { private bool _disposed = false; @@ -114,24 +114,15 @@ public async ValueTask Resized(int width, int height) #endregion Properties #region Disposing - protected virtual void Dispose(bool disposing) + public async ValueTask DisposeAsync() { if (!_disposed) { - if (disposing) - { - // Call javascript to delete this canvas Id from the _contexts array - await JSRuntime.InvokeVoidAsync("Blazorex.removeContext", Id); - } + // Call javascript to delete this canvas Id from the _contexts array + await JSRuntime.InvokeVoidAsync("Blazorex.removeContext", Id); _disposed = true; } } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } #endregion Disposing } } \ No newline at end of file From 36541286892b13369f5caf608bece3c1a1933ee4 Mon Sep 17 00:00:00 2001 From: iancoetzer Date: Thu, 27 Jun 2024 11:02:33 +0200 Subject: [PATCH 3/4] CanvasBase cannot find Blazorex.removeContext, changing blazorex.js to return the removeContext function to patch this error. --- src/Blazorex/CanvasBase.cs | 2 ++ src/Blazorex/wwwroot/blazorex.js | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Blazorex/CanvasBase.cs b/src/Blazorex/CanvasBase.cs index acd7b8a..c865412 100644 --- a/src/Blazorex/CanvasBase.cs +++ b/src/Blazorex/CanvasBase.cs @@ -120,6 +120,8 @@ public async ValueTask DisposeAsync() { // Call javascript to delete this canvas Id from the _contexts array await JSRuntime.InvokeVoidAsync("Blazorex.removeContext", Id); + await JSRuntime.InvokeVoidAsync("Blazorex.initCanvas", Id, managedInstance); + _disposed = true; } } diff --git a/src/Blazorex/wwwroot/blazorex.js b/src/Blazorex/wwwroot/blazorex.js index 2c08a32..e4734be 100644 --- a/src/Blazorex/wwwroot/blazorex.js +++ b/src/Blazorex/wwwroot/blazorex.js @@ -121,7 +121,8 @@ window.Blazorex = (() => { createImageData, putImageData, processBatch, - directCall + directCall, + removeContext }; })(); From 8788454b0431e9a19ae85160e7827ea60e265926 Mon Sep 17 00:00:00 2001 From: iancoetzer Date: Thu, 27 Jun 2024 11:03:15 +0200 Subject: [PATCH 4/4] Remove initCanvas call from DisposeAsync --- src/Blazorex/CanvasBase.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Blazorex/CanvasBase.cs b/src/Blazorex/CanvasBase.cs index c865412..e769bf5 100644 --- a/src/Blazorex/CanvasBase.cs +++ b/src/Blazorex/CanvasBase.cs @@ -120,8 +120,7 @@ public async ValueTask DisposeAsync() { // Call javascript to delete this canvas Id from the _contexts array await JSRuntime.InvokeVoidAsync("Blazorex.removeContext", Id); - await JSRuntime.InvokeVoidAsync("Blazorex.initCanvas", Id, managedInstance); - + _disposed = true; } }