mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-10-30 05:42:47 +01:00
41 lines
823 B
C#
41 lines
823 B
C#
using Serilog;
|
|
|
|
namespace Phantom.Utils.Events;
|
|
|
|
public abstract class ObservableState<T> {
|
|
public EventSubscribers<T> Subs { get; }
|
|
|
|
protected ObservableState(ILogger logger) {
|
|
Subs = new Subscribers(logger, this);
|
|
}
|
|
|
|
protected void Update() {
|
|
Subs.Publish(GetData());
|
|
}
|
|
|
|
protected bool UpdateIf(bool result) {
|
|
if (result) {
|
|
Update();
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
protected abstract T GetData();
|
|
|
|
private sealed class Subscribers : EventSubscribers<T> {
|
|
private readonly ObservableState<T> observer;
|
|
|
|
public Subscribers(ILogger logger, ObservableState<T> observer) : base(logger) {
|
|
this.observer = observer;
|
|
}
|
|
|
|
public override void Subscribe(object owner, Action<T> subscriber) {
|
|
base.Subscribe(owner, subscriber);
|
|
subscriber(observer.GetData());
|
|
}
|
|
}
|
|
}
|