Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/Sample.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.Concurrency;
6 using System.Reactive.Disposables;
7
8 namespace System.Reactive.Linq.ObservableImpl
9 {
10 class Sample<TSource, TSample> : Producer<TSource>
11 {
12 private readonly IObservable<TSource> _source;
13 private readonly IObservable<TSample> _sampler;
14
15 public Sample(IObservable<TSource> source, IObservable<TSample> sampler)
16 {
17 _source = source;
18 _sampler = sampler;
19 }
20
21 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
22 {
23 var sink = new _(this, observer, cancel);
24 setSink(sink);
25 return sink.Run();
26 }
27
28 class _ : Sink<TSource>, IObserver<TSource>
29 {
30 private readonly Sample<TSource, TSample> _parent;
31
32 public _(Sample<TSource, TSample> parent, IObserver<TSource> observer, IDisposable cancel)
33 : base(observer, cancel)
34 {
35 _parent = parent;
36 }
37
38 private object _gate;
39
40 private IDisposable _sourceSubscription;
41
42 private bool _hasValue;
43 private TSource _value;
44 private bool _atEnd;
45
46 public IDisposable Run()
47 {
48 _gate = new object();
49
50 var sourceSubscription = new SingleAssignmentDisposable();
51 _sourceSubscription = sourceSubscription;
52 sourceSubscription.Disposable = _parent._source.SubscribeSafe(this);
53
54 var samplerSubscription = _parent._sampler.SubscribeSafe(new SampleImpl(this));
55
56 return new CompositeDisposable(_sourceSubscription, samplerSubscription);
57 }
58
59 public void OnNext(TSource value)
60 {
61 lock (_gate)
62 {
63 _hasValue = true;
64 _value = value;
65 }
66 }
67
68 public void OnError(Exception error)
69 {
70 lock (_gate)
71 {
72 base._observer.OnError(error);
73 base.Dispose();
74 }
75 }
76
77 public void OnCompleted()
78 {
79 lock (_gate)
80 {
81 _atEnd = true;
82 _sourceSubscription.Dispose();
83 }
84 }
85
86 class SampleImpl : IObserver<TSample>
87 {
88 private readonly _ _parent;
89
90 public SampleImpl(_ parent)
91 {
92 _parent = parent;
93 }
94
95 public void OnNext(TSample value)
96 {
97 lock (_parent._gate)
98 {
99 if (_parent._hasValue)
100 {
101 _parent._hasValue = false;
102 _parent._observer.OnNext(_parent._value);
103 }
104
105 if (_parent._atEnd)
106 {
107 _parent._observer.OnCompleted();
108 _parent.Dispose();
109 }
110 }
111 }
112
113 public void OnError(Exception error)
114 {
115 // BREAKING CHANGE v2 > v1.x - This error used to be swallowed
116 lock (_parent._gate)
117 {
118 _parent._observer.OnError(error);
119 _parent.Dispose();
120 }
121 }
122
123 public void OnCompleted()
124 {
125 lock (_parent._gate)
126 {
127 if (_parent._hasValue)
128 {
129 _parent._hasValue = false;
130 _parent._observer.OnNext(_parent._value);
131 }
132
133 if (_parent._atEnd)
134 {
135 _parent._observer.OnCompleted();
136 _parent.Dispose();
137 }
138 }
139 }
140 }
141 }
142 }
143
144 class Sample<TSource> : Producer<TSource>
145 {
146 private readonly IObservable<TSource> _source;
147 private readonly TimeSpan _interval;
148 private readonly IScheduler _scheduler;
149
150 public Sample(IObservable<TSource> source, TimeSpan interval, IScheduler scheduler)
151 {
152 _source = source;
153 _interval = interval;
154 _scheduler = scheduler;
155 }
156
157 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
158 {
159 var sink = new _(this, observer, cancel);
160 setSink(sink);
161 return sink.Run();
162 }
163
164 class _ : Sink<TSource>, IObserver<TSource>
165 {
166 private readonly Sample<TSource> _parent;
167
168 public _(Sample<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
169 : base(observer, cancel)
170 {
171 _parent = parent;
172 }
173
174 private object _gate;
175
176 private IDisposable _sourceSubscription;
177
178 private bool _hasValue;
179 private TSource _value;
180 private bool _atEnd;
181
182 public IDisposable Run()
183 {
184 _gate = new object();
185
186 var sourceSubscription = new SingleAssignmentDisposable();
187 _sourceSubscription = sourceSubscription;
188 sourceSubscription.Disposable = _parent._source.SubscribeSafe(this);
189
190 return new CompositeDisposable(
191 sourceSubscription,
192 _parent._scheduler.SchedulePeriodic(_parent._interval, Tick)
193 );
194 }
195
196 private void Tick()
197 {
198 lock (_gate)
199 {
200 if (_hasValue)
201 {
202 _hasValue = false;
203 base._observer.OnNext(_value);
204 }
205
206 if (_atEnd)
207 {
208 base._observer.OnCompleted();
209 base.Dispose();
210 }
211 }
212 }
213
214 public void OnNext(TSource value)
215 {
216 lock (_gate)
217 {
218 _hasValue = true;
219 _value = value;
220 }
221 }
222
223 public void OnError(Exception error)
224 {
225 lock (_gate)
226 {
227 base._observer.OnError(error);
228 base.Dispose();
229 }
230 }
231
232 public void OnCompleted()
233 {
234 lock (_gate)
235 {
236 _atEnd = true;
237 _sourceSubscription.Dispose();
238 }
239 }
240 }
241 }
242 }
243 #endif