Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Do not render components until state is restored
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed May 24, 2024
1 parent 7af4e31 commit 24aa4e6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
namespace BitzArt.Blazor.MVVM.SampleApp;

namespace BitzArt.Blazor.MVVM.SampleApp;

public class TestPageViewModel : ViewModel<TestPageState>
{
protected override void OnDependenciesInjected()
public override async Task InitializeStateAsync()
{
OnStateInitializedAsync += async (_) =>
{
State.Text = "Test Page State initialized";
await ComponentStateChangedAsync();
};
State.Text = "Test Page State was initialized";
await ComponentStateChangedAsync();
}

public override async Task OnStateRestoredAsync()
{
await Task.Delay(3000);

State.Text = "Test Page State was restored";
await ComponentStateChangedAsync();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ else
protected override async Task OnInitializedAsync()
{
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(500);
await Task.Delay(5000);

var startDate = DateOnly.FromDateTime(DateTime.Now);
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
Expand Down
17 changes: 17 additions & 0 deletions src/BitzArt.Blazor.MVVM/Components/ComponentBase{TViewModel}.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Reflection;

namespace BitzArt.Blazor.MVVM;

Expand Down Expand Up @@ -30,6 +31,20 @@ public abstract class ComponentBase<TViewModel> : ComponentBase, IStateComponent
[Inject]
protected NavigationManager NavigationManager { get; set; } = null!;

private readonly FieldInfo _hasPendingQueuedRenderField;

protected ComponentBase()
{
_hasPendingQueuedRenderField = typeof(ComponentBase)
.GetField("_hasPendingQueuedRender", BindingFlags.Instance | BindingFlags.NonPublic)!;
}

protected override void OnInitialized()
{
_hasPendingQueuedRenderField.SetValue(this, true);
base.OnInitialized();
}

/// <summary>
/// Method invoked when the component is ready to start, having received its initial
/// parameters from its parent in the render tree. Override this method if you will
Expand All @@ -47,6 +62,8 @@ protected override async Task OnInitializedAsync()
await InvokeAsync(StateHasChanged);
};
await InvokeAsync(StateHasChanged);

_hasPendingQueuedRenderField.SetValue(this, false);
}

private async Task SetupStateAsync()
Expand Down

0 comments on commit 24aa4e6

Please sign in to comment.