-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
HxChangeTracker.cs
74 lines (61 loc) · 1.92 KB
/
HxChangeTracker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.ComponentModel;
using Microsoft.Extensions.Logging;
namespace Havit.Blazor.Components.Web;
/// <summary>
/// Subscribes to INotifyPropertyChanged changes and triggers re-rendering of ChildContent.
/// To be used when you want to limit the scope of re-rendering and prevent re-rendering of a wider component.
/// </summary>
public class HxChangeTracker : ComponentBase, IDisposable
{
/// <summary>
/// The content to which the value should be provided.
/// </summary>
[Parameter] public RenderFragment ChildContent { get; set; }
/// <summary>
/// The value to be tracked.
/// </summary>
[Parameter] public INotifyPropertyChanged Value { get; set; }
[Inject] protected ILogger<HxChangeTracker> Logger { get; set; }
private INotifyPropertyChanged _previousValueSet;
protected override void OnParametersSet()
{
base.OnParametersSet();
if (Value is null)
{
throw new ArgumentException($"Missing required parameter '{nameof(Value)}' for component '{GetType().Name}'.");
}
if (_previousValueSet != Value)
{
Logger.LogDebug("Value.PropertyChanged += Value_PropertyChanged;");
Value.PropertyChanged += Value_PropertyChanged;
if (_previousValueSet is not null)
{
Logger.LogDebug("previousValueSet.PropertyChanged -= Value_PropertyChanged");
_previousValueSet.PropertyChanged -= Value_PropertyChanged;
}
}
_previousValueSet = Value;
}
private void Value_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Logger.LogDebug("Value_PropertyChanged");
StateHasChanged();
}
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
Logger.LogDebug("BuildRenderTree");
builder.AddContent(0, ChildContent);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Logger.LogDebug("Dispose: Value.PropertyChanged -= Value_PropertyChanged;");
Value.PropertyChanged -= Value_PropertyChanged;
}
}
}