Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/SkipUntil.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.Reactive.Concurrency;
6 using System.Reactive.Disposables;
7 using System.Threading;
8
9 namespace System.Reactive.Linq.ObservableImpl
10 {
11 class SkipUntil<TSource, TOther> : Producer<TSource>
12 {
13 private readonly IObservable<TSource> _source;
14 private readonly IObservable<TOther> _other;
15
16 public SkipUntil(IObservable<TSource> source, IObservable<TOther> other)
17 {
18 _source = source;
19 _other = other;
20 }
21
22 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
23 {
24 var sink = new _(this, observer, cancel);
25 setSink(sink);
26 return sink.Run();
27 }
28
29 class _ : Sink<TSource>
30 {
31 private readonly SkipUntil<TSource, TOther> _parent;
32
33 public _(SkipUntil<TSource, TOther> parent, IObserver<TSource> observer, IDisposable cancel)
34 : base(observer, cancel)
35 {
36 _parent = parent;
37 }
38
39 public IDisposable Run()
40 {
41 var sourceObserver = new T(this);
42 var otherObserver = new O(this, sourceObserver);
43
44 var sourceSubscription = _parent._source.SubscribeSafe(sourceObserver);
45 var otherSubscription = _parent._other.SubscribeSafe(otherObserver);
46
47 sourceObserver.Disposable = sourceSubscription;
48 otherObserver.Disposable = otherSubscription;
49
50 return new CompositeDisposable(
51 sourceSubscription,
52 otherSubscription
53 );
54 }
55
56 class T : IObserver<TSource>
57 {
58 private readonly _ _parent;
59 public volatile IObserver<TSource> _observer;
60 private readonly SingleAssignmentDisposable _subscription;
61
62 public T(_ parent)
63 {
64 _parent = parent;
65 _observer = NopObserver<TSource>.Instance;
66 _subscription = new SingleAssignmentDisposable();
67 }
68
69 public IDisposable Disposable
70 {
71 set { _subscription.Disposable = value; }
72 }
73
74 public void OnNext(TSource value)
75 {
76 _observer.OnNext(value);
77 }
78
79 public void OnError(Exception error)
80 {
81 _parent._observer.OnError(error);
82 _parent.Dispose();
83 }
84
85 public void OnCompleted()
86 {
87 _observer.OnCompleted();
88 _subscription.Dispose(); // We can't cancel the other stream yet, it may be on its way to dispatch an OnError message and we don't want to have a race.
89 }
90 }
91
92 class O : IObserver<TOther>
93 {
94 private readonly _ _parent;
95 private readonly T _sourceObserver;
96 private readonly SingleAssignmentDisposable _subscription;
97
98 public O(_ parent, T sourceObserver)
99 {
100 _parent = parent;
101 _sourceObserver = sourceObserver;
102 _subscription = new SingleAssignmentDisposable();
103 }
104
105 public IDisposable Disposable
106 {
107 set { _subscription.Disposable = value; }
108 }
109
110 public void OnNext(TOther value)
111 {
112 _sourceObserver._observer = _parent._observer;
113 _subscription.Dispose();
114 }
115
116 public void OnError(Exception error)
117 {
118 _parent._observer.OnError(error);
119 _parent.Dispose();
120 }
121
122 public void OnCompleted()
123 {
124 _subscription.Dispose();
125 }
126 }
127 }
128 }
129
130 class SkipUntil<TSource> : Producer<TSource>
131 {
132 private readonly IObservable<TSource> _source;
133 private readonly DateTimeOffset _startTime;
134 internal readonly IScheduler _scheduler;
135
136 public SkipUntil(IObservable<TSource> source, DateTimeOffset startTime, IScheduler scheduler)
137 {
138 _source = source;
139 _startTime = startTime;
140 _scheduler = scheduler;
141 }
142
143 public IObservable<TSource> Omega(DateTimeOffset startTime)
144 {
145 //
146 // Maximum semantics:
147 //
148 // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
149 //
150 // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
151 // xs.SU(5AM) xxxxxxxxxxxxxxxx-o--| xs.SU(3AM) xxxxxxxxxx-o--o--o--|
152 // xs.SU(5AM).SU(3AM) xxxxxxxxx--------o--| xs.SU(3AM).SU(5AM) xxxxxxxxxxxxxxxx-o--|
153 //
154 if (startTime <= _startTime)
155 return this;
156 else
157 return new SkipUntil<TSource>(_source, startTime, _scheduler);
158 }
159
160 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
161 {
162 var sink = new _(this, observer, cancel);
163 setSink(sink);
164 return sink.Run();
165 }
166
167 class _ : Sink<TSource>, IObserver<TSource>
168 {
169 private readonly SkipUntil<TSource> _parent;
170 private volatile bool _open;
171
172 public _(SkipUntil<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
173 : base(observer, cancel)
174 {
175 _parent = parent;
176 }
177
178 public IDisposable Run()
179 {
180 var t = _parent._scheduler.Schedule(_parent._startTime, Tick);
181 var d = _parent._source.SubscribeSafe(this);
182 return new CompositeDisposable(t, d);
183 }
184
185 private void Tick()
186 {
187 _open = true;
188 }
189
190 public void OnNext(TSource value)
191 {
192 if (_open)
193 base._observer.OnNext(value);
194 }
195
196 public void OnError(Exception error)
197 {
198 base._observer.OnError(error);
199 base.Dispose();
200 }
201
202 public void OnCompleted()
203 {
204 base._observer.OnCompleted();
205 base.Dispose();
206 }
207 }
208 }
209 }
210 #endif