Skip to content

Commit

Permalink
Fix: unexpected JSDisconnectedException was thrown when it is running…
Browse files Browse the repository at this point in the history
… on a Blazor Server
  • Loading branch information
jsakamoto committed Apr 3, 2024
1 parent 5b02f3f commit 10c86b1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions BlazingStory/Internals/Components/Menus/PopupMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
this._HotKeysContext = null;

if (this._EventSubscriber == null) return;
await this._EventSubscriber.InvokeVoidAsync("dispose");
await this._EventSubscriber.DisposeAsync();
await this._EventSubscriber.InvokeVoidIfConnectedAsync("dispose");
await this._EventSubscriber.DisposeIfConnectedAsync();
this._EventSubscriber = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@

public async ValueTask DisposeAsync()
{
await _EventMonitorSubscriber.InvokeVoidIfConnectedAsync("dispose");
await _EventMonitorSubscriber.DisposeIfConnectedAsync();
await _JSModule.DisposeAsync();
_ThisRef?.Dispose();
Expand Down
11 changes: 7 additions & 4 deletions BlazingStory/Internals/Extensions/IJSExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ namespace BlazingStory.Internals.Extensions;

internal static class IJSExtensions
{
public static async ValueTask InvokeVoidIfConnectedAsync(this IJSObjectReference? value, string identifier, params object?[]? args)
{
try { if (value != null) await value.InvokeVoidAsync(identifier, args); }
catch (JSDisconnectedException) { }
}

public static async ValueTask DisposeIfConnectedAsync(this IJSObjectReference? value)
{
try
{
if (value != null) await value.DisposeAsync();
}
try { if (value != null) await value.DisposeAsync(); }
catch (JSDisconnectedException) { }
}
}
2 changes: 1 addition & 1 deletion BlazingStory/Internals/Pages/Canvas/MeasureLayer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@

public async ValueTask DisposeAsync()
{
await this._JSModule.InvokeVoidAsync("detach");
await this._JSModule.InvokeVoidIfConnectedAsync("detach");
await this._JSModule.DisposeAsync();
this._This.Dispose();
}
Expand Down
6 changes: 1 addition & 5 deletions BlazingStory/Internals/Services/JSModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ private async ValueTask<IJSObjectReference> GetModuleAsync()
return this._Module;
}

public async ValueTask InvokeVoidIfConnectedAsync(string identifier, params object?[]? args)
{
try { await this.InvokeVoidAsync(identifier, args); }
catch (JSDisconnectedException) { }
}
public ValueTask InvokeVoidIfConnectedAsync(string identifier, params object?[]? args) => this._Module.InvokeVoidIfConnectedAsync(identifier, args);

public async ValueTask InvokeVoidAsync(string identifier, params object?[]? args)
{
Expand Down

0 comments on commit 10c86b1

Please sign in to comment.