public ObserverHandle Alloc() { var handle = new ObserverHandle(); Add(handle); return(handle); }
public ObserverHandle Observe <T>(IObserverList <T> observable, T observer) { ObserverHandle handle = null; Observe(ref handle, observable, observer); return(handle); }
public void Unobserve(ObserverHandle handle) { if (handle != null) { handle.Dispose(); handles.Remove(handle); } }
public void Add(ref ObserverHandle handle, T observer) { if (handle == null) { handle = new ObserverHandle(); } DebugOnly.Check(observer != null, "Observer is null."); DebugOnly.Check(handle.List == null, "Handle is already in use."); handle.Index = list.Count; handle.List = this; list.Add(new Item { handle = handle, observer = observer }); }
public void Remove(ObserverHandle handle) { DebugOnly.Check(handle != null, "Handle is null."); DebugOnly.Check(handle.List == this, "Handle is not registered with this list."); int lastIndex = list.Count - 1; if (handle.Index != lastIndex) { var replacement = list[lastIndex]; list[handle.Index] = replacement; replacement.handle.Index = handle.Index; } handle.List = null; list.RemoveAt(lastIndex); }
void IObserverList.Add(ref ObserverHandle handle, object observer) { if (observer is T typedObserver) { Add(ref handle, typedObserver); } else { if (observer == null) { DebugOnly.Error("Observer is null."); } else { DebugOnly.Error($"Was expecting type {typeof(T)}, got type {observer.GetType().Name}."); } } }
public void Observe <T>(ref ObserverHandle handle, IObserverList <T> observable, T observer) { observable.Add(ref handle, observer); Add(handle); }
public void Add(ObserverHandle handle) { handles.Add(handle); }
protected void Unobserve(ObserverHandle handle) { Observers.Unobserve(handle); }
protected void Observe <O>(ref ObserverHandle handle, IObserverList <O> observable) where O : class { Observers.Observe(ref handle, observable, this as O); }