-
Notifications
You must be signed in to change notification settings - Fork 735
/
Join.cs
319 lines (271 loc) · 11.6 KB
/
Join.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Reactive.Disposables;
namespace System.Reactive.Linq.ObservableImpl
{
internal sealed class Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> : Producer<TResult, Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult>._>
{
private readonly IObservable<TLeft> _left;
private readonly IObservable<TRight> _right;
private readonly Func<TLeft, IObservable<TLeftDuration>> _leftDurationSelector;
private readonly Func<TRight, IObservable<TRightDuration>> _rightDurationSelector;
private readonly Func<TLeft, TRight, TResult> _resultSelector;
public Join(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, IObservable<TLeftDuration>> leftDurationSelector, Func<TRight, IObservable<TRightDuration>> rightDurationSelector, Func<TLeft, TRight, TResult> resultSelector)
{
_left = left;
_right = right;
_leftDurationSelector = leftDurationSelector;
_rightDurationSelector = rightDurationSelector;
_resultSelector = resultSelector;
}
protected override _ CreateSink(IObserver<TResult> observer) => new _(this, observer);
protected override void Run(_ sink) => sink.Run(this);
internal sealed class _ : IdentitySink<TResult>
{
private readonly object _gate = new object();
private readonly CompositeDisposable _group = new CompositeDisposable();
private readonly SortedDictionary<int, TLeft> _leftMap = new SortedDictionary<int, TLeft>();
private readonly SortedDictionary<int, TRight> _rightMap = new SortedDictionary<int, TRight>();
private readonly Func<TLeft, IObservable<TLeftDuration>> _leftDurationSelector;
private readonly Func<TRight, IObservable<TRightDuration>> _rightDurationSelector;
private readonly Func<TLeft, TRight, TResult> _resultSelector;
public _(Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> parent, IObserver<TResult> observer)
: base(observer)
{
_leftDurationSelector = parent._leftDurationSelector;
_rightDurationSelector = parent._rightDurationSelector;
_resultSelector = parent._resultSelector;
}
private bool _leftDone;
private int _leftID;
private bool _rightDone;
private int _rightID;
public void Run(Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> parent)
{
var leftObserver = new LeftObserver(this);
var rightObserver = new RightObserver(this);
_group.Add(leftObserver);
_group.Add(rightObserver);
leftObserver.SetResource(parent._left.SubscribeSafe(leftObserver));
rightObserver.SetResource(parent._right.SubscribeSafe(rightObserver));
SetUpstream(_group);
}
private sealed class LeftObserver : SafeObserver<TLeft>
{
private readonly _ _parent;
public LeftObserver(_ parent)
{
_parent = parent;
}
private void Expire(int id, IDisposable resource)
{
lock (_parent._gate)
{
if (_parent._leftMap.Remove(id) && _parent._leftMap.Count == 0 && _parent._leftDone)
{
_parent.ForwardOnCompleted();
}
}
_parent._group.Remove(resource);
}
public override void OnNext(TLeft value)
{
var id = 0;
var rightID = 0;
lock (_parent._gate)
{
id = _parent._leftID++;
rightID = _parent._rightID;
_parent._leftMap.Add(id, value);
}
var duration = default(IObservable<TLeftDuration>);
try
{
duration = _parent._leftDurationSelector(value);
}
catch (Exception exception)
{
_parent.ForwardOnError(exception);
return;
}
var durationObserver = new DurationObserver(this, id);
_parent._group.Add(durationObserver);
durationObserver.SetResource(duration.SubscribeSafe(durationObserver));
lock (_parent._gate)
{
foreach (var rightValue in _parent._rightMap)
{
if (rightValue.Key < rightID)
{
TResult result;
try
{
result = _parent._resultSelector(value, rightValue.Value);
}
catch (Exception exception)
{
_parent.ForwardOnError(exception);
return;
}
_parent.ForwardOnNext(result);
}
}
}
}
private sealed class DurationObserver : SafeObserver<TLeftDuration>
{
private readonly LeftObserver _parent;
private readonly int _id;
public DurationObserver(LeftObserver parent, int id)
{
_parent = parent;
_id = id;
}
public override void OnNext(TLeftDuration value)
{
_parent.Expire(_id, this);
}
public override void OnError(Exception error)
{
_parent.OnError(error);
}
public override void OnCompleted()
{
_parent.Expire(_id, this);
}
}
public override void OnError(Exception error)
{
lock (_parent._gate)
{
_parent.ForwardOnError(error);
}
}
public override void OnCompleted()
{
lock (_parent._gate)
{
_parent._leftDone = true;
if (_parent._rightDone || _parent._leftMap.Count == 0)
{
_parent.ForwardOnCompleted();
}
else
{
Dispose();
}
}
}
}
private sealed class RightObserver : SafeObserver<TRight>
{
private readonly _ _parent;
public RightObserver(_ parent)
{
_parent = parent;
}
private void Expire(int id, IDisposable resource)
{
lock (_parent._gate)
{
if (_parent._rightMap.Remove(id) && _parent._rightMap.Count == 0 && _parent._rightDone)
{
_parent.ForwardOnCompleted();
}
}
_parent._group.Remove(resource);
}
public override void OnNext(TRight value)
{
var id = 0;
var leftID = 0;
lock (_parent._gate)
{
id = _parent._rightID++;
leftID = _parent._leftID;
_parent._rightMap.Add(id, value);
}
var duration = default(IObservable<TRightDuration>);
try
{
duration = _parent._rightDurationSelector(value);
}
catch (Exception exception)
{
_parent.ForwardOnError(exception);
return;
}
var durationObserver = new DurationObserver(this, id);
_parent._group.Add(durationObserver);
durationObserver.SetResource(duration.SubscribeSafe(durationObserver));
lock (_parent._gate)
{
foreach (var leftValue in _parent._leftMap)
{
if (leftValue.Key < leftID)
{
TResult result;
try
{
result = _parent._resultSelector(leftValue.Value, value);
}
catch (Exception exception)
{
_parent.ForwardOnError(exception);
return;
}
_parent.ForwardOnNext(result);
}
}
}
}
private sealed class DurationObserver : SafeObserver<TRightDuration>
{
private readonly RightObserver _parent;
private readonly int _id;
public DurationObserver(RightObserver parent, int id)
{
_parent = parent;
_id = id;
}
public override void OnNext(TRightDuration value)
{
_parent.Expire(_id, this);
}
public override void OnError(Exception error)
{
_parent.OnError(error);
}
public override void OnCompleted()
{
_parent.Expire(_id, this);
}
}
public override void OnError(Exception error)
{
lock (_parent._gate)
{
_parent.ForwardOnError(error);
}
}
public override void OnCompleted()
{
lock (_parent._gate)
{
_parent._rightDone = true;
if (_parent._leftDone || _parent._rightMap.Count == 0)
{
_parent.ForwardOnCompleted();
}
else
{
Dispose();
}
}
}
}
}
}
}