public IDisposable Subscribe(IObserver <T> observer) { if (observer == null) { throw new ArgumentNullException(nameof(observer)); } if (_error != null) { observer.OnError(_error); return(Disposable.Nop); } if (_completed) { observer.OnCompleted(); return(Disposable.Nop); } Observers.Add(observer); return(Delegate.Disposable(() => { var observers = Observers; // Could do the following to find the index of the // the observer: // // var i = observers.FindIndex(o => o == observer); // // but it would require a closure allocation. for (var i = 0; i < observers.Count; i++) { if (observers[i] == observer) { if (_shouldDeleteObserver) { observers[i] = null; } else { observers.RemoveAt(i); } break; } } })); }
public IDisposable Subscribe(IObserver <T> observer) { if (observer == null) { throw new ArgumentNullException(nameof(observer)); } if (_error != null) { observer.OnError(_error); return(Disposable.Nop); } if (_completed) { observer.OnCompleted(); return(Disposable.Nop); } Observers.Add(observer); return(Delegate.Disposable(() => Observers.Remove(observer))); }