mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-23 00:28:23 -05:00
41 lines
1.0 KiB
C#
41 lines
1.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace UnityAtoms
|
|
{
|
|
public class ObservableEvent<T> : IObservable<T>
|
|
{
|
|
private Action<Action<T>> _unregister;
|
|
private List<IObserver<T>> _observers = new List<IObserver<T>>();
|
|
|
|
public ObservableEvent(Action<Action<T>> register, Action<Action<T>> unregister)
|
|
{
|
|
register(NotifyObservers);
|
|
_unregister = unregister;
|
|
}
|
|
|
|
~ObservableEvent()
|
|
{
|
|
if (_unregister != null)
|
|
{
|
|
_unregister(NotifyObservers);
|
|
}
|
|
}
|
|
|
|
public IDisposable Subscribe(IObserver<T> observer)
|
|
{
|
|
if (!_observers.Contains(observer))
|
|
_observers.Add(observer);
|
|
return new ObservableUnsubscriber<T>(_observers, observer);
|
|
}
|
|
|
|
private void NotifyObservers(T value)
|
|
{
|
|
for (int i = 0; _observers != null && i < _observers.Count; ++i)
|
|
{
|
|
_observers[i].OnNext(value);
|
|
}
|
|
}
|
|
}
|
|
}
|