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