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

Commit

Permalink
Component lifecycle fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed May 23, 2024
1 parent a64e8ee commit d19b295
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@page "/test"
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: true))
@inherits PageBase<TestPageViewModel>

<h3>TestPage</h3>

<p>@ViewModel.State?.Text</p>

<ComponentStateContainer ViewModel="ViewModel" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BitzArt.Blazor.MVVM.SampleApp;

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

public class TestPageState : ComponentState
{
public string Text { get; set; } = "Test Page State not initialized";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Weather
</NavLink>
</div>

<div class="nav-item px-3">
<NavLink class="nav-link" href="test">
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Test
</NavLink>
</div>
</nav>
</div>

12 changes: 7 additions & 5 deletions src/BitzArt.Blazor.MVVM/Components/ComponentBase{TViewModel}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ public abstract class ComponentBase<TViewModel> : ComponentBase, IStateComponent
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
ViewModel.OnComponentStateChanged += async (_) =>
await SetupStateAsync();

ViewModel.OnComponentStateChanged += (_) =>
{
await InvokeAsync(StateHasChanged);
InvokeAsync(StateHasChanged);
};

await SetupStateAsync();
await InvokeAsync(StateHasChanged);
}

private async Task SetupStateAsync()
Expand All @@ -70,7 +71,8 @@ protected virtual async Task InitializeStateAsync()
statefulViewModel.State.IsInitialized = true;

statefulViewModel.OnStateInitialized?.Invoke(ViewModel);
statefulViewModel.OnStateInitializedAsync?.Invoke(ViewModel);
if (statefulViewModel.OnStateInitializedAsync is not null)
await statefulViewModel.OnStateInitializedAsync!.Invoke(ViewModel);

StateHasChanged();
ViewModel.ComponentStateContainer?.NotifyStateChanged();
Expand Down
5 changes: 5 additions & 0 deletions src/BitzArt.Blazor.MVVM/Components/PageBase{TViewModel}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public abstract class PageBase<TViewModel> : ComponentBase<TViewModel>, IStateCo
protected override async Task RestoreStateAsync()
{
var state = await Js.InvokeAsync<string?>("getInnerText", [StateKey]);
if (state is null)
{
PageStateDictionaryContainer.MarkConfigured();
return;
}
var buffer = Convert.FromBase64String(state!);
var json = Encoding.UTF8.GetString(buffer);

Expand Down
1 change: 0 additions & 1 deletion src/BitzArt.Blazor.MVVM/Factory/ViewModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public ViewModel Create(IServiceProvider serviceProvider, Type viewModelType, Co
viewModel.OnComponentStateChanged += (sender) =>
{
viewModel.ComponentStateContainer?.NotifyStateChanged();
return Task.CompletedTask;
};

foreach (var injection in viewModelMap.Injections)
Expand Down
2 changes: 1 addition & 1 deletion src/BitzArt.Blazor.MVVM/Models/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected TViewModel CreateNestedViewModel<TViewModel>()
return ViewModelFactory.Create<TViewModel>(ServiceProvider, nestedSignature);
}

public delegate Task ComponentStateHasChangedHandler(object sender);
public delegate void ComponentStateHasChangedHandler(object sender);
public ComponentStateHasChangedHandler? OnComponentStateChanged { get; set; }

/// <summary>
Expand Down
10 changes: 1 addition & 9 deletions src/BitzArt.Blazor.MVVM/Services/BlazorViewModelStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class BlazorViewModelStateManager(IViewModelFactory viewModelFactory)
{
var injectionMap = _viewModelFactory.GetInjectionMap(viewModel.GetType());
var state = GetState(viewModel, injectionMap);
if (state is null) return null;
state ??= [];

return JsonSerializer.SerializeToUtf8Bytes(state, StateJsonOptionsProvider.Options);
}
Expand All @@ -31,14 +31,6 @@ internal class BlazorViewModelStateManager(IViewModelFactory viewModelFactory)
{
var nodeJson = (JsonObject)JsonSerializer.SerializeToNode(statefulViewModel.State, statefulViewModel.State.GetType(), StateJsonOptionsProvider.Options)!;
foreach (var node in nodeJson) state.Add(node.Key, node.Value);

//var nodeState = JsonSerializer.Deserialize<IDictionary<string, object>>(json, StateJsonOptionsProvider.Options)!;
//foreach (var item in nodeState) state.Add(item.Key, item.Value);

/*foreach (var property in statefulViewModel.StateType.GetProperties())
{
state.Add(property.Name, property.GetValue(statefulViewModel.State));
}*/
}

foreach (var injection in injectionMap.Injections.Where(x => x.IsNestedViewModelInjection))
Expand Down

0 comments on commit d19b295

Please sign in to comment.