// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #if !NO_PERF using System; using System.Reactive.Disposables; namespace System.Reactive.Linq.Observαble { class Finally : Producer { private readonly IObservable _source; private readonly Action _finallyAction; public Finally(IObservable source, Action finallyAction) { _source = source; _finallyAction = finallyAction; } protected override IDisposable Run(IObserver observer, IDisposable cancel, Action setSink) { var sink = new _(this, observer, cancel); setSink(sink); return sink.Run(); } class _ : Sink, IObserver { private readonly Finally _parent; public _(Finally parent, IObserver observer, IDisposable cancel) : base(observer, cancel) { _parent = parent; } public IDisposable Run() { var subscription = _parent._source.SubscribeSafe(this); return Disposable.Create(() => { try { subscription.Dispose(); } finally { _parent._finallyAction(); } }); } public void OnNext(TSource value) { base._observer.OnNext(value); } public void OnError(Exception error) { base._observer.OnError(error); base.Dispose(); } public void OnCompleted() { base._observer.OnCompleted(); base.Dispose(); } } } } #endif