Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/QueryLanguage.Single.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Reactive.Concurrency;
6 using System.Reactive.Disposables;
7 using System.Reactive.Subjects;
8
9 namespace System.Reactive.Linq
10 {
11 #if !NO_PERF
12 using ObservableImpl;
13 #endif
14
15 internal partial class QueryLanguage
16 {
17 #region + AsObservable +
18
19 public virtual IObservable<TSource> AsObservable<TSource>(IObservable<TSource> source)
20 {
21 #if !NO_PERF
22 var asObservable = source as AsObservable<TSource>;
23 if (asObservable != null)
24 return asObservable.Omega();
25
26 return new AsObservable<TSource>(source);
27 #else
28 return new AnonymousObservable<TSource>(observer => source.Subscribe(observer));
29 #endif
30 }
31
32 #endregion
33
34 #region + Buffer +
35
36 public virtual IObservable<IList<TSource>> Buffer<TSource>(IObservable<TSource> source, int count)
37 {
38 return Buffer_<TSource>(source, count, count);
39 }
40
41 public virtual IObservable<IList<TSource>> Buffer<TSource>(IObservable<TSource> source, int count, int skip)
42 {
43 return Buffer_<TSource>(source, count, skip);
44 }
45
46 private static IObservable<IList<TSource>> Buffer_<TSource>(IObservable<TSource> source, int count, int skip)
47 {
48 #if !NO_PERF
49 return new Buffer<TSource>(source, count, skip);
50 #else
51 return Window_<TSource>(source, count, skip).SelectMany(Observable.ToList).Where(list => list.Count > 0);
52 #endif
53 }
54
55 #endregion
56
57 #region + Dematerialize +
58
59 public virtual IObservable<TSource> Dematerialize<TSource>(IObservable<Notification<TSource>> source)
60 {
61 #if !NO_PERF
62 var materialize = source as Materialize<TSource>;
63 if (materialize != null)
64 return materialize.Dematerialize();
65
66 return new Dematerialize<TSource>(source);
67 #else
68 return new AnonymousObservable<TSource>(observer =>
69 source.Subscribe(x => x.Accept(observer), observer.OnError, observer.OnCompleted));
70 #endif
71 }
72
73 #endregion
74
75 #region + DistinctUntilChanged +
76
77 public virtual IObservable<TSource> DistinctUntilChanged<TSource>(IObservable<TSource> source)
78 {
79 return DistinctUntilChanged_(source, x => x, EqualityComparer<TSource>.Default);
80 }
81
82 public virtual IObservable<TSource> DistinctUntilChanged<TSource>(IObservable<TSource> source, IEqualityComparer<TSource> comparer)
83 {
84 return DistinctUntilChanged_(source, x => x, comparer);
85 }
86
87 public virtual IObservable<TSource> DistinctUntilChanged<TSource, TKey>(IObservable<TSource> source, Func<TSource, TKey> keySelector)
88 {
89 return DistinctUntilChanged_(source, keySelector, EqualityComparer<TKey>.Default);
90 }
91
92 public virtual IObservable<TSource> DistinctUntilChanged<TSource, TKey>(IObservable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
93 {
94 return DistinctUntilChanged_(source, keySelector, comparer);
95 }
96
97 private static IObservable<TSource> DistinctUntilChanged_<TSource, TKey>(IObservable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
98 {
99 #if !NO_PERF
100 return new DistinctUntilChanged<TSource, TKey>(source, keySelector, comparer);
101 #else
102 return new AnonymousObservable<TSource>(observer =>
103 {
104 var currentKey = default(TKey);
105 var hasCurrentKey = false;
106 return source.Subscribe(
107 value =>
108 {
109 var key = default(TKey);
110 try
111 {
112 key = keySelector(value);
113 }
114 catch (Exception exception)
115 {
116 observer.OnError(exception);
117 return;
118 }
119 var comparerEquals = false;
120 if (hasCurrentKey)
121 {
122 try
123 {
124 comparerEquals = comparer.Equals(currentKey, key);
125 }
126 catch (Exception exception)
127 {
128 observer.OnError(exception);
129 return;
130 }
131 }
132 if (!hasCurrentKey || !comparerEquals)
133 {
134 hasCurrentKey = true;
135 currentKey = key;
136 observer.OnNext(value);
137 }
138 },
139 observer.OnError,
140 observer.OnCompleted);
141 });
142 #endif
143 }
144
145 #endregion
146
147 #region + Do +
148
149 public virtual IObservable<TSource> Do<TSource>(IObservable<TSource> source, Action<TSource> onNext)
150 {
151 #if !NO_PERF
152 return Do_<TSource>(source, onNext, Stubs<Exception>.Ignore, Stubs.Nop);
153 #else
154 // PERFORMANCE - Use of Select allows for operator coalescing
155 return source.Select(
156 x =>
157 {
158 onNext(x);
159 return x;
160 }
161 );
162 #endif
163 }
164
165 public virtual IObservable<TSource> Do<TSource>(IObservable<TSource> source, Action<TSource> onNext, Action onCompleted)
166 {
167 #if !NO_PERF
168 return Do_<TSource>(source, onNext, Stubs<Exception>.Ignore, onCompleted);
169 #else
170 return new AnonymousObservable<TSource>(obs =>
171 {
172 return source.Subscribe(
173 x =>
174 {
175 try
176 {
177 onNext(x);
178 }
179 catch (Exception ex)
180 {
181 obs.OnError(ex);
182 }
183 obs.OnNext(x);
184 },
185 obs.OnError,
186 () =>
187 {
188 try
189 {
190 onCompleted();
191 }
192 catch (Exception ex)
193 {
194 obs.OnError(ex);
195 }
196 obs.OnCompleted();
197 });
198 });
199 #endif
200 }
201
202 public virtual IObservable<TSource> Do<TSource>(IObservable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
203 {
204 #if !NO_PERF
205 return Do_<TSource>(source, onNext, onError, Stubs.Nop);
206 #else
207 return new AnonymousObservable<TSource>(obs =>
208 {
209 return source.Subscribe(
210 x =>
211 {
212 try
213 {
214 onNext(x);
215 }
216 catch (Exception ex)
217 {
218 obs.OnError(ex);
219 }
220 obs.OnNext(x);
221 },
222 ex =>
223 {
224 try
225 {
226 onError(ex);
227 }
228 catch (Exception ex2)
229 {
230 obs.OnError(ex2);
231 }
232 obs.OnError(ex);
233 },
234 obs.OnCompleted);
235 });
236 #endif
237 }
238
239 public virtual IObservable<TSource> Do<TSource>(IObservable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
240 {
241 return Do_(source, onNext, onError, onCompleted);
242 }
243
244 public virtual IObservable<TSource> Do<TSource>(IObservable<TSource> source, IObserver<TSource> observer)
245 {
246 return Do_(source, observer.OnNext, observer.OnError, observer.OnCompleted);
247 }
248
249 private static IObservable<TSource> Do_<TSource>(IObservable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
250 {
251 #if !NO_PERF
252 return new Do<TSource>(source, onNext, onError, onCompleted);
253 #else
254 return new AnonymousObservable<TSource>(obs =>
255 {
256 return source.Subscribe(
257 x =>
258 {
259 try
260 {
261 onNext(x);
262 }
263 catch (Exception ex)
264 {
265 obs.OnError(ex);
266 }
267 obs.OnNext(x);
268 },
269 ex =>
270 {
271 try
272 {
273 onError(ex);
274 }
275 catch (Exception ex2)
276 {
277 obs.OnError(ex2);
278 }
279 obs.OnError(ex);
280 },
281 () =>
282 {
283 try
284 {
285 onCompleted();
286 }
287 catch (Exception ex)
288 {
289 obs.OnError(ex);
290 }
291 obs.OnCompleted();
292 });
293 });
294 #endif
295 }
296
297 #endregion
298
299 #region + Finally +
300
301 public virtual IObservable<TSource> Finally<TSource>(IObservable<TSource> source, Action finallyAction)
302 {
303 #if !NO_PERF
304 return new Finally<TSource>(source, finallyAction);
305 #else
306 return new AnonymousObservable<TSource>(observer =>
307 {
308 var subscription = source.Subscribe(observer);
309
310 return Disposable.Create(() =>
311 {
312 try
313 {
314 subscription.Dispose();
315 }
316 finally
317 {
318 finallyAction();
319 }
320 });
321 });
322 #endif
323 }
324
325 #endregion
326
327 #region + IgnoreElements +
328
329 public virtual IObservable<TSource> IgnoreElements<TSource>(IObservable<TSource> source)
330 {
331 #if !NO_PERF
332 var ignoreElements = source as IgnoreElements<TSource>;
333 if (ignoreElements != null)
334 return ignoreElements.Omega();
335
336 return new IgnoreElements<TSource>(source);
337 #else
338 return new AnonymousObservable<TSource>(observer => source.Subscribe(_ => { }, observer.OnError, observer.OnCompleted));
339 #endif
340 }
341
342 #endregion
343
344 #region + Materialize +
345
346 public virtual IObservable<Notification<TSource>> Materialize<TSource>(IObservable<TSource> source)
347 {
348 #if !NO_PERF
349 //
350 // NOTE: Peephole optimization of xs.Dematerialize().Materialize() should not be performed. It's possible for xs to
351 // contain multiple terminal notifications, which won't survive a Dematerialize().Materialize() chain. In case
352 // a reduction to xs.AsObservable() would be performed, those notification elements would survive.
353 //
354
355 return new Materialize<TSource>(source);
356 #else
357 return new AnonymousObservable<Notification<TSource>>(observer =>
358 source.Subscribe(
359 value => observer.OnNext(Notification.CreateOnNext<TSource>(value)),
360 exception =>
361 {
362 observer.OnNext(Notification.CreateOnError<TSource>(exception));
363 observer.OnCompleted();
364 },
365 () =>
366 {
367 observer.OnNext(Notification.CreateOnCompleted<TSource>());
368 observer.OnCompleted();
369 }));
370 #endif
371 }
372
373 #endregion
374
375 #region - Repeat -
376
377 public virtual IObservable<TSource> Repeat<TSource>(IObservable<TSource> source)
378 {
379 return RepeatInfinite(source).Concat();
380 }
381
382 private static IEnumerable<T> RepeatInfinite<T>(T value)
383 {
384 while (true)
385 yield return value;
386 }
387
388 public virtual IObservable<TSource> Repeat<TSource>(IObservable<TSource> source, int repeatCount)
389 {
390 return Enumerable.Repeat(source, repeatCount).Concat();
391 }
392
393 #endregion
394
395 #region - Retry -
396
397 public virtual IObservable<TSource> Retry<TSource>(IObservable<TSource> source)
398 {
399 return RepeatInfinite(source).Catch();
400 }
401
402 public virtual IObservable<TSource> Retry<TSource>(IObservable<TSource> source, int retryCount)
403 {
404 return Enumerable.Repeat(source, retryCount).Catch();
405 }
406
407 #endregion
408
409 #region + Scan +
410
411 public virtual IObservable<TAccumulate> Scan<TSource, TAccumulate>(IObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
412 {
413 #if !NO_PERF
414 return new Scan<TSource, TAccumulate>(source, seed, accumulator);
415 #else
416 return Defer(() =>
417 {
418 var accumulation = default(TAccumulate);
419 var hasAccumulation = false;
420 return source.Select(x =>
421 {
422 if (hasAccumulation)
423 accumulation = accumulator(accumulation, x);
424 else
425 {
426 accumulation = accumulator(seed, x);
427 hasAccumulation = true;
428 }
429 return accumulation;
430 });
431 });
432 #endif
433 }
434
435 public virtual IObservable<TSource> Scan<TSource>(IObservable<TSource> source, Func<TSource, TSource, TSource> accumulator)
436 {
437 #if !NO_PERF
438 return new Scan<TSource>(source, accumulator);
439 #else
440 return Defer(() =>
441 {
442 var accumulation = default(TSource);
443 var hasAccumulation = false;
444 return source.Select(x =>
445 {
446 if (hasAccumulation)
447 accumulation = accumulator(accumulation, x);
448 else
449 {
450 accumulation = x;
451 hasAccumulation = true;
452 }
453 return accumulation;
454 });
455 });
456 #endif
457 }
458
459 #endregion
460
461 #region + SkipLast +
462
463 public virtual IObservable<TSource> SkipLast<TSource>(IObservable<TSource> source, int count)
464 {
465 #if !NO_PERF
466 return new SkipLast<TSource>(source, count);
467 #else
468 return new AnonymousObservable<TSource>(observer =>
469 {
470 var q = new Queue<TSource>();
471
472 return source.Subscribe(
473 x =>
474 {
475 q.Enqueue(x);
476 if (q.Count > count)
477 observer.OnNext(q.Dequeue());
478 },
479 observer.OnError,
480 observer.OnCompleted);
481 });
482 #endif
483 }
484
485 #endregion
486
487 #region - StartWith -
488
489 public virtual IObservable<TSource> StartWith<TSource>(IObservable<TSource> source, params TSource[] values)
490 {
491 return StartWith_<TSource>(source, SchedulerDefaults.ConstantTimeOperations, values);
492 }
493
494 public virtual IObservable<TSource> StartWith<TSource>(IObservable<TSource> source, IScheduler scheduler, params TSource[] values)
495 {
496 return StartWith_<TSource>(source, scheduler, values);
497 }
498
499 public virtual IObservable<TSource> StartWith<TSource>(IObservable<TSource> source, IEnumerable<TSource> values)
500 {
501 return StartWith(source, SchedulerDefaults.ConstantTimeOperations, values);
502 }
503
504 public virtual IObservable<TSource> StartWith<TSource>(IObservable<TSource> source, IScheduler scheduler, IEnumerable<TSource> values)
505 {
506 if (values == null)
507 throw new ArgumentNullException("values");
508
509 var valueArray = values as TSource[];
510 if (valueArray == null)
511 {
512 List<TSource> valueList = new List<TSource>(values);
513 valueArray = valueList.ToArray();
514 }
515 return StartWith_<TSource>(source, scheduler, valueArray);
516 }
517
518 private static IObservable<TSource> StartWith_<TSource>(IObservable<TSource> source, IScheduler scheduler, params TSource[] values)
519 {
520 return values.ToObservable(scheduler).Concat(source);
521 }
522
523 #endregion
524
525 #region + TakeLast +
526
527 public virtual IObservable<TSource> TakeLast<TSource>(IObservable<TSource> source, int count)
528 {
529 return TakeLast_(source, count, SchedulerDefaults.Iteration);
530 }
531
532 public virtual IObservable<TSource> TakeLast<TSource>(IObservable<TSource> source, int count, IScheduler scheduler)
533 {
534 return TakeLast_(source, count, scheduler);
535 }
536
537 private static IObservable<TSource> TakeLast_<TSource>(IObservable<TSource> source, int count, IScheduler scheduler)
538 {
539 #if !NO_PERF
540 return new TakeLast<TSource>(source, count, scheduler);
541 #else
542 return new AnonymousObservable<TSource>(observer =>
543 {
544 var q = new Queue<TSource>();
545
546 var g = new CompositeDisposable();
547
548 g.Add(source.Subscribe(
549 x =>
550 {
551 q.Enqueue(x);
552 if (q.Count > count)
553 q.Dequeue();
554 },
555 observer.OnError,
556 () =>
557 {
558 g.Add(scheduler.Schedule(rec =>
559 {
560 if (q.Count > 0)
561 {
562 observer.OnNext(q.Dequeue());
563 rec();
564 }
565 else
566 {
567 observer.OnCompleted();
568 }
569 }));
570 }
571 ));
572
573 return g;
574 });
575 #endif
576 }
577
578 public virtual IObservable<IList<TSource>> TakeLastBuffer<TSource>(IObservable<TSource> source, int count)
579 {
580 #if !NO_PERF
581 return new TakeLastBuffer<TSource>(source, count);
582 #else
583 return new AnonymousObservable<IList<TSource>>(observer =>
584 {
585 var q = new Queue<TSource>();
586
587 return source.Subscribe(
588 x =>
589 {
590 q.Enqueue(x);
591 if (q.Count > count)
592 q.Dequeue();
593 },
594 observer.OnError,
595 () =>
596 {
597 observer.OnNext(q.ToList());
598 observer.OnCompleted();
599 });
600 });
601 #endif
602 }
603
604 #endregion
605
606 #region + Window +
607
608 public virtual IObservable<IObservable<TSource>> Window<TSource>(IObservable<TSource> source, int count, int skip)
609 {
610 return Window_<TSource>(source, count, skip);
611 }
612
613 public virtual IObservable<IObservable<TSource>> Window<TSource>(IObservable<TSource> source, int count)
614 {
615 return Window_<TSource>(source, count, count);
616 }
617
618 private static IObservable<IObservable<TSource>> Window_<TSource>(IObservable<TSource> source, int count, int skip)
619 {
620 #if !NO_PERF
621 return new Window<TSource>(source, count, skip);
622 #else
623 return new AnonymousObservable<IObservable<TSource>>(observer =>
624 {
625 var q = new Queue<ISubject<TSource>>();
626 var n = 0;
627
628 var m = new SingleAssignmentDisposable();
629 var refCountDisposable = new RefCountDisposable(m);
630
631 Action createWindow = () =>
632 {
633 var s = new Subject<TSource>();
634 q.Enqueue(s);
635 observer.OnNext(s.AddRef(refCountDisposable));
636 };
637
638 createWindow();
639
640 m.Disposable = source.Subscribe(
641 x =>
642 {
643 foreach (var s in q)
644 s.OnNext(x);
645
646 var c = n - count + 1;
647 if (c >= 0 && c % skip == 0)
648 {
649 var s = q.Dequeue();
650 s.OnCompleted();
651 }
652
653 n++;
654 if (n % skip == 0)
655 createWindow();
656 },
657 exception =>
658 {
659 while (q.Count > 0)
660 q.Dequeue().OnError(exception);
661
662 observer.OnError(exception);
663 },
664 () =>
665 {
666 while (q.Count > 0)
667 q.Dequeue().OnCompleted();
668
669 observer.OnCompleted();
670 }
671 );
672
673 return refCountDisposable;
674 });
675 #endif
676 }
677
678 #endregion
679 }
680 }