Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/If.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.Collections.Generic;
6 using System.Reactive.Disposables;
7
8 namespace System.Reactive.Linq.ObservableImpl
9 {
10 class If<TResult> : Producer<TResult>, IEvaluatableObservable<TResult>
11 {
12 private readonly Func<bool> _condition;
13 private readonly IObservable<TResult> _thenSource;
14 private readonly IObservable<TResult> _elseSource;
15
16 public If(Func<bool> condition, IObservable<TResult> thenSource, IObservable<TResult> elseSource)
17 {
18 _condition = condition;
19 _thenSource = thenSource;
20 _elseSource = elseSource;
21 }
22
23 public IObservable<TResult> Eval()
24 {
25 return _condition() ? _thenSource : _elseSource;
26 }
27
28 protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
29 {
30 var sink = new _(this, observer, cancel);
31 setSink(sink);
32 return sink.Run();
33 }
34
35 class _ : Sink<TResult>, IObserver<TResult>
36 {
37 private readonly If<TResult> _parent;
38
39 public _(If<TResult> parent, IObserver<TResult> observer, IDisposable cancel)
40 : base(observer, cancel)
41 {
42 _parent = parent;
43 }
44
45 public IDisposable Run()
46 {
47 var result = default(IObservable<TResult>);
48 try
49 {
50 result = _parent.Eval();
51 }
52 catch (Exception exception)
53 {
54 base._observer.OnError(exception);
55 base.Dispose();
56 return Disposable.Empty;
57 }
58
59 return result.SubscribeSafe(this);
60 }
61
62 public void OnNext(TResult value)
63 {
64 base._observer.OnNext(value);
65 }
66
67 public void OnError(Exception error)
68 {
69 base._observer.OnError(error);
70 base.Dispose();
71 }
72
73 public void OnCompleted()
74 {
75 base._observer.OnCompleted();
76 base.Dispose();
77 }
78 }
79 }
80 }
81 #endif