Skip to content

Commit

Permalink
⚡️ Add a cancellation token to help remove errors in Blazor Server wi…
Browse files Browse the repository at this point in the history
…th pre-rendering
  • Loading branch information
gaelj committed Mar 4, 2024
1 parent 2d1bc67 commit d17bb83
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CodeMirror6/CodeMirror6WrapperInternal.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public partial class CodeMirror6WrapperInternal : ComponentBase, IAsyncDisposabl
internal CodeMirrorConfiguration Config = null!;
private bool shouldRender = true;
private bool IsCodeMirrorInitialized;
private readonly CancellationTokenSource LifeCycleCancellationTokenSource = new();

/// <summary>
/// Life-cycle method invoked when the component is initialized.
Expand Down Expand Up @@ -323,12 +324,16 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
private async Task InitializeJsInterop()
{
if (CmJsInterop is null) {
Logger.LogDebug("Initializing CodeMirror JS Interop with id {id}", Setup.Id);
if (LifeCycleCancellationTokenSource.IsCancellationRequested) return;
CmJsInterop = new CodeMirrorJsInterop(JSRuntime, this);
if (LifeCycleCancellationTokenSource.IsCancellationRequested) return;
if (!await CmJsInterop.PropertySetters.InitCodeMirror()) return;
if (GetMentionCompletions is not null) {
var mentionCompletions = await GetMentionCompletions();
if (!await CmJsInterop.PropertySetters.SetMentionCompletions(mentionCompletions)) return;
}
if (LifeCycleCancellationTokenSource.IsCancellationRequested) return;
IsCodeMirrorInitialized = true;
await InvokeAsync(StateHasChanged);
await OnParametersSetAsync();
Expand Down Expand Up @@ -463,6 +468,7 @@ protected override async Task OnParametersSetAsync()
Config.ShowMarkdownControlCharactersAroundCursor = ShowMarkdownControlCharactersAroundCursor;
updated = true;
}
if (LifeCycleCancellationTokenSource.IsCancellationRequested) return;
if (updated)
await CmJsInterop.PropertySetters.SetConfiguration();
}
Expand All @@ -486,6 +492,11 @@ protected override async Task OnParametersSetAsync()
/// <returns></returns>
public async ValueTask DisposeAsync()
{
try {
LifeCycleCancellationTokenSource.Cancel();
LifeCycleCancellationTokenSource.Dispose();
}
catch (ObjectDisposedException) {}
if (CmJsInterop?.IsJSReady == true)
await CmJsInterop.DisposeAsync();
CmJsInterop = null;
Expand Down

0 comments on commit d17bb83

Please sign in to comment.