unity-atoms/Packages/Core/Runtime/Observables/ObservableEvent.cs
Adam Ramberg 8a6b8a97a6
Added Variable Instancer, Event Reference, Atom Collection and Atom List (old Atom List renamed to Atom Value List) (#110)
AtomVariableInstancer
- Added AtomVariableInstancer as an option to AtomReference.
- Added AtomVariableInstancer to generator.
- Added editor icon for AtomVariableInstancer.

AtomEventReference
- Added an AtomEventReference class (and AtomEventX2Reference). It’s similar to an AtomReference, but for Events. Let’s you pick between an Event, Variable (will select the Changed event) and a VariableInstancer (see above).
- Added AtomEventReference and AtomEventX2Reference to generator.
- Added a drawer for AtomEventReference.
- Listeners are now using AtomEventReference instead of AtomEvent.
- Refactoring of VoidHooks since Listeners are now using AtomEventReference.

AtomCollection
- Created an AtomCollection - a collection of Atoms associated with key strings (AtomReferences).
- Added new editor icon for collections.
- Created a SerializableDictionary class, which AtomCollection is using.
- Custom property drawer for SerializableDictionary.
- SerializableDictionary supports nested structures meaning that a AtomCollection can have a KVP that is pointing to another AtomCollection.
- AtomCollections have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to collection - adding it to the collection when created (using gameObject’s instance id as key) and removing it from the collection when destroyed.

AtomList
- Renamed old AtomList to AtomValueList
- Added AtomList, like Collection, but a list
- Added new icon for AtomList
- Created a AtomBaseVariableList class, which AtomList is using.
- Custom property drawer for AtomBaseVariableList.
- AtomLists have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to list - adding it to the list when created and removing it from the list when destroyed.
2020-02-23 02:39:43 +01:00

78 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
namespace UnityAtoms
{
internal 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);
}
}
}
internal class ObservableEvent<T1, T2, M> : IObservable<M>
{
private Action<Action<T1, T2>> _unregister;
private List<IObserver<M>> _observers = new List<IObserver<M>>();
private Func<T1, T2, M> _createCombinedModel;
public ObservableEvent(Action<Action<T1, T2>> register, Action<Action<T1, T2>> unregister, Func<T1, T2, M> createCombinedModel)
{
register(NotifyObservers);
_unregister = unregister;
_createCombinedModel = createCombinedModel;
}
~ObservableEvent()
{
if (_unregister != null)
{
_unregister(NotifyObservers);
}
}
public IDisposable Subscribe(IObserver<M> observer)
{
if (!_observers.Contains(observer))
_observers.Add(observer);
return new ObservableUnsubscriber<M>(_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));
}
}
}
}