Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/Finally.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 Finally<TSource> : Producer<TSource>
10 {
11 private readonly IObservable<TSource> _source;
12 private readonly Action _finallyAction;
13
14 public Finally(IObservable<TSource> source, Action finallyAction)
15 {
16 _source = source;
17 _finallyAction = finallyAction;
18 }
19
20 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
21 {
22 var sink = new _(this, observer, cancel);
23 setSink(sink);
24 return sink.Run();
25 }
26
27 class _ : Sink<TSource>, IObserver<TSource>
28 {
29 private readonly Finally<TSource> _parent;
30
31 public _(Finally<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
32 : base(observer, cancel)
33 {
34 _parent = parent;
35 }
36
37 public IDisposable Run()
38 {
39 var subscription = _parent._source.SubscribeSafe(this);
40
41 return Disposable.Create(() =>
42 {
43 try
44 {
45 subscription.Dispose();
46 }
47 finally
48 {
49 _parent._finallyAction();
50 }
51 });
52 }
53
54 public void OnNext(TSource value)
55 {
56 base._observer.OnNext(value);
57 }
58
59 public void OnError(Exception error)
60 {
61 base._observer.OnError(error);
62 base.Dispose();
63 }
64
65 public void OnCompleted()
66 {
67 base._observer.OnCompleted();
68 base.Dispose();
69 }
70 }
71 }
72 }
73 #endif