1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#if !NO_PERF
using System;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
namespace System.Reactive.Linq.Observαble
{
class Repeat<TResult> : Producer<TResult>
{
private readonly TResult _value;
private readonly int? _repeatCount;
private readonly IScheduler _scheduler;
public Repeat(TResult value, int? repeatCount, IScheduler scheduler)
{
_value = value;
_repeatCount = repeatCount;
_scheduler = scheduler;
}
protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
{
var sink = new _(this, observer, cancel);
setSink(sink);
return sink.Run();
}
class _ : Sink<TResult>
{
private readonly Repeat<TResult> _parent;
public _(Repeat<TResult> parent, IObserver<TResult> observer, IDisposable cancel)
: base(observer, cancel)
{
_parent = parent;
}
public IDisposable Run()
{
var longRunning = _parent._scheduler.AsLongRunning();
if (longRunning != null)
{
return Run(longRunning);
}
else
{
return Run(_parent._scheduler);
}
}
private IDisposable Run(IScheduler scheduler)
{
if (_parent._repeatCount == null)
{
return scheduler.Schedule(LoopRecInf);
}
else
{
return scheduler.Schedule(_parent._repeatCount.Value, LoopRec);
}
}
private void LoopRecInf(Action recurse)
{
base._observer.OnNext(_parent._value);
recurse();
}
private void LoopRec(int n, Action<int> recurse)
{
if (n > 0)
{
base._observer.OnNext(_parent._value);
n--;
}
if (n == 0)
{
base._observer.OnCompleted();
base.Dispose();
return;
}
recurse(n);
}
private IDisposable Run(ISchedulerLongRunning scheduler)
{
if (_parent._repeatCount == null)
{
return scheduler.ScheduleLongRunning(LoopInf);
}
else
{
return scheduler.ScheduleLongRunning(_parent._repeatCount.Value, Loop);
}
}
private void LoopInf(ICancelable cancel)
{
var value = _parent._value;
while (!cancel.IsDisposed)
base._observer.OnNext(value);
base.Dispose();
}
private void Loop(int n, ICancelable cancel)
{
var value = _parent._value;
while (n > 0 && !cancel.IsDisposed)
{
base._observer.OnNext(value);
n--;
}
if (!cancel.IsDisposed)
base._observer.OnCompleted();
base.Dispose();
}
}
}
}
#endif
|