/// <inheritdoc/> public IDisposable Subscribe(ISensorObserver observer) { if (observer == null) { throw new ArgumentNullException("observer"); } lock (observerSync) { observers.Add(observer); return(new SensorSubscription(this, observer)); } }
public SensorSubscription(SensorObservable observable, ISensorObserver observer) { if (observable == null) { throw new ArgumentNullException("observable"); } if (observer == null) { throw new ArgumentNullException("observer"); } this.observable = observable; this.observer = observer; }
/// <summary> /// Unsubscribes the specified observer. /// </summary> /// <param name="observer">Observer to unsubscribe.</param> /// <returns>if the subscription was cancelled</returns> public bool Unsubscribe(ISensorObserver observer) { lock (observerSync) { int iObserver = observers.IndexOf(observer); if (iObserver < 0) { return(false); } // we receive an exception if an observer is removed while iterating through them // therefore we keep the original observer list untouched // by creating a new one that will be used for future operations int capacity = observers.Count; var nextObservers = new List <ISensorObserver>(capacity); for (int i = 0; i < capacity; i++) { if (i != iObserver) { nextObservers.Add(observers[iObserver]); } } observers = nextObservers; return(true); } }
public void subscribe(ISensorObserver iSensorObserver) { // observer += new EventHandler (iSensorObserver.Notify); }
///<inheritdoc/> public void RemoveObserver(ISensorObserver observer) { _sensorObservers.Remove(observer); }
///<inheritdoc/> public void RegisterObserver(ISensorObserver observer) { _sensorObservers.Add(observer); }
//Remove an observer front he list. public void removeObserver(ISensorObserver o) { observerList.Remove(o); }
//Addes and observer to the observer list. public void addObserver(ISensorObserver o) { observerList.Add(o); }