| 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 Repeat<TResult> : Producer<TResult> |
| 11 |
{ |
| 12 |
private readonly TResult _value; |
| 13 |
private readonly int? _repeatCount; |
| 14 |
private readonly IScheduler _scheduler; |
| 15 |
|
| 16 |
public Repeat(TResult value, int? repeatCount, IScheduler scheduler) |
| 17 |
{ |
| 18 |
_value = value; |
| 19 |
_repeatCount = repeatCount; |
| 20 |
_scheduler = scheduler; |
| 21 |
} |
| 22 |
|
| 23 |
protected override IDisposable Run(IObserver<TResult> 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<TResult> |
| 31 |
{ |
| 32 |
private readonly Repeat<TResult> _parent; |
| 33 |
|
| 34 |
public _(Repeat<TResult> parent, IObserver<TResult> 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 Run(longRunning); |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
return Run(_parent._scheduler); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
private IDisposable Run(IScheduler scheduler) |
| 54 |
{ |
| 55 |
if (_parent._repeatCount == null) |
| 56 |
{ |
| 57 |
return scheduler.Schedule(LoopRecInf); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
return scheduler.Schedule(_parent._repeatCount.Value, LoopRec); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
private void LoopRecInf(Action recurse) |
| 66 |
{ |
| 67 |
base._observer.OnNext(_parent._value); |
| 68 |
recurse(); |
| 69 |
} |
| 70 |
|
| 71 |
private void LoopRec(int n, Action<int> recurse) |
| 72 |
{ |
| 73 |
if (n > 0) |
| 74 |
{ |
| 75 |
base._observer.OnNext(_parent._value); |
| 76 |
n--; |
| 77 |
} |
| 78 |
|
| 79 |
if (n == 0) |
| 80 |
{ |
| 81 |
base._observer.OnCompleted(); |
| 82 |
base.Dispose(); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
recurse(n); |
| 87 |
} |
| 88 |
|
| 89 |
private IDisposable Run(ISchedulerLongRunning scheduler) |
| 90 |
{ |
| 91 |
if (_parent._repeatCount == null) |
| 92 |
{ |
| 93 |
return scheduler.ScheduleLongRunning(LoopInf); |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
return scheduler.ScheduleLongRunning(_parent._repeatCount.Value, Loop); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
private void LoopInf(ICancelable cancel) |
| 102 |
{ |
| 103 |
var value = _parent._value; |
| 104 |
while (!cancel.IsDisposed) |
| 105 |
base._observer.OnNext(value); |
| 106 |
|
| 107 |
base.Dispose(); |
| 108 |
} |
| 109 |
|
| 110 |
private void Loop(int n, ICancelable cancel) |
| 111 |
{ |
| 112 |
var value = _parent._value; |
| 113 |
while (n > 0 && !cancel.IsDisposed) |
| 114 |
{ |
| 115 |
base._observer.OnNext(value); |
| 116 |
n--; |
| 117 |
} |
| 118 |
|
| 119 |
if (!cancel.IsDisposed) |
| 120 |
base._observer.OnCompleted(); |
| 121 |
|
| 122 |
base.Dispose(); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
#endif |