using System; using System.Collections.Generic; using UnityEngine; 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); } } } internal class ObservableEvent : IObservable { private Action> _unregister; private List> _observers = new List>(); private Func _createCombinedModel; public ObservableEvent(Action> register, Action> unregister, Func createCombinedModel) { register(NotifyObservers); _unregister = unregister; _createCombinedModel = createCombinedModel; } ~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(T1 value1, T2 value2) { for (int i = 0; _observers != null && i < _observers.Count; ++i) { _observers[i].OnNext(_createCombinedModel(value1, value2)); } } } }