mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-08-18 18:24:56 +02:00
.config
.run
.workdir
Agent
Common
Docker
Server
Utils
Phantom.Utils.Collections
Phantom.Utils.Collections.Tests
Phantom.Utils.Cryptography
Phantom.Utils.Events
EventSubscribers.cs
ObservableState.cs
Phantom.Utils.Events.csproj
Phantom.Utils.IO
Phantom.Utils.Rpc
Phantom.Utils.Runtime
Phantom.Utils.Runtime.Tests
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
PhantomPanel.sln
global.json
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());
|
|
}
|
|
}
|
|
}
|