Debian Codegraph @

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