using System; using System.Collections.Generic; namespace UnityAtoms { internal class ObservableEvent : IObservable { private Action> _unregister; private List> _observers = new List>(); public ObservableEvent(Action> register, Action> unregister) { register(NotifyObservers); _unregister = unregister; } ~ObservableEvent() { if (_unregister != null) { _unregister(NotifyObservers); } } public IDisposable Subscribe(IObserver observer) { if (!_observers.Contains(observer)) _observers.Add(observer); return new ObservableUnsubscriber(_observers, observer); } private void NotifyObservers(T value) { for (int i = 0; _observers != null && i < _observers.Count; ++i) { _observers[i].OnNext(value); } } } }