blob: 4737504f1025d1b6bb2ccb2f49206e435a80a9b9 (
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
|
#if !NO_PERF
using System.Threading;
namespace System.Reactive
{
/// <summary>
/// Base class for implementation of query operators, providing a lightweight sink that can be disposed to mute the outgoing observer.
/// </summary>
/// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam>
/// <remarks>Implementations of sinks are responsible to enforce the message grammar on the associated observer. Upon sending a terminal message, a pairing Dispose call should be made to trigger cancellation of related resources and to mute the outgoing observer.</remarks>
abstract class Sink<TSource> : IDisposable
{
protected volatile IObserver<TSource> _observer;
private IDisposable _cancel;
public Sink(IObserver<TSource> observer, IDisposable cancel)
{
_observer = observer;
_cancel = cancel;
}
public virtual void Dispose()
{
_observer = NopObserver<TSource>.Instance;
var cancel = Interlocked.Exchange(ref _cancel, null);
if (cancel != null)
{
cancel.Dispose();
}
}
public IObserver<TSource> GetForwarder()
{
return new _(this);
}
class _ : IObserver<TSource>
{
private readonly Sink<TSource> _forward;
public _(Sink<TSource> forward)
{
_forward = forward;
}
public void OnNext(TSource value)
{
_forward._observer.OnNext(value);
}
public void OnError(Exception error)
{
_forward._observer.OnError(error);
_forward.Dispose();
}
public void OnCompleted()
{
_forward._observer.OnCompleted();
_forward.Dispose();
}
}
}
}
#endif
|