diff options
author | Atsushi Eno <atsushieno@veritas-vos-liberabit.com> | 2013-01-22 12:25:22 +0400 |
---|---|---|
committer | Atsushi Eno <atsushieno@veritas-vos-liberabit.com> | 2013-01-22 12:25:22 +0400 |
commit | cde9fc6a8fe569203cb991121a35c2a9c7f4c420 (patch) | |
tree | 8633a637be4973b221d9c7e9378af5e0a08b5670 /Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SafeObserver.cs | |
parent | 8911e1d3f169a0e378b4e237926269d9218c8fd3 (diff) |
import 2b5dbddd740b, new directory structure in the original rx.
Diffstat (limited to 'Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SafeObserver.cs')
-rw-r--r-- | Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SafeObserver.cs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SafeObserver.cs b/Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SafeObserver.cs new file mode 100644 index 0000000..2693569 --- /dev/null +++ b/Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SafeObserver.cs @@ -0,0 +1,71 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +using System; + +namespace System.Reactive +{ + // + // See AutoDetachObserver.cs for more information on the safeguarding requirement and + // its implementation aspects. + // + + class SafeObserver<TSource> : IObserver<TSource> + { + private readonly IObserver<TSource> _observer; + private readonly IDisposable _disposable; + + public static IObserver<TSource> Create(IObserver<TSource> observer, IDisposable disposable) + { + var a = observer as AnonymousObserver<TSource>; + if (a != null) + return a.MakeSafe(disposable); + else + return new SafeObserver<TSource>(observer, disposable); + } + + private SafeObserver(IObserver<TSource> observer, IDisposable disposable) + { + _observer = observer; + _disposable = disposable; + } + + public void OnNext(TSource value) + { + var __noError = false; + try + { + _observer.OnNext(value); + __noError = true; + } + finally + { + if (!__noError) + _disposable.Dispose(); + } + } + + public void OnError(Exception error) + { + try + { + _observer.OnError(error); + } + finally + { + _disposable.Dispose(); + } + } + + public void OnCompleted() + { + try + { + _observer.OnCompleted(); + } + finally + { + _disposable.Dispose(); + } + } + } +} |