Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SafeObserver.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System;
4
5 namespace System.Reactive
6 {
7 //
8 // See AutoDetachObserver.cs for more information on the safeguarding requirement and
9 // its implementation aspects.
10 //
11
12 class SafeObserver<TSource> : IObserver<TSource>
13 {
14 private readonly IObserver<TSource> _observer;
15 private readonly IDisposable _disposable;
16
17 public static IObserver<TSource> Create(IObserver<TSource> observer, IDisposable disposable)
18 {
19 var a = observer as AnonymousObserver<TSource>;
20 if (a != null)
21 return a.MakeSafe(disposable);
22 else
23 return new SafeObserver<TSource>(observer, disposable);
24 }
25
26 private SafeObserver(IObserver<TSource> observer, IDisposable disposable)
27 {
28 _observer = observer;
29 _disposable = disposable;
30 }
31
32 public void OnNext(TSource value)
33 {
34 var __noError = false;
35 try
36 {
37 _observer.OnNext(value);
38 __noError = true;
39 }
40 finally
41 {
42 if (!__noError)
43 _disposable.Dispose();
44 }
45 }
46
47 public void OnError(Exception error)
48 {
49 try
50 {
51 _observer.OnError(error);
52 }
53 finally
54 {
55 _disposable.Dispose();
56 }
57 }
58
59 public void OnCompleted()
60 {
61 try
62 {
63 _observer.OnCompleted();
64 }
65 finally
66 {
67 _disposable.Dispose();
68 }
69 }
70 }
71 }