| 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 Skip<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 Skip(IObservable<TSource> source, int count) |
| 18 |
{ |
| 19 |
_source = source; |
| 20 |
_count = count; |
| 21 |
} |
| 22 |
|
| 23 |
public Skip(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 |
// Sum semantics: |
| 34 |
// |
| 35 |
// xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--| |
| 36 |
// xs.Skip(2) --x--x--o--o--o--o--| xs.Skip(3) --x--x--x--o--o--o--| |
| 37 |
// xs.Skip(2).Skip(3) --------x--x--x--o--| xs.Skip(3).Skip(2) -----------x--x--o--| |
| 38 |
// |
| 39 |
return new Skip<TSource>(_source, _count + count); |
| 40 |
} |
| 41 |
|
| 42 |
public IObservable<TSource> Omega(TimeSpan duration) |
| 43 |
{ |
| 44 |
// |
| 45 |
// Maximum semantics: |
| 46 |
// |
| 47 |
// t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7-> |
| 48 |
// |
| 49 |
// xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--| |
| 50 |
// xs.Skip(2s) xxxxxxx-o--o--o--o--| xs.Skip(3s) xxxxxxxxxx-o--o--o--| |
| 51 |
// xs.Skip(2s).Skip(3s) xxxxxxxxxx-o--o--o--| xs.Skip(3s).Skip(2s) xxxxxxx----o--o--o--| |
| 52 |
// |
| 53 |
if (duration <= _duration) |
| 54 |
return this; |
| 55 |
else |
| 56 |
return new Skip<TSource>(_source, duration, _scheduler); |
| 57 |
} |
| 58 |
|
| 59 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 60 |
{ |
| 61 |
if (_scheduler == null) |
| 62 |
{ |
| 63 |
var sink = new _(this, observer, cancel); |
| 64 |
setSink(sink); |
| 65 |
return _source.SubscribeSafe(sink); |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
var sink = new SkipImpl(this, observer, cancel); |
| 70 |
setSink(sink); |
| 71 |
return sink.Run(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
class _ : Sink<TSource>, IObserver<TSource> |
| 76 |
{ |
| 77 |
private readonly Skip<TSource> _parent; |
| 78 |
private int _remaining; |
| 79 |
|
| 80 |
public _(Skip<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 81 |
: base(observer, cancel) |
| 82 |
{ |
| 83 |
_parent = parent; |
| 84 |
_remaining = _parent._count; |
| 85 |
} |
| 86 |
|
| 87 |
public void OnNext(TSource value) |
| 88 |
{ |
| 89 |
if (_remaining <= 0) |
| 90 |
base._observer.OnNext(value); |
| 91 |
else |
| 92 |
_remaining--; |
| 93 |
} |
| 94 |
|
| 95 |
public void OnError(Exception error) |
| 96 |
{ |
| 97 |
base._observer.OnError(error); |
| 98 |
base.Dispose(); |
| 99 |
} |
| 100 |
|
| 101 |
public void OnCompleted() |
| 102 |
{ |
| 103 |
base._observer.OnCompleted(); |
| 104 |
base.Dispose(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
class SkipImpl : Sink<TSource>, IObserver<TSource> |
| 109 |
{ |
| 110 |
private readonly Skip<TSource> _parent; |
| 111 |
private volatile bool _open; |
| 112 |
|
| 113 |
public SkipImpl(Skip<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 114 |
: base(observer, cancel) |
| 115 |
{ |
| 116 |
_parent = parent; |
| 117 |
} |
| 118 |
|
| 119 |
public IDisposable Run() |
| 120 |
{ |
| 121 |
var t = _parent._scheduler.Schedule(_parent._duration, Tick); |
| 122 |
var d = _parent._source.SubscribeSafe(this); |
| 123 |
return new CompositeDisposable(t, d); |
| 124 |
} |
| 125 |
|
| 126 |
private void Tick() |
| 127 |
{ |
| 128 |
_open = true; |
| 129 |
} |
| 130 |
|
| 131 |
public void OnNext(TSource value) |
| 132 |
{ |
| 133 |
if (_open) |
| 134 |
base._observer.OnNext(value); |
| 135 |
} |
| 136 |
|
| 137 |
public void OnError(Exception error) |
| 138 |
{ |
| 139 |
base._observer.OnError(error); |
| 140 |
base.Dispose(); |
| 141 |
} |
| 142 |
|
| 143 |
public void OnCompleted() |
| 144 |
{ |
| 145 |
base._observer.OnCompleted(); |
| 146 |
base.Dispose(); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
#endif |