Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ public partial class DiagramCanvas : IAsyncDisposable

public async ValueTask DisposeAsync()
{
BlazorDiagram.Changed -= OnDiagramChanged;
try
{
BlazorDiagram.Changed -= OnDiagramChanged;

if (_reference == null)
return;
if (_reference == null)
return;

if (elementReference.Id != null)
await JSRuntime.UnobserveResizes(elementReference);
if (elementReference.Id != null && JSRuntime != null)
await JSRuntime.UnobserveResizes(elementReference);

_reference.Dispose();
_reference.Dispose();
}
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
{
// This exception is expected when the user navigates away from the page
// and the component is disposed. It can be ignored.
}
}

private string GetLayerStyle(int order)
Expand All @@ -56,12 +64,20 @@ protected override void OnInitialized()

protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
try
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
if (firstRender)
{
BlazorDiagram.SetContainer(await JSRuntime.GetBoundingClientRect(elementReference));
await JSRuntime.ObserveResizes(elementReference, _reference!);
}
}
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
{
BlazorDiagram.SetContainer(await JSRuntime.GetBoundingClientRect(elementReference));
await JSRuntime.ObserveResizes(elementReference, _reference!);
// This exception is expected when the user navigates away from the page
// and the component is disposed. It can be ignored
}
}

Expand Down
29 changes: 19 additions & 10 deletions src/Blazor.Diagrams/Components/Renderers/GroupRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

namespace Blazor.Diagrams.Components.Renderers;

Expand Down Expand Up @@ -53,17 +54,25 @@ protected override bool ShouldRender()

protected override void OnAfterRender(bool firstRender)
{
if (Size.Zero.Equals(Group.Size))
return;

// Update the port positions (and links) when the size of the group changes
// This will save us some JS trips as well as useless rerenders

if (_lastSize == null || !_lastSize.Equals(Group.Size))
try
{
if (Size.Zero.Equals(Group.Size))
return;

// Update the port positions (and links) when the size of the group changes
// This will save us some JS trips as well as useless rerenders

if (_lastSize == null || !_lastSize.Equals(Group.Size))
{
Group.ReinitializePorts();
Group.RefreshLinks();
_lastSize = Group.Size;
}
}
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
{
Group.ReinitializePorts();
Group.RefreshLinks();
_lastSize = Group.Size;
// This exception is expected when the user navigates away from the page
// and the component is disposed. It can be ignored
}
}

Expand Down
41 changes: 28 additions & 13 deletions src/Blazor.Diagrams/Components/Renderers/NodeRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ public class NodeRenderer : ComponentBase, IDisposable

public void Dispose()
{
Node.Changed -= OnNodeChanged;
Node.VisibilityChanged -= OnVisibilityChanged;
try
{
Node.Changed -= OnNodeChanged;
Node.VisibilityChanged -= OnVisibilityChanged;

if (_element.Id != null && !Node.ControlledSize && JsRuntime != null)
{
_ = JsRuntime.UnobserveResizes(_element);
}

if (_element.Id != null && !Node.ControlledSize)
_reference?.Dispose();
}
catch (Exception ex) when (ex is JSDisconnectedException || ex is ObjectDisposedException)
{
_ = JsRuntime.UnobserveResizes(_element);
// This exception can be ignored.
}

_reference?.Dispose();
}

[JSInvokable]
Expand Down Expand Up @@ -127,18 +134,26 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && !Node.Visible)
return;

if (firstRender || _becameVisible)
try
{
_becameVisible = false;
if (firstRender && !Node.Visible)
return;

if (!Node.ControlledSize)
if (firstRender || _becameVisible)
{
await JsRuntime.ObserveResizes(_element, _reference!);
_becameVisible = false;

if (!Node.ControlledSize)
{
await JsRuntime.ObserveResizes(_element, _reference!);
}
}
}
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
{
// This exception is expected when the user navigates away from the page
// and the component is disposed. It can be ignored
}
}

private void OnNodeChanged(Model _)
Expand Down
12 changes: 10 additions & 2 deletions src/Blazor.Diagrams/Components/Renderers/PortRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!Port.Initialized)
try
{
await UpdateDimensions();
if (!Port.Initialized)
{
await UpdateDimensions();
}
}
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
{
// This exception is expected when the user navigates away from the page
// and the component is disposed. It can be ignored
}
}

Expand Down