Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/AddRef.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_PERF
4 using System;
5 using System.Reactive.Disposables;
6
7 namespace System.Reactive.Linq.ObservableImpl
8 {
9 class AddRef<TSource> : Producer<TSource>
10 {
11 private readonly IObservable<TSource> _source;
12 private readonly RefCountDisposable _refCount;
13
14 public AddRef(IObservable<TSource> source, RefCountDisposable refCount)
15 {
16 _source = source;
17 _refCount = refCount;
18 }
19
20 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
21 {
22 var d = new CompositeDisposable(_refCount.GetDisposable(), cancel);
23
24 var sink = new _(observer, d);
25 setSink(sink);
26 return _source.SubscribeSafe(sink);
27 }
28
29 class _ : Sink<TSource>, IObserver<TSource>
30 {
31 public _(IObserver<TSource> observer, IDisposable cancel)
32 : base(observer, cancel)
33 {
34 }
35
36 public void OnNext(TSource value)
37 {
38 base._observer.OnNext(value);
39 }
40
41 public void OnError(Exception error)
42 {
43 base._observer.OnError(error);
44 base.Dispose();
45 }
46
47 public void OnCompleted()
48 {
49 base._observer.OnCompleted();
50 base.Dispose();
51 }
52 }
53 }
54 }
55 #endif