| 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 |
|
| 8 |
namespace System.Reactive.Linq.ObservableImpl |
| 9 |
{ |
| 10 |
class Throttle<TSource> : Producer<TSource> |
| 11 |
{ |
| 12 |
private readonly IObservable<TSource> _source; |
| 13 |
private readonly TimeSpan _dueTime; |
| 14 |
private readonly IScheduler _scheduler; |
| 15 |
|
| 16 |
public Throttle(IObservable<TSource> source, TimeSpan dueTime, IScheduler scheduler) |
| 17 |
{ |
| 18 |
_source = source; |
| 19 |
_dueTime = dueTime; |
| 20 |
_scheduler = scheduler; |
| 21 |
} |
| 22 |
|
| 23 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 24 |
{ |
| 25 |
var sink = new _(this, observer, cancel); |
| 26 |
setSink(sink); |
| 27 |
return sink.Run(); |
| 28 |
} |
| 29 |
|
| 30 |
class _ : Sink<TSource>, IObserver<TSource> |
| 31 |
{ |
| 32 |
private readonly Throttle<TSource> _parent; |
| 33 |
|
| 34 |
public _(Throttle<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 35 |
: base(observer, cancel) |
| 36 |
{ |
| 37 |
_parent = parent; |
| 38 |
} |
| 39 |
|
| 40 |
private object _gate; |
| 41 |
private TSource _value; |
| 42 |
private bool _hasValue; |
| 43 |
private SerialDisposable _cancelable; |
| 44 |
private ulong _id; |
| 45 |
|
| 46 |
public IDisposable Run() |
| 47 |
{ |
| 48 |
_gate = new object(); |
| 49 |
_value = default(TSource); |
| 50 |
_hasValue = false; |
| 51 |
_cancelable = new SerialDisposable(); |
| 52 |
_id = 0UL; |
| 53 |
|
| 54 |
var subscription = _parent._source.SubscribeSafe(this); |
| 55 |
|
| 56 |
return new CompositeDisposable(subscription, _cancelable); |
| 57 |
} |
| 58 |
|
| 59 |
public void OnNext(TSource value) |
| 60 |
{ |
| 61 |
var currentid = default(ulong); |
| 62 |
lock (_gate) |
| 63 |
{ |
| 64 |
_hasValue = true; |
| 65 |
_value = value; |
| 66 |
_id = unchecked(_id + 1); |
| 67 |
currentid = _id; |
| 68 |
} |
| 69 |
var d = new SingleAssignmentDisposable(); |
| 70 |
_cancelable.Disposable = d; |
| 71 |
d.Disposable = _parent._scheduler.Schedule(currentid, _parent._dueTime, Propagate); |
| 72 |
} |
| 73 |
|
| 74 |
private IDisposable Propagate(IScheduler self, ulong currentid) |
| 75 |
{ |
| 76 |
lock (_gate) |
| 77 |
{ |
| 78 |
if (_hasValue && _id == currentid) |
| 79 |
base._observer.OnNext(_value); |
| 80 |
_hasValue = false; |
| 81 |
} |
| 82 |
|
| 83 |
return Disposable.Empty; |
| 84 |
} |
| 85 |
|
| 86 |
public void OnError(Exception error) |
| 87 |
{ |
| 88 |
_cancelable.Dispose(); |
| 89 |
|
| 90 |
lock (_gate) |
| 91 |
{ |
| 92 |
base._observer.OnError(error); |
| 93 |
base.Dispose(); |
| 94 |
|
| 95 |
_hasValue = false; |
| 96 |
_id = unchecked(_id + 1); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public void OnCompleted() |
| 101 |
{ |
| 102 |
_cancelable.Dispose(); |
| 103 |
|
| 104 |
lock (_gate) |
| 105 |
{ |
| 106 |
if (_hasValue) |
| 107 |
base._observer.OnNext(_value); |
| 108 |
|
| 109 |
base._observer.OnCompleted(); |
| 110 |
base.Dispose(); |
| 111 |
|
| 112 |
_hasValue = false; |
| 113 |
_id = unchecked(_id + 1); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
class Throttle<TSource, TThrottle> : Producer<TSource> |
| 120 |
{ |
| 121 |
private readonly IObservable<TSource> _source; |
| 122 |
private readonly Func<TSource, IObservable<TThrottle>> _throttleSelector; |
| 123 |
|
| 124 |
public Throttle(IObservable<TSource> source, Func<TSource, IObservable<TThrottle>> throttleSelector) |
| 125 |
{ |
| 126 |
_source = source; |
| 127 |
_throttleSelector = throttleSelector; |
| 128 |
} |
| 129 |
|
| 130 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 131 |
{ |
| 132 |
var sink = new _(this, observer, cancel); |
| 133 |
setSink(sink); |
| 134 |
return sink.Run(); |
| 135 |
} |
| 136 |
|
| 137 |
class _ : Sink<TSource>, IObserver<TSource> |
| 138 |
{ |
| 139 |
private readonly Throttle<TSource, TThrottle> _parent; |
| 140 |
|
| 141 |
public _(Throttle<TSource, TThrottle> parent, IObserver<TSource> observer, IDisposable cancel) |
| 142 |
: base(observer, cancel) |
| 143 |
{ |
| 144 |
_parent = parent; |
| 145 |
} |
| 146 |
|
| 147 |
private object _gate; |
| 148 |
private TSource _value; |
| 149 |
private bool _hasValue; |
| 150 |
private SerialDisposable _cancelable; |
| 151 |
private ulong _id; |
| 152 |
|
| 153 |
public IDisposable Run() |
| 154 |
{ |
| 155 |
_gate = new object(); |
| 156 |
_value = default(TSource); |
| 157 |
_hasValue = false; |
| 158 |
_cancelable = new SerialDisposable(); |
| 159 |
_id = 0UL; |
| 160 |
|
| 161 |
var subscription = _parent._source.SubscribeSafe(this); |
| 162 |
|
| 163 |
return new CompositeDisposable(subscription, _cancelable); |
| 164 |
} |
| 165 |
|
| 166 |
public void OnNext(TSource value) |
| 167 |
{ |
| 168 |
var throttle = default(IObservable<TThrottle>); |
| 169 |
try |
| 170 |
{ |
| 171 |
throttle = _parent._throttleSelector(value); |
| 172 |
} |
| 173 |
catch (Exception error) |
| 174 |
{ |
| 175 |
lock (_gate) |
| 176 |
{ |
| 177 |
base._observer.OnError(error); |
| 178 |
base.Dispose(); |
| 179 |
} |
| 180 |
|
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
ulong currentid; |
| 185 |
lock (_gate) |
| 186 |
{ |
| 187 |
_hasValue = true; |
| 188 |
_value = value; |
| 189 |
_id = unchecked(_id + 1); |
| 190 |
currentid = _id; |
| 191 |
} |
| 192 |
|
| 193 |
var d = new SingleAssignmentDisposable(); |
| 194 |
_cancelable.Disposable = d; |
| 195 |
d.Disposable = throttle.SubscribeSafe(new Delta(this, value, currentid, d)); |
| 196 |
} |
| 197 |
|
| 198 |
public void OnError(Exception error) |
| 199 |
{ |
| 200 |
_cancelable.Dispose(); |
| 201 |
|
| 202 |
lock (_gate) |
| 203 |
{ |
| 204 |
base._observer.OnError(error); |
| 205 |
base.Dispose(); |
| 206 |
|
| 207 |
_hasValue = false; |
| 208 |
_id = unchecked(_id + 1); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
public void OnCompleted() |
| 213 |
{ |
| 214 |
_cancelable.Dispose(); |
| 215 |
|
| 216 |
lock (_gate) |
| 217 |
{ |
| 218 |
if (_hasValue) |
| 219 |
base._observer.OnNext(_value); |
| 220 |
|
| 221 |
base._observer.OnCompleted(); |
| 222 |
base.Dispose(); |
| 223 |
|
| 224 |
_hasValue = false; |
| 225 |
_id = unchecked(_id + 1); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
class Delta : IObserver<TThrottle> |
| 230 |
{ |
| 231 |
private readonly _ _parent; |
| 232 |
private readonly TSource _value; |
| 233 |
private readonly ulong _currentid; |
| 234 |
private readonly IDisposable _self; |
| 235 |
|
| 236 |
public Delta(_ parent, TSource value, ulong currentid, IDisposable self) |
| 237 |
{ |
| 238 |
_parent = parent; |
| 239 |
_value = value; |
| 240 |
_currentid = currentid; |
| 241 |
_self = self; |
| 242 |
} |
| 243 |
|
| 244 |
public void OnNext(TThrottle value) |
| 245 |
{ |
| 246 |
lock (_parent._gate) |
| 247 |
{ |
| 248 |
if (_parent._hasValue && _parent._id == _currentid) |
| 249 |
_parent._observer.OnNext(_value); |
| 250 |
|
| 251 |
_parent._hasValue = false; |
| 252 |
_self.Dispose(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
public void OnError(Exception error) |
| 257 |
{ |
| 258 |
lock (_parent._gate) |
| 259 |
{ |
| 260 |
_parent._observer.OnError(error); |
| 261 |
_parent.Dispose(); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
public void OnCompleted() |
| 266 |
{ |
| 267 |
lock (_parent._gate) |
| 268 |
{ |
| 269 |
if (_parent._hasValue && _parent._id == _currentid) |
| 270 |
_parent._observer.OnNext(_value); |
| 271 |
|
| 272 |
_parent._hasValue = false; |
| 273 |
_self.Dispose(); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
#endif |