blob: 26935691f3b638e46451d604f7b1a01c66598942 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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();
}
}
}
}
|