Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/Join.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 Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> : Producer<TResult>
11 {
12 private readonly IObservable<TLeft> _left;
13 private readonly IObservable<TRight> _right;
14 private readonly Func<TLeft, IObservable<TLeftDuration>> _leftDurationSelector;
15 private readonly Func<TRight, IObservable<TRightDuration>> _rightDurationSelector;
16 private readonly Func<TLeft, TRight, TResult> _resultSelector;
17
18 public Join(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, IObservable<TLeftDuration>> leftDurationSelector, Func<TRight, IObservable<TRightDuration>> rightDurationSelector, Func<TLeft, TRight, TResult> resultSelector)
19 {
20 _left = left;
21 _right = right;
22 _leftDurationSelector = leftDurationSelector;
23 _rightDurationSelector = rightDurationSelector;
24 _resultSelector = resultSelector;
25 }
26
27 protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
28 {
29 var sink = new _(this, observer, cancel);
30 setSink(sink);
31 return sink.Run();
32 }
33
34 class _ : Sink<TResult>
35 {
36 private readonly Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> _parent;
37
38 public _(Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
39 : base(observer, cancel)
40 {
41 _parent = parent;
42 }
43
44 private object _gate;
45 private CompositeDisposable _group;
46
47 private bool _leftDone;
48 private int _leftID;
49 private Dictionary<int, TLeft> _leftMap;
50
51 private bool _rightDone;
52 private int _rightID;
53 private Dictionary<int, TRight> _rightMap;
54
55 public IDisposable Run()
56 {
57 _gate = new object();
58 _group = new CompositeDisposable();
59
60 var leftSubscription = new SingleAssignmentDisposable();
61 _group.Add(leftSubscription);
62 _leftDone = false;
63 _leftID = 0;
64 _leftMap = new Dictionary<int, TLeft>();
65
66 var rightSubscription = new SingleAssignmentDisposable();
67 _group.Add(rightSubscription);
68 _rightDone = false;
69 _rightID = 0;
70 _rightMap = new Dictionary<int, TRight>();
71
72 leftSubscription.Disposable = _parent._left.SubscribeSafe(new LeftObserver(this, leftSubscription));
73 rightSubscription.Disposable = _parent._right.SubscribeSafe(new RightObserver(this, rightSubscription));
74
75 return _group;
76 }
77
78 class LeftObserver : IObserver<TLeft>
79 {
80 private readonly _ _parent;
81 private readonly IDisposable _self;
82
83 public LeftObserver(_ parent, IDisposable self)
84 {
85 _parent = parent;
86 _self = self;
87 }
88
89 private void Expire(int id, IDisposable resource)
90 {
91 lock (_parent._gate)
92 {
93 if (_parent._leftMap.Remove(id) && _parent._leftMap.Count == 0 && _parent._leftDone)
94 {
95 _parent._observer.OnCompleted();
96 _parent.Dispose();
97 }
98 }
99
100 _parent._group.Remove(resource);
101 }
102
103 public void OnNext(TLeft value)
104 {
105 var id = 0;
106 lock (_parent._gate)
107 {
108 id = _parent._leftID++;
109 _parent._leftMap.Add(id, value);
110 }
111
112 var md = new SingleAssignmentDisposable();
113 _parent._group.Add(md);
114
115 var duration = default(IObservable<TLeftDuration>);
116 try
117 {
118 duration = _parent._parent._leftDurationSelector(value);
119 }
120 catch (Exception exception)
121 {
122 _parent._observer.OnError(exception);
123 _parent.Dispose();
124 return;
125 }
126
127 md.Disposable = duration.SubscribeSafe(new Delta(this, id, md));
128
129 lock (_parent._gate)
130 {
131 foreach (var rightValue in _parent._rightMap.Values)
132 {
133 var result = default(TResult);
134 try
135 {
136 result = _parent._parent._resultSelector(value, rightValue);
137 }
138 catch (Exception exception)
139 {
140 _parent._observer.OnError(exception);
141 _parent.Dispose();
142 return;
143 }
144
145 _parent._observer.OnNext(result);
146 }
147 }
148 }
149
150 class Delta : IObserver<TLeftDuration>
151 {
152 private readonly LeftObserver _parent;
153 private readonly int _id;
154 private readonly IDisposable _self;
155
156 public Delta(LeftObserver parent, int id, IDisposable self)
157 {
158 _parent = parent;
159 _id = id;
160 _self = self;
161 }
162
163 public void OnNext(TLeftDuration value)
164 {
165 _parent.Expire(_id, _self);
166 }
167
168 public void OnError(Exception error)
169 {
170 _parent.OnError(error);
171 }
172
173 public void OnCompleted()
174 {
175 _parent.Expire(_id, _self);
176 }
177 }
178
179 public void OnError(Exception error)
180 {
181 lock (_parent._gate)
182 {
183 _parent._observer.OnError(error);
184 _parent.Dispose();
185 }
186 }
187
188 public void OnCompleted()
189 {
190 lock (_parent._gate)
191 {
192 _parent._leftDone = true;
193 if (_parent._rightDone || _parent._leftMap.Count == 0)
194 {
195 _parent._observer.OnCompleted();
196 _parent.Dispose();
197 }
198 else
199 {
200 _self.Dispose();
201 }
202 }
203 }
204 }
205
206 class RightObserver : IObserver<TRight>
207 {
208 private readonly _ _parent;
209 private readonly IDisposable _self;
210
211 public RightObserver(_ parent, IDisposable self)
212 {
213 _parent = parent;
214 _self = self;
215 }
216
217 private void Expire(int id, IDisposable resource)
218 {
219 lock (_parent._gate)
220 {
221 if (_parent._rightMap.Remove(id) && _parent._rightMap.Count == 0 && _parent._rightDone)
222 {
223 _parent._observer.OnCompleted();
224 _parent.Dispose();
225 }
226 }
227
228 _parent._group.Remove(resource);
229 }
230
231 public void OnNext(TRight value)
232 {
233 var id = 0;
234 lock (_parent._gate)
235 {
236 id = _parent._rightID++;
237 _parent._rightMap.Add(id, value);
238 }
239
240 var md = new SingleAssignmentDisposable();
241 _parent._group.Add(md);
242
243 var duration = default(IObservable<TRightDuration>);
244 try
245 {
246 duration = _parent._parent._rightDurationSelector(value);
247 }
248 catch (Exception exception)
249 {
250 _parent._observer.OnError(exception);
251 _parent.Dispose();
252 return;
253 }
254
255 md.Disposable = duration.SubscribeSafe(new Delta(this, id, md));
256
257 lock (_parent._gate)
258 {
259 foreach (var leftValue in _parent._leftMap.Values)
260 {
261 var result = default(TResult);
262 try
263 {
264 result = _parent._parent._resultSelector(leftValue, value);
265 }
266 catch (Exception exception)
267 {
268 _parent._observer.OnError(exception);
269 _parent.Dispose();
270 return;
271 }
272
273 _parent._observer.OnNext(result);
274 }
275 }
276 }
277
278 class Delta : IObserver<TRightDuration>
279 {
280 private readonly RightObserver _parent;
281 private readonly int _id;
282 private readonly IDisposable _self;
283
284 public Delta(RightObserver parent, int id, IDisposable self)
285 {
286 _parent = parent;
287 _id = id;
288 _self = self;
289 }
290
291 public void OnNext(TRightDuration value)
292 {
293 _parent.Expire(_id, _self);
294 }
295
296 public void OnError(Exception error)
297 {
298 _parent.OnError(error);
299 }
300
301 public void OnCompleted()
302 {
303 _parent.Expire(_id, _self);
304 }
305 }
306
307 public void OnError(Exception error)
308 {
309 lock (_parent._gate)
310 {
311 _parent._observer.OnError(error);
312 _parent.Dispose();
313 }
314 }
315
316 public void OnCompleted()
317 {
318 lock (_parent._gate)
319 {
320 _parent._rightDone = true;
321 if (_parent._leftDone || _parent._rightMap.Count == 0)
322 {
323 _parent._observer.OnCompleted();
324 _parent.Dispose();
325 }
326 else
327 {
328 _self.Dispose();
329 }
330 }
331 }
332 }
333 }
334 }
335 }
336 #endif