/// <summary> /// Subscribes the given observer to the observable sequence. /// </summary> /// <param name="observer">Observer that will receive notifications from the observable sequence.</param> /// <returns>Disposable object representing an observer's subscription to the observable sequence.</returns> /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception> public IDisposable Subscribe(IObserver <T> observer) { if (observer == null) { throw new ArgumentNullException("observer"); } var autoDetachObserver = new AutoDetachObserver <T>(observer); try { autoDetachObserver.Disposable = SubscribeCore(autoDetachObserver); } catch (Exception exception) { // // This can happen when there's a synchronous callback to OnError in the // implementation of SubscribeCore, which also throws. So, we're seeing // an exception being thrown from a handler. // // For compat with v1.x, we rethrow the exception in this case, keeping // in mind this should be rare but if it happens, something's totally // screwed up. // if (!autoDetachObserver.Fail(exception)) { throw; } } return(autoDetachObserver); }
public void AutoDetachObserver_ImplementISafeObserver() { var myObserver = Observer.Create <int>(i => { i.Dump(); if (i == 1) { throw new Exception("triggered by observer"); } }, ex => ex.Message.ToString(), () => "completed".Dump()); var observer = new AutoDetachObserver <int>(myObserver); observer.SetResource(Disposable.Create(() => "obsrever exception i will clearnup by IDisposable Resource".Dump())); var observable = new MyObservable(); observable.Subscribe(observer); }
public IDisposable Subscribe(IObserver <T> observer) { if (observer == null) { throw new ArgumentNullException("observer"); } var autoDetachObserver = observer as AutoDetachObserver; if (autoDetachObserver == null) { autoDetachObserver = new AutoDetachObserver(observer); } var subscription = new Disposable(autoDetachObserver); autoDetachObserver.Add(subscription); Scheduler.CurrentThread.EnsureTrampoline(() => subscription.Set(subscribe(autoDetachObserver))); return(subscription); }
object gate = new object(); // protects disposable state public Disposable(AutoDetachObserver observer) { this.observer = observer; }