Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/GroupJoin.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 using System.Reactive.Subjects;
8
9 namespace System.Reactive.Linq.ObservableImpl
10 {
11 class GroupJoin<TLeft, TRight, TLeftDuration, TRightDuration, TResult> : Producer<TResult>
12 {
13 private readonly IObservable<TLeft> _left;
14 private readonly IObservable<TRight> _right;
15 private readonly Func<TLeft, IObservable<TLeftDuration>> _leftDurationSelector;
16 private readonly Func<TRight, IObservable<TRightDuration>> _rightDurationSelector;
17 private readonly Func<TLeft, IObservable<TRight>, TResult> _resultSelector;
18
19 public GroupJoin(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, IObservable<TLeftDuration>> leftDurationSelector, Func<TRight, IObservable<TRightDuration>> rightDurationSelector, Func<TLeft, IObservable<TRight>, TResult> resultSelector)
20 {
21 _left = left;
22 _right = right;
23 _leftDurationSelector = leftDurationSelector;
24 _rightDurationSelector = rightDurationSelector;
25 _resultSelector = resultSelector;
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>
36 {
37 private readonly GroupJoin<TLeft, TRight, TLeftDuration, TRightDuration, TResult> _parent;
38
39 public _(GroupJoin<TLeft, TRight, TLeftDuration, TRightDuration, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
40 : base(observer, cancel)
41 {
42 _parent = parent;
43 }
44
45 private object _gate;
46 private CompositeDisposable _group;
47 private RefCountDisposable _refCount;
48
49 private int _leftID;
50 private Dictionary<int, IObserver<TRight>> _leftMap;
51
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 _refCount = new RefCountDisposable(_group);
60
61 var leftSubscription = new SingleAssignmentDisposable();
62 _group.Add(leftSubscription);
63 _leftID = 0;
64 _leftMap = new Dictionary<int, IObserver<TRight>>();
65
66 var rightSubscription = new SingleAssignmentDisposable();
67 _group.Add(rightSubscription);
68 _rightID = 0;
69 _rightMap = new Dictionary<int, TRight>();
70
71 leftSubscription.Disposable = _parent._left.SubscribeSafe(new LeftObserver(this, leftSubscription));
72 rightSubscription.Disposable = _parent._right.SubscribeSafe(new RightObserver(this, rightSubscription));
73
74 return _refCount;
75 }
76
77 class LeftObserver : IObserver<TLeft>
78 {
79 private readonly _ _parent;
80 private readonly IDisposable _self;
81
82 public LeftObserver(_ parent, IDisposable self)
83 {
84 _parent = parent;
85 _self = self;
86 }
87
88 private void Expire(int id, IObserver<TRight> group, IDisposable resource)
89 {
90 lock (_parent._gate)
91 if (_parent._leftMap.Remove(id))
92 group.OnCompleted();
93
94 _parent._group.Remove(resource);
95 }
96
97 public void OnNext(TLeft value)
98 {
99 var s = new Subject<TRight>();
100 var id = 0;
101 lock (_parent._gate)
102 {
103 id = _parent._leftID++;
104 _parent._leftMap.Add(id, s);
105 }
106
107 var window = new WindowObservable<TRight>(s, _parent._refCount);
108
109 // BREAKING CHANGE v2 > v1.x - Order of evaluation or the _leftDurationSelector and _resultSelector now consistent with Join.
110 var md = new SingleAssignmentDisposable();
111 _parent._group.Add(md);
112
113 var duration = default(IObservable<TLeftDuration>);
114 try
115 {
116 duration = _parent._parent._leftDurationSelector(value);
117 }
118 catch (Exception exception)
119 {
120 OnError(exception);
121 return;
122 }
123
124 // BREAKING CHANGE v2 > v1.x - The duration sequence is subscribed to before the result sequence is evaluated.
125 md.Disposable = duration.SubscribeSafe(new Delta(this, id, s, md));
126
127 var result = default(TResult);
128 try
129 {
130 result = _parent._parent._resultSelector(value, window);
131 }
132 catch (Exception exception)
133 {
134 OnError(exception);
135 return;
136 }
137
138 lock (_parent._gate)
139 {
140 _parent._observer.OnNext(result);
141
142 foreach (var rightValue in _parent._rightMap.Values)
143 {
144 s.OnNext(rightValue);
145 }
146 }
147 }
148
149 class Delta : IObserver<TLeftDuration>
150 {
151 private readonly LeftObserver _parent;
152 private readonly int _id;
153 private readonly IObserver<TRight> _group;
154 private readonly IDisposable _self;
155
156 public Delta(LeftObserver parent, int id, IObserver<TRight> group, IDisposable self)
157 {
158 _parent = parent;
159 _id = id;
160 _group = group;
161 _self = self;
162 }
163
164 public void OnNext(TLeftDuration value)
165 {
166 _parent.Expire(_id, _group, _self);
167 }
168
169 public void OnError(Exception error)
170 {
171 _parent.OnError(error);
172 }
173
174 public void OnCompleted()
175 {
176 _parent.Expire(_id, _group, _self);
177 }
178 }
179
180 public void OnError(Exception error)
181 {
182 lock (_parent._gate)
183 {
184 foreach (var o in _parent._leftMap.Values)
185 o.OnError(error);
186
187 _parent._observer.OnError(error);
188 _parent.Dispose();
189 }
190 }
191
192 public void OnCompleted()
193 {
194 lock (_parent._gate)
195 {
196 _parent._observer.OnCompleted();
197 _parent.Dispose();
198 }
199
200 _self.Dispose();
201 }
202 }
203
204 class RightObserver : IObserver<TRight>
205 {
206 private readonly _ _parent;
207 private readonly IDisposable _self;
208
209 public RightObserver(_ parent, IDisposable self)
210 {
211 _parent = parent;
212 _self = self;
213 }
214
215 private void Expire(int id, IDisposable resource)
216 {
217 lock (_parent._gate)
218 _parent._rightMap.Remove(id);
219
220 _parent._group.Remove(resource);
221 }
222
223 public void OnNext(TRight value)
224 {
225 var id = 0;
226 lock (_parent._gate)
227 {
228 id = _parent._rightID++;
229 _parent._rightMap.Add(id, value);
230 }
231
232 var md = new SingleAssignmentDisposable();
233 _parent._group.Add(md);
234
235 var duration = default(IObservable<TRightDuration>);
236 try
237 {
238 duration = _parent._parent._rightDurationSelector(value);
239 }
240 catch (Exception exception)
241 {
242 OnError(exception);
243 return;
244 }
245 md.Disposable = duration.SubscribeSafe(new Delta(this, id, md));
246
247 lock (_parent._gate)
248 {
249 foreach (var o in _parent._leftMap.Values)
250 o.OnNext(value);
251 }
252 }
253
254 class Delta : IObserver<TRightDuration>
255 {
256 private readonly RightObserver _parent;
257 private readonly int _id;
258 private readonly IDisposable _self;
259
260 public Delta(RightObserver parent, int id, IDisposable self)
261 {
262 _parent = parent;
263 _id = id;
264 _self = self;
265 }
266
267 public void OnNext(TRightDuration value)
268 {
269 _parent.Expire(_id, _self);
270 }
271
272 public void OnError(Exception error)
273 {
274 _parent.OnError(error);
275 }
276
277 public void OnCompleted()
278 {
279 _parent.Expire(_id, _self);
280 }
281 }
282
283 public void OnError(Exception error)
284 {
285 lock (_parent._gate)
286 {
287 foreach (var o in _parent._leftMap.Values)
288 o.OnError(error);
289 _parent._observer.OnError(error);
290 _parent.Dispose();
291 }
292 }
293
294 public void OnCompleted()
295 {
296 _self.Dispose();
297 }
298 }
299 }
300 }
301 }
302 #endif