From 895df931b9fdefd4e8f8c4c65b1e2b819d0b1019 Mon Sep 17 00:00:00 2001 From: Yuriy Durov Date: Sun, 15 Sep 2024 21:02:02 +0400 Subject: [PATCH] Update state lifecycle --- .../PersistentComponentRenderStrategy.cs | 42 +++++++------------ .../PersistentPageRenderStrategy.cs | 2 +- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/src/BitzArt.Blazor.State/RenderStrategies/PersistentComponentRenderStrategy.cs b/src/BitzArt.Blazor.State/RenderStrategies/PersistentComponentRenderStrategy.cs index 9bec637..d7ded6e 100644 --- a/src/BitzArt.Blazor.State/RenderStrategies/PersistentComponentRenderStrategy.cs +++ b/src/BitzArt.Blazor.State/RenderStrategies/PersistentComponentRenderStrategy.cs @@ -18,11 +18,20 @@ internal class PersistentComponentRenderStrategy(PersistentComponentBase compone protected override async Task RunInitAndSetParametersAsync() { - // Setup state before initializing the component - await SetupStateAsync(); + // TODO: Dotnet 9 + //if (Handle.RendererInfo.IsInteractive) + var shouldInitializeState = true; + var isInteractive = RendererInfo.IsInteractive; + if (isInteractive) + { + var restored = await TryRestoringStateAsync(); + shouldInitializeState = !restored; + } await base.RunInitAndSetParametersAsync(); + if (shouldInitializeState) await InitializeStateAsync(); + if (PersistentComponent.StateContainer is not null) await PersistentComponent.StateContainer!.RefreshAsync(); } @@ -56,23 +65,7 @@ private async Task WaitForPageStateAsync() throw new TimeoutException("Timed out: Page state took too long to restore."); } - protected virtual async Task SetupStateAsync() - { - if (StateInitialized) throw new InvalidOperationException("State has already been initialized for this component."); - - // TODO: Dotnet 9 - //if (Handle.RendererInfo.IsInteractive) - if (RendererInfo.IsInteractive) - { - await RestoreStateAsync(); - } - else - { - await InitializeStateAsync(); - } - } - - protected virtual async Task RestoreStateAsync() + protected virtual async Task TryRestoringStateAsync() { if (ShouldWaitForRootStateRestore) { @@ -88,10 +81,7 @@ protected virtual async Task RestoreStateAsync() if (pageState is null) { // Page state not found. - // Initializing state as a fallback. - - await InitializeStateAsync(); - return; + return false; } var path = GetComponentLocation(PersistentComponent); @@ -100,16 +90,14 @@ protected virtual async Task RestoreStateAsync() if (state is null) { // Component state not found. - // Initializing state as a fallback. - - await InitializeStateAsync(); - return; + return false; } RestoreComponentState(state); StateInitialized = true; PersistentComponent.NotifyStateRestored(); + return true; } protected void RestoreComponentState(JsonObject state) diff --git a/src/BitzArt.Blazor.State/RenderStrategies/PersistentPageRenderStrategy.cs b/src/BitzArt.Blazor.State/RenderStrategies/PersistentPageRenderStrategy.cs index b3a6a1c..6c7988d 100644 --- a/src/BitzArt.Blazor.State/RenderStrategies/PersistentPageRenderStrategy.cs +++ b/src/BitzArt.Blazor.State/RenderStrategies/PersistentPageRenderStrategy.cs @@ -57,7 +57,7 @@ internal override void BuildRenderTree(RenderTreeBuilder builder) builder.CloseComponent(); } - protected override async Task RestoreStateAsync() + protected override async Task TryRestoringStateAsync() { var js = ServiceProvider.GetRequiredService(); var stateBase64 = await js.InvokeAsync("getInnerText", [PageStateElementId]);