Skip to content

Commit 20ce7d9

Browse files
authoredApr 20, 2021
Refined the data component semantically (#3560)
1 parent 002da3a commit 20ce7d9

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed
 

‎src/StrawberryShake/Client/src/Razor/DataComponent.cs

+57-1
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,83 @@
44

55
namespace StrawberryShake.Razor
66
{
7+
/// <summary>
8+
/// A data component can be used to remove boiler plate from using reactive data operations.
9+
/// </summary>
10+
/// <typeparam name="TClientOrOperation">
11+
/// The client or operation this component shall interact with.
12+
/// </typeparam>
713
public abstract class DataComponent<TClientOrOperation>
814
: ComponentBase
915
, IDisposable
1016
{
1117
private readonly List<IDisposable> _subscriptions = new();
1218
private bool _disposed;
1319

20+
/// <summary>
21+
/// Gets the client or operation.
22+
/// </summary>
1423
[Inject]
1524
protected internal TClientOrOperation ClientOrOperation { get; internal set; } = default!;
1625

17-
public void Subscribe(Func<TClientOrOperation, IDisposable> subscribe)
26+
/// <summary>
27+
/// Gets the data client.
28+
/// </summary>
29+
protected TClientOrOperation Client => ClientOrOperation;
30+
31+
/// <summary>
32+
/// Gets the data operation.
33+
/// </summary>
34+
protected TClientOrOperation Operation => ClientOrOperation;
35+
36+
/// <summary>
37+
/// Registers a data subscription with the component.
38+
/// The component will dispose any registered subscription when it is disposed.
39+
/// </summary>
40+
/// <param name="subscribe">
41+
/// The subscribe delegate creating a data subscription.
42+
/// </param>
43+
/// <exception cref="ArgumentNullException">
44+
/// <paramref name="subscribe"/> is <c>null</c>.
45+
/// </exception>
46+
public void Register(Func<TClientOrOperation, IDisposable> subscribe)
1847
{
48+
if (subscribe is null)
49+
{
50+
throw new ArgumentNullException(nameof(subscribe));
51+
}
52+
1953
_subscriptions.Add(subscribe(ClientOrOperation));
2054
}
2155

56+
/// <summary>
57+
/// Registers a data subscription with the component.
58+
/// The component will dispose any registered subscription when it is disposed.
59+
/// </summary>
60+
/// <param name="subscribe">
61+
/// The subscribe delegate creating a data subscription.
62+
/// </param>
63+
/// <exception cref="ArgumentNullException">
64+
/// <paramref name="subscribe"/> is <c>null</c>.
65+
/// </exception>
66+
[Obsolete("Use Register(Func<TClientOrOperation, IDisposable> subscribe)")]
67+
public void Subscribe(Func<TClientOrOperation, IDisposable> subscribe) =>
68+
Register(subscribe);
69+
70+
/// <summary>
71+
/// Performs application-defined tasks associated with freeing,
72+
/// releasing, or resetting unmanaged resources.
73+
/// </summary>
2274
public void Dispose()
2375
{
2476
Dispose(true);
2577
GC.SuppressFinalize(this);
2678
}
2779

80+
/// <summary>
81+
/// Performs application-defined tasks associated with freeing,
82+
/// releasing, or resetting unmanaged resources.
83+
/// </summary>
2884
protected void Dispose(bool disposing)
2985
{
3086
if (!_disposed)

0 commit comments

Comments
 (0)
Please sign in to comment.