Skip to content

Commit

Permalink
Merge pull request #5 from iancoetzer/master
Browse files Browse the repository at this point in the history
Introduce disposing of CanvasBase (Ids) to prevent calling event handers on disposed canvas base instances
  • Loading branch information
mizrael authored Jun 27, 2024
2 parents d5518ff + 8788454 commit c1da363
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/Blazorex/CanvasBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
19 changes: 16 additions & 3 deletions src/Blazorex/wwwroot/blazorex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -112,8 +121,12 @@ window.Blazorex = (() => {
createImageData,
putImageData,
processBatch,
directCall
directCall,
removeContext
};
})();

window.requestAnimationFrame(Blazorex.onFrameUpdate);
window.requestAnimationFrame(Blazorex.onFrameUpdate);



0 comments on commit c1da363

Please sign in to comment.