Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/QueryLanguage.Joins.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.Reactive.Disposables;
5 using System.Reactive.Joins;
6
7 namespace System.Reactive.Linq
8 {
9 internal partial class QueryLanguage
10 {
11 #region And
12
13 public virtual Pattern<TLeft, TRight> And<TLeft, TRight>(IObservable<TLeft> left, IObservable<TRight> right)
14 {
15 return new Pattern<TLeft, TRight>(left, right);
16 }
17
18 #endregion
19
20 #region Then
21
22 public virtual Plan<TResult> Then<TSource, TResult>(IObservable<TSource> source, Func<TSource, TResult> selector)
23 {
24 return new Pattern<TSource>(source).Then(selector);
25 }
26
27 #endregion
28
29 #region When
30
31 public virtual IObservable<TResult> When<TResult>(params Plan<TResult>[] plans)
32 {
33 return When((IEnumerable<Plan<TResult>>)plans);
34 }
35
36 public virtual IObservable<TResult> When<TResult>(IEnumerable<Plan<TResult>> plans)
37 {
38 return new AnonymousObservable<TResult>(observer =>
39 {
40 var externalSubscriptions = new Dictionary<object, IJoinObserver>();
41 var gate = new object();
42 var activePlans = new List<ActivePlan>();
43 var outObserver = Observer.Create<TResult>(observer.OnNext,
44 exception =>
45 {
46 foreach (var po in externalSubscriptions.Values)
47 {
48 po.Dispose();
49 }
50 observer.OnError(exception);
51 },
52 observer.OnCompleted);
53 try
54 {
55 foreach (var plan in plans)
56 activePlans.Add(plan.Activate(externalSubscriptions, outObserver,
57 activePlan =>
58 {
59 activePlans.Remove(activePlan);
60 if (activePlans.Count == 0)
61 outObserver.OnCompleted();
62 }));
63 }
64 catch (Exception e)
65 {
66 //
67 // [OK] Use of unsafe Subscribe: we're calling into a known producer implementation.
68 //
69 return Throw<TResult>(e).Subscribe/*Unsafe*/(observer);
70 }
71
72 var group = new CompositeDisposable(externalSubscriptions.Values.Count);
73 foreach (var joinObserver in externalSubscriptions.Values)
74 {
75 joinObserver.Subscribe(gate);
76 group.Add(joinObserver);
77 }
78 return group;
79 });
80 }
81
82 #endregion
83 }
84 }