-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathFluxorComponent.cs
101 lines (90 loc) · 2.75 KB
/
FluxorComponent.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Fluxor.UnsupportedClasses;
using Microsoft.AspNetCore.Components;
using System;
using System.Threading.Tasks;
namespace Fluxor.Blazor.Web.Components;
/// <summary>
/// A component that auto-subscribes to state changes on all <see cref="IStateChangedNotifier"/> properties
/// and ensures <see cref="ComponentBase.StateHasChanged"/> is called
/// </summary>
public abstract class FluxorComponent : ComponentBase, IAsyncDisposable
{
[Inject]
private IActionSubscriber ActionSubscriber { get; set; }
private bool Disposed;
private IDisposable StateSubscription;
private readonly ThrottledInvoker StateHasChangedThrottler;
/// <summary>
/// Creates a new instance
/// </summary>
public FluxorComponent()
{
StateHasChangedThrottler = new ThrottledInvoker(() =>
{
if (!Disposed)
InvokeAsync(StateHasChanged);
});
}
/// <summary>
/// If greater than 0, the feature will not execute state changes
/// more often than this many times per second. Additional notifications
/// will be supressed, and observers will be notified of the latest
/// state when the time window has elapsed to allow another notification.
/// </summary>
protected byte MaximumStateChangedNotificationsPerSecond { get; set; }
/// <see cref="IActionSubscriber.SubscribeToAction{TAction}(object, Action{TAction})"/>
public void SubscribeToAction<TAction>(Action<TAction> callback)
{
ActionSubscriber.SubscribeToAction<TAction>(this, action =>
{
InvokeAsync(() =>
{
if (!Disposed)
callback(action);
StateHasChanged();
});
});
}
/// <summary>
/// Disposes of the component and unsubscribes from any state
/// </summary>
public async ValueTask DisposeAsync()
{
if (Disposed)
return;
Disposed = true;
StateHasChangedThrottler.Dispose();
await DisposeAsyncCore(true).ConfigureAwait(false);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes via IAsyncDisposable
/// </summary>
/// <param name="disposing">true if called manually, otherwise false</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">
/// Thrown when a descendant overrides DisposeAsyncCore and does call base.
/// </exception>
protected virtual ValueTask DisposeAsyncCore(bool disposing)
{
if (disposing)
{
if (StateSubscription is null)
throw new NullReferenceException(ErrorMessages.ForgottenToCallBaseOnInitialized);
StateSubscription.Dispose();
ActionSubscriber?.UnsubscribeFromAllActions(this);
}
return ValueTask.CompletedTask;
}
/// <summary>
/// Subscribes to state properties
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
StateSubscription = StateSubscriber.Subscribe(this, _ =>
{
StateHasChangedThrottler.Invoke(MaximumStateChangedNotificationsPerSecond);
});
}
}