| 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 Take<TSource> : Producer<TSource> |
| 11 |
{ |
| 12 |
private readonly IObservable<TSource> _source; |
| 13 |
private readonly int _count; |
| 14 |
private readonly TimeSpan _duration; |
| 15 |
internal readonly IScheduler _scheduler; |
| 16 |
|
| 17 |
public Take(IObservable<TSource> source, int count) |
| 18 |
{ |
| 19 |
_source = source; |
| 20 |
_count = count; |
| 21 |
} |
| 22 |
|
| 23 |
public Take(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler) |
| 24 |
{ |
| 25 |
_source = source; |
| 26 |
_duration = duration; |
| 27 |
_scheduler = scheduler; |
| 28 |
} |
| 29 |
|
| 30 |
public IObservable<TSource> Omega(int count) |
| 31 |
{ |
| 32 |
// |
| 33 |
// Minimum semantics: |
| 34 |
// |
| 35 |
// xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--| |
| 36 |
// xs.Take(5) --o--o--o--o--o| xs.Take(3) --o--o--o| |
| 37 |
// xs.Take(5).Take(3) --o--o--o| xs.Take(3).Take(5) --o--o--o| |
| 38 |
// |
| 39 |
if (_count <= count) |
| 40 |
return this; |
| 41 |
else |
| 42 |
return new Take<TSource>(_source, count); |
| 43 |
} |
| 44 |
|
| 45 |
public IObservable<TSource> Omega(TimeSpan duration) |
| 46 |
{ |
| 47 |
// |
| 48 |
// Minimum semantics: |
| 49 |
// |
| 50 |
// t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7-> |
| 51 |
// |
| 52 |
// xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--| |
| 53 |
// xs.Take(5s) --o--o--o--o--o| xs.Take(3s) --o--o--o| |
| 54 |
// xs.Take(5s).Take(3s) --o--o--o| xs.Take(3s).Take(5s) --o--o--o| |
| 55 |
// |
| 56 |
if (_duration <= duration) |
| 57 |
return this; |
| 58 |
else |
| 59 |
return new Take<TSource>(_source, duration, _scheduler); |
| 60 |
} |
| 61 |
|
| 62 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 63 |
{ |
| 64 |
if (_scheduler == null) |
| 65 |
{ |
| 66 |
var sink = new _(this, observer, cancel); |
| 67 |
setSink(sink); |
| 68 |
return _source.SubscribeSafe(sink); |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
var sink = new TakeImpl(this, observer, cancel); |
| 73 |
setSink(sink); |
| 74 |
return sink.Run(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
class _ : Sink<TSource>, IObserver<TSource> |
| 79 |
{ |
| 80 |
private readonly Take<TSource> _parent; |
| 81 |
private int _remaining; |
| 82 |
|
| 83 |
public _(Take<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 84 |
: base(observer, cancel) |
| 85 |
{ |
| 86 |
_parent = parent; |
| 87 |
_remaining = _parent._count; |
| 88 |
} |
| 89 |
|
| 90 |
public void OnNext(TSource value) |
| 91 |
{ |
| 92 |
if (_remaining > 0) |
| 93 |
{ |
| 94 |
--_remaining; |
| 95 |
base._observer.OnNext(value); |
| 96 |
|
| 97 |
if (_remaining == 0) |
| 98 |
{ |
| 99 |
base._observer.OnCompleted(); |
| 100 |
base.Dispose(); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public void OnError(Exception error) |
| 106 |
{ |
| 107 |
base._observer.OnError(error); |
| 108 |
base.Dispose(); |
| 109 |
} |
| 110 |
|
| 111 |
public void OnCompleted() |
| 112 |
{ |
| 113 |
base._observer.OnCompleted(); |
| 114 |
base.Dispose(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
class TakeImpl : Sink<TSource>, IObserver<TSource> |
| 119 |
{ |
| 120 |
private readonly Take<TSource> _parent; |
| 121 |
|
| 122 |
public TakeImpl(Take<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 123 |
: base(observer, cancel) |
| 124 |
{ |
| 125 |
_parent = parent; |
| 126 |
} |
| 127 |
|
| 128 |
private object _gate; |
| 129 |
|
| 130 |
public IDisposable Run() |
| 131 |
{ |
| 132 |
_gate = new object(); |
| 133 |
|
| 134 |
var t = _parent._scheduler.Schedule(_parent._duration, Tick); |
| 135 |
var d = _parent._source.SubscribeSafe(this); |
| 136 |
return new CompositeDisposable(t, d); |
| 137 |
} |
| 138 |
|
| 139 |
private void Tick() |
| 140 |
{ |
| 141 |
lock (_gate) |
| 142 |
{ |
| 143 |
base._observer.OnCompleted(); |
| 144 |
base.Dispose(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public void OnNext(TSource value) |
| 149 |
{ |
| 150 |
lock (_gate) |
| 151 |
{ |
| 152 |
base._observer.OnNext(value); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public void OnError(Exception error) |
| 157 |
{ |
| 158 |
lock (_gate) |
| 159 |
{ |
| 160 |
base._observer.OnError(error); |
| 161 |
base.Dispose(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
public void OnCompleted() |
| 166 |
{ |
| 167 |
lock (_gate) |
| 168 |
{ |
| 169 |
base._observer.OnCompleted(); |
| 170 |
base.Dispose(); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
#endif |