| 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.Collections.Generic; |
| 6 |
using System.Reactive.Concurrency; |
| 7 |
using System.Reactive.Disposables; |
| 8 |
|
| 9 |
namespace System.Reactive.Linq.ObservableImpl |
| 10 |
{ |
| 11 |
class TakeLast<TSource> : Producer<TSource> |
| 12 |
{ |
| 13 |
private readonly IObservable<TSource> _source; |
| 14 |
private readonly int _count; |
| 15 |
private readonly TimeSpan _duration; |
| 16 |
private readonly IScheduler _scheduler; |
| 17 |
private readonly IScheduler _loopScheduler; |
| 18 |
|
| 19 |
public TakeLast(IObservable<TSource> source, int count, IScheduler loopScheduler) |
| 20 |
{ |
| 21 |
_source = source; |
| 22 |
_count = count; |
| 23 |
_loopScheduler = loopScheduler; |
| 24 |
} |
| 25 |
|
| 26 |
public TakeLast(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler, IScheduler loopScheduler) |
| 27 |
{ |
| 28 |
_source = source; |
| 29 |
_duration = duration; |
| 30 |
_scheduler = scheduler; |
| 31 |
_loopScheduler = loopScheduler; |
| 32 |
} |
| 33 |
|
| 34 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 35 |
{ |
| 36 |
if (_scheduler == null) |
| 37 |
{ |
| 38 |
var sink = new _(this, observer, cancel); |
| 39 |
setSink(sink); |
| 40 |
return sink.Run(); |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
var sink = new TakeLastImpl(this, observer, cancel); |
| 45 |
setSink(sink); |
| 46 |
return sink.Run(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
class _ : Sink<TSource>, IObserver<TSource> |
| 51 |
{ |
| 52 |
private readonly TakeLast<TSource> _parent; |
| 53 |
private Queue<TSource> _queue; |
| 54 |
|
| 55 |
public _(TakeLast<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 56 |
: base(observer, cancel) |
| 57 |
{ |
| 58 |
_parent = parent; |
| 59 |
_queue = new Queue<TSource>(); |
| 60 |
} |
| 61 |
|
| 62 |
private SingleAssignmentDisposable _subscription; |
| 63 |
private SingleAssignmentDisposable _loop; |
| 64 |
|
| 65 |
public IDisposable Run() |
| 66 |
{ |
| 67 |
_subscription = new SingleAssignmentDisposable(); |
| 68 |
_loop = new SingleAssignmentDisposable(); |
| 69 |
|
| 70 |
_subscription.Disposable = _parent._source.SubscribeSafe(this); |
| 71 |
|
| 72 |
return new CompositeDisposable(_subscription, _loop); |
| 73 |
} |
| 74 |
|
| 75 |
public void OnNext(TSource value) |
| 76 |
{ |
| 77 |
_queue.Enqueue(value); |
| 78 |
if (_queue.Count > _parent._count) |
| 79 |
_queue.Dequeue(); |
| 80 |
} |
| 81 |
|
| 82 |
public void OnError(Exception error) |
| 83 |
{ |
| 84 |
base._observer.OnError(error); |
| 85 |
base.Dispose(); |
| 86 |
} |
| 87 |
|
| 88 |
public void OnCompleted() |
| 89 |
{ |
| 90 |
_subscription.Dispose(); |
| 91 |
|
| 92 |
var longRunning = _parent._loopScheduler.AsLongRunning(); |
| 93 |
if (longRunning != null) |
| 94 |
_loop.Disposable = longRunning.ScheduleLongRunning(Loop); |
| 95 |
else |
| 96 |
_loop.Disposable = _parent._loopScheduler.Schedule(LoopRec); |
| 97 |
} |
| 98 |
|
| 99 |
private void LoopRec(Action recurse) |
| 100 |
{ |
| 101 |
if (_queue.Count > 0) |
| 102 |
{ |
| 103 |
base._observer.OnNext(_queue.Dequeue()); |
| 104 |
recurse(); |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
base._observer.OnCompleted(); |
| 109 |
base.Dispose(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
private void Loop(ICancelable cancel) |
| 114 |
{ |
| 115 |
var n = _queue.Count; |
| 116 |
|
| 117 |
while (!cancel.IsDisposed) |
| 118 |
{ |
| 119 |
if (n == 0) |
| 120 |
{ |
| 121 |
base._observer.OnCompleted(); |
| 122 |
break; |
| 123 |
} |
| 124 |
else |
| 125 |
base._observer.OnNext(_queue.Dequeue()); |
| 126 |
|
| 127 |
n--; |
| 128 |
} |
| 129 |
|
| 130 |
base.Dispose(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
class TakeLastImpl : Sink<TSource>, IObserver<TSource> |
| 135 |
{ |
| 136 |
private readonly TakeLast<TSource> _parent; |
| 137 |
private Queue<System.Reactive.TimeInterval<TSource>> _queue; |
| 138 |
|
| 139 |
public TakeLastImpl(TakeLast<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 140 |
: base(observer, cancel) |
| 141 |
{ |
| 142 |
_parent = parent; |
| 143 |
_queue = new Queue<System.Reactive.TimeInterval<TSource>>(); |
| 144 |
} |
| 145 |
|
| 146 |
private SingleAssignmentDisposable _subscription; |
| 147 |
private SingleAssignmentDisposable _loop; |
| 148 |
private IStopwatch _watch; |
| 149 |
|
| 150 |
public IDisposable Run() |
| 151 |
{ |
| 152 |
_subscription = new SingleAssignmentDisposable(); |
| 153 |
_loop = new SingleAssignmentDisposable(); |
| 154 |
|
| 155 |
_watch = _parent._scheduler.StartStopwatch(); |
| 156 |
_subscription.Disposable = _parent._source.SubscribeSafe(this); |
| 157 |
|
| 158 |
return new CompositeDisposable(_subscription, _loop); |
| 159 |
} |
| 160 |
|
| 161 |
public void OnNext(TSource value) |
| 162 |
{ |
| 163 |
var now = _watch.Elapsed; |
| 164 |
_queue.Enqueue(new System.Reactive.TimeInterval<TSource>(value, now)); |
| 165 |
Trim(now); |
| 166 |
} |
| 167 |
|
| 168 |
public void OnError(Exception error) |
| 169 |
{ |
| 170 |
base._observer.OnError(error); |
| 171 |
base.Dispose(); |
| 172 |
} |
| 173 |
|
| 174 |
public void OnCompleted() |
| 175 |
{ |
| 176 |
_subscription.Dispose(); |
| 177 |
|
| 178 |
var now = _watch.Elapsed; |
| 179 |
Trim(now); |
| 180 |
|
| 181 |
var longRunning = _parent._loopScheduler.AsLongRunning(); |
| 182 |
if (longRunning != null) |
| 183 |
_loop.Disposable = longRunning.ScheduleLongRunning(Loop); |
| 184 |
else |
| 185 |
_loop.Disposable = _parent._loopScheduler.Schedule(LoopRec); |
| 186 |
} |
| 187 |
|
| 188 |
private void LoopRec(Action recurse) |
| 189 |
{ |
| 190 |
if (_queue.Count > 0) |
| 191 |
{ |
| 192 |
base._observer.OnNext(_queue.Dequeue().Value); |
| 193 |
recurse(); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
base._observer.OnCompleted(); |
| 198 |
base.Dispose(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
private void Loop(ICancelable cancel) |
| 203 |
{ |
| 204 |
var n = _queue.Count; |
| 205 |
|
| 206 |
while (!cancel.IsDisposed) |
| 207 |
{ |
| 208 |
if (n == 0) |
| 209 |
{ |
| 210 |
base._observer.OnCompleted(); |
| 211 |
break; |
| 212 |
} |
| 213 |
else |
| 214 |
base._observer.OnNext(_queue.Dequeue().Value); |
| 215 |
|
| 216 |
n--; |
| 217 |
} |
| 218 |
|
| 219 |
base.Dispose(); |
| 220 |
} |
| 221 |
|
| 222 |
private void Trim(TimeSpan now) |
| 223 |
{ |
| 224 |
while (_queue.Count > 0 && now - _queue.Peek().Interval >= _parent._duration) |
| 225 |
_queue.Dequeue(); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
#endif |