| 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 Range : Producer<int> |
| 11 |
{ |
| 12 |
private readonly int _start; |
| 13 |
private readonly int _count; |
| 14 |
private readonly IScheduler _scheduler; |
| 15 |
|
| 16 |
public Range(int start, int count, IScheduler scheduler) |
| 17 |
{ |
| 18 |
_start = start; |
| 19 |
_count = count; |
| 20 |
_scheduler = scheduler; |
| 21 |
} |
| 22 |
|
| 23 |
protected override IDisposable Run(IObserver<int> 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<int> |
| 31 |
{ |
| 32 |
private readonly Range _parent; |
| 33 |
|
| 34 |
public _(Range parent, IObserver<int> observer, IDisposable cancel) |
| 35 |
: base(observer, cancel) |
| 36 |
{ |
| 37 |
_parent = parent; |
| 38 |
} |
| 39 |
|
| 40 |
public IDisposable Run() |
| 41 |
{ |
| 42 |
var longRunning = _parent._scheduler.AsLongRunning(); |
| 43 |
if (longRunning != null) |
| 44 |
{ |
| 45 |
return longRunning.ScheduleLongRunning(0, Loop); |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
return _parent._scheduler.Schedule(0, LoopRec); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
private void Loop(int i, ICancelable cancel) |
| 54 |
{ |
| 55 |
while (!cancel.IsDisposed && i < _parent._count) |
| 56 |
{ |
| 57 |
base._observer.OnNext(_parent._start + i); |
| 58 |
i++; |
| 59 |
} |
| 60 |
|
| 61 |
if (!cancel.IsDisposed) |
| 62 |
base._observer.OnCompleted(); |
| 63 |
|
| 64 |
base.Dispose(); |
| 65 |
} |
| 66 |
|
| 67 |
private void LoopRec(int i, Action<int> recurse) |
| 68 |
{ |
| 69 |
if (i < _parent._count) |
| 70 |
{ |
| 71 |
base._observer.OnNext(_parent._start + i); |
| 72 |
recurse(i + 1); |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
base._observer.OnCompleted(); |
| 77 |
base.Dispose(); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
#endif |