-
Notifications
You must be signed in to change notification settings - Fork 2
/
StateObservable.cs
66 lines (57 loc) · 2.53 KB
/
StateObservable.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
using Memento.Core.Internals;
using System.Collections.Concurrent;
namespace Memento.Core;
public interface IStateObservable<out TMessage> : IObservable<IStateChangedEventArgs>
where TMessage : notnull {
/// <summary>
/// Notifies observers that the state of the store has changed.
/// </summary>
void StateHasChanged();
}
public abstract class StateObservable<TMessage> : IStateObservable<TMessage>
where TMessage : notnull {
readonly ConcurrentDictionary<Guid, IObserver<IStateChangedEventArgs>> _observers = new();
/// <summary>
/// Notifies observers that the state of the store has changed.
/// </summary>
/// <param name="message">The optional message associated with the state change.</param>
public void StateHasChanged(TMessage? message = default) {
InvokeObserver(new StateChangedEventArgs() {
Message = message,
Sender = this,
StateChangeType = StateChangeType.StateHasChanged,
});
}
void IStateObservable<TMessage>.StateHasChanged() {
StateHasChanged(default);
}
/// <summary>
/// Subscribes an observer to receive state change notifications.
/// </summary>
/// <param name="observer">The observer to subscribe.</param>
/// <returns>An IDisposable object that can be used to unsubscribe the observer.</returns>
public IDisposable Subscribe(IObserver<IStateChangedEventArgs> observer) {
var id = Guid.NewGuid();
if (_observers.TryAdd(id, observer) is false) {
throw new InvalidOperationException("Failed to subscribe observer");
}
return new StoreSubscription(GetType().FullName ?? "StateObservable.Subscribe", () => {
if (_observers.TryRemove(new(id, observer)) is false) {
throw new InvalidOperationException("Failed to unsubscribe observer");
}
});
}
/// <summary>
/// Subscribes an action as an observer to receive state change notifications.
/// </summary>
/// <param name="observer">The action to subscribe as an observer.</param>
/// <returns>An IDisposable object that can be used to unsubscribe the observer.</returns>
public IDisposable Subscribe(Action<IStateChangedEventArgs> observer) {
return Subscribe(new GeneralObserver<IStateChangedEventArgs>(observer));
}
internal void InvokeObserver(IStateChangedEventArgs e) {
foreach (var (_, obs) in _observers) {
obs.OnNext(e);
}
}
}