Skip to content

Commit e033af5

Browse files
author
Stedra Kristóf
committed
Refactor component state
1 parent 2a48d10 commit e033af5

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

src/AutSoft.AspNetCore.Blazor/ComponentState/ComponentStateStorage.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,19 @@ public void SaveStateForComponent(string instanceKey, ComponentBase component)
1818

1919
var propertiesToSave = GetPropertiesFromHierarchy(componentType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
2020
.Where(prop => prop.IsDefined(typeof(PreserveStateAttribute), false))
21+
.Where(prop => prop.GetValue(component) != null)
22+
.Select(prop => new StateEntry(prop, prop.GetValue(component)!))
2123
.ToList();
2224

23-
foreach (var property in propertiesToSave)
24-
{
25-
var value = property.GetValue(component);
26-
27-
if (value != null)
28-
componentStates.Add(new StateEntry(property, value));
29-
}
25+
componentStates.AddRange(propertiesToSave);
3026

3127
var fieldsToSave = GetFieldsFromHierarchy(componentType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
32-
.Where(prop => prop.IsDefined(typeof(PreserveStateAttribute), false))
28+
.Where(field => field.IsDefined(typeof(PreserveStateAttribute), false))
29+
.Where(field => field.GetValue(component) != null)
30+
.Select(field => new StateEntry(field, field.GetValue(component)!))
3331
.ToList();
3432

35-
foreach (var field in fieldsToSave)
36-
{
37-
var value = field.GetValue(component);
38-
39-
if (value != null)
40-
componentStates.Add(new StateEntry(field, value));
41-
}
33+
componentStates.AddRange(fieldsToSave);
4234

4335
_currentComponentStates[instanceKey] = componentStates;
4436
}
@@ -47,12 +39,16 @@ public void SaveStateForComponent(string instanceKey, ComponentBase component)
4739
public void RestoreStateForComponent(string instanceKey, ComponentBase component)
4840
{
4941
if (!_currentComponentStates.ContainsKey(instanceKey))
42+
{
5043
return;
44+
}
5145

5246
foreach (var state in _currentComponentStates[instanceKey])
5347
{
5448
if (state.Member is PropertyInfo pi)
49+
{
5550
pi.SetValue(component, state.Value);
51+
}
5652
else if (state.Member is FieldInfo fi)
5753
{
5854
fi.SetValue(component, state.Value);
@@ -73,24 +69,36 @@ public void ClearComponentStates()
7369
private IEnumerable<PropertyInfo> GetPropertiesFromHierarchy(Type type, BindingFlags bindingFlags)
7470
{
7571
foreach (var property in type.GetProperties(bindingFlags))
72+
{
7673
yield return property;
74+
}
7775

7876
if (type.BaseType == null)
77+
{
7978
yield break;
79+
}
8080

8181
foreach (var property in GetPropertiesFromHierarchy(type.BaseType, bindingFlags))
82+
{
8283
yield return property;
84+
}
8385
}
8486

8587
private IEnumerable<FieldInfo> GetFieldsFromHierarchy(Type type, BindingFlags bindingFlags)
8688
{
8789
foreach (var field in type.GetFields(bindingFlags))
90+
{
8891
yield return field;
92+
}
8993

9094
if (type.BaseType == null)
95+
{
9196
yield break;
97+
}
9298

9399
foreach (var field in GetFieldsFromHierarchy(type.BaseType, bindingFlags))
100+
{
94101
yield return field;
102+
}
95103
}
96104
}

src/AutSoft.AspNetCore.Blazor/ComponentState/IComponentStateStorage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ namespace AutSoft.AspNetCore.Blazor.ComponentState;
88
public interface IComponentStateStorage
99
{
1010
/// <summary>
11-
/// Saves the component state.
11+
/// Saves the state of the component to the specified instance key.
1212
/// </summary>
13-
/// <param name="instanceKey">Key of the instance.</param>
13+
/// <param name="instanceKey">Key of the instance where to save.</param>
1414
/// <param name="component">Component to save.</param>
1515
void SaveStateForComponent(string instanceKey, ComponentBase component);
1616

1717
/// <summary>
18-
/// Restores the component state.
18+
/// Restores the state of the component from the specified instance key.
1919
/// </summary>
20-
/// <param name="instanceKey">Key of the instance.</param>
20+
/// <param name="instanceKey">Key of the instance from which to reload.</param>
2121
/// <param name="component">Component to restore.</param>
2222
void RestoreStateForComponent(string instanceKey, ComponentBase component);
2323

2424
/// <summary>
25-
/// Clears the component state.
25+
/// Clear all component states.
2626
/// </summary>
2727
void ClearComponentStates();
2828
}

src/AutSoft.AspNetCore.Blazor/ComponentState/StateEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace AutSoft.AspNetCore.Blazor.ComponentState;
44

55
/// <summary>
6-
/// State of the entry.
6+
/// State entry for the component.
77
/// </summary>
88
public class StateEntry
99
{

src/AutSoft.AspNetCore.Blazor/ComponentState/StatefulComponentBase.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ namespace AutSoft.AspNetCore.Blazor.ComponentState;
99
public class StatefulComponentBase : ComponentBase, IDisposable
1010
{
1111
private string? _instanceKey;
12-
private bool _disposed;
12+
private bool _isDisposed;
1313

1414
/// <summary>
1515
/// Indicates that the component state should be saved.
1616
/// </summary>
1717
protected virtual bool ShouldSaveState { get; } = true;
1818

19+
[Inject]
1920
private IComponentStateStorage ComponentStateStorage { get; set; } = null!;
2021

22+
[Inject]
2123
private IJSRuntime JSRuntime { get; set; } = null!;
2224

2325
/// <inheritdoc />
@@ -42,12 +44,16 @@ public void Dispose()
4244
/// <inheritdoc />
4345
protected virtual void Dispose(bool disposing)
4446
{
45-
if (_disposed)
47+
if (_isDisposed)
48+
{
4649
return;
50+
}
4751

4852
if (disposing && !string.IsNullOrEmpty(_instanceKey))
53+
{
4954
ComponentStateStorage.SaveStateForComponent(_instanceKey, this);
55+
}
5056

51-
_disposed = true;
57+
_isDisposed = true;
5258
}
5359
}

0 commit comments

Comments
 (0)