From d17bb83c40eb5fac06168d8df7ecd164921b7232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20James?= Date: Mon, 4 Mar 2024 16:29:54 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Add=20a=20cancellation=20t?= =?UTF-8?q?oken=20to=20help=20remove=20errors=20in=20Blazor=20Server=20wit?= =?UTF-8?q?h=20pre-rendering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeMirror6/CodeMirror6WrapperInternal.razor.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CodeMirror6/CodeMirror6WrapperInternal.razor.cs b/CodeMirror6/CodeMirror6WrapperInternal.razor.cs index d5fe914..f3539ec 100644 --- a/CodeMirror6/CodeMirror6WrapperInternal.razor.cs +++ b/CodeMirror6/CodeMirror6WrapperInternal.razor.cs @@ -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(); /// /// Life-cycle method invoked when the component is initialized. @@ -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(); @@ -463,6 +468,7 @@ protected override async Task OnParametersSetAsync() Config.ShowMarkdownControlCharactersAroundCursor = ShowMarkdownControlCharactersAroundCursor; updated = true; } + if (LifeCycleCancellationTokenSource.IsCancellationRequested) return; if (updated) await CmJsInterop.PropertySetters.SetConfiguration(); } @@ -486,6 +492,11 @@ protected override async Task OnParametersSetAsync() /// public async ValueTask DisposeAsync() { + try { + LifeCycleCancellationTokenSource.Cancel(); + LifeCycleCancellationTokenSource.Dispose(); + } + catch (ObjectDisposedException) {} if (CmJsInterop?.IsJSReady == true) await CmJsInterop.DisposeAsync(); CmJsInterop = null;