Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/Where.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
6 namespace System.Reactive.Linq.ObservableImpl
7 {
8 class Where<TSource> : Producer<TSource>
9 {
10 private readonly IObservable<TSource> _source;
11 private readonly Func<TSource, bool> _predicate;
12 private readonly Func<TSource, int, bool> _predicateI;
13
14 public Where(IObservable<TSource> source, Func<TSource, bool> predicate)
15 {
16 _source = source;
17 _predicate = predicate;
18 }
19
20 public Where(IObservable<TSource> source, Func<TSource, int, bool> predicate)
21 {
22 _source = source;
23 _predicateI = predicate;
24 }
25
26 public IObservable<TSource> Omega(Func<TSource, bool> predicate)
27 {
28 if (_predicate != null)
29 return new Where<TSource>(_source, x => _predicate(x) && predicate(x));
30 else
31 return new Where<TSource>(this, predicate);
32 }
33
34 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
35 {
36 if (_predicate != null)
37 {
38 var sink = new _(this, observer, cancel);
39 setSink(sink);
40 return _source.SubscribeSafe(sink);
41 }
42 else
43 {
44 var sink = new WhereImpl(this, observer, cancel);
45 setSink(sink);
46 return _source.SubscribeSafe(sink);
47 }
48 }
49
50 class _ : Sink<TSource>, IObserver<TSource>
51 {
52 private readonly Where<TSource> _parent;
53
54 public _(Where<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
55 : base(observer, cancel)
56 {
57 _parent = parent;
58 }
59
60 public void OnNext(TSource value)
61 {
62 var shouldRun = default(bool);
63 try
64 {
65 shouldRun = _parent._predicate(value);
66 }
67 catch (Exception exception)
68 {
69 base._observer.OnError(exception);
70 base.Dispose();
71 return;
72 }
73
74 if (shouldRun)
75 base._observer.OnNext(value);
76 }
77
78 public void OnError(Exception error)
79 {
80 base._observer.OnError(error);
81 base.Dispose();
82 }
83
84 public void OnCompleted()
85 {
86 base._observer.OnCompleted();
87 base.Dispose();
88 }
89 }
90
91 class WhereImpl : Sink<TSource>, IObserver<TSource>
92 {
93 private readonly Where<TSource> _parent;
94 private int _index;
95
96 public WhereImpl(Where<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
97 : base(observer, cancel)
98 {
99 _parent = parent;
100 _index = 0;
101 }
102
103 public void OnNext(TSource value)
104 {
105 var shouldRun = default(bool);
106 try
107 {
108 shouldRun = _parent._predicateI(value, checked(_index++));
109 }
110 catch (Exception exception)
111 {
112 base._observer.OnError(exception);
113 base.Dispose();
114 return;
115 }
116
117 if (shouldRun)
118 base._observer.OnNext(value);
119 }
120
121 public void OnError(Exception error)
122 {
123 base._observer.OnError(error);
124 base.Dispose();
125 }
126
127 public void OnCompleted()
128 {
129 base._observer.OnCompleted();
130 base.Dispose();
131 }
132 }
133 }
134 }
135 #endif