| 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.Diagnostics; |
| 6 |
using System.Reactive.Concurrency; |
| 7 |
using System.Reactive.Disposables; |
| 8 |
using System.Threading; |
| 9 |
|
| 10 |
namespace System.Reactive.Linq.ObservableImpl |
| 11 |
{ |
| 12 |
class Generate<TState, TResult> : Producer<TResult> |
| 13 |
{ |
| 14 |
private readonly TState _initialState; |
| 15 |
private readonly Func<TState, bool> _condition; |
| 16 |
private readonly Func<TState, TState> _iterate; |
| 17 |
private readonly Func<TState, TResult> _resultSelector; |
| 18 |
private readonly Func<TState, DateTimeOffset> _timeSelectorA; |
| 19 |
private readonly Func<TState, TimeSpan> _timeSelectorR; |
| 20 |
private readonly IScheduler _scheduler; |
| 21 |
|
| 22 |
public Generate(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IScheduler scheduler) |
| 23 |
{ |
| 24 |
_initialState = initialState; |
| 25 |
_condition = condition; |
| 26 |
_iterate = iterate; |
| 27 |
_resultSelector = resultSelector; |
| 28 |
_scheduler = scheduler; |
| 29 |
} |
| 30 |
|
| 31 |
public Generate(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, Func<TState, DateTimeOffset> timeSelector, IScheduler scheduler) |
| 32 |
{ |
| 33 |
_initialState = initialState; |
| 34 |
_condition = condition; |
| 35 |
_iterate = iterate; |
| 36 |
_resultSelector = resultSelector; |
| 37 |
_timeSelectorA = timeSelector; |
| 38 |
_scheduler = scheduler; |
| 39 |
} |
| 40 |
|
| 41 |
public Generate(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, Func<TState, TimeSpan> timeSelector, IScheduler scheduler) |
| 42 |
{ |
| 43 |
_initialState = initialState; |
| 44 |
_condition = condition; |
| 45 |
_iterate = iterate; |
| 46 |
_resultSelector = resultSelector; |
| 47 |
_timeSelectorR = timeSelector; |
| 48 |
_scheduler = scheduler; |
| 49 |
} |
| 50 |
|
| 51 |
protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 52 |
{ |
| 53 |
if (_timeSelectorA != null) |
| 54 |
{ |
| 55 |
var sink = new SelectorA(this, observer, cancel); |
| 56 |
setSink(sink); |
| 57 |
return sink.Run(); |
| 58 |
} |
| 59 |
else if (_timeSelectorR != null) |
| 60 |
{ |
| 61 |
var sink = new Delta(this, observer, cancel); |
| 62 |
setSink(sink); |
| 63 |
return sink.Run(); |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
var sink = new _(this, observer, cancel); |
| 68 |
setSink(sink); |
| 69 |
return sink.Run(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
class SelectorA : Sink<TResult> |
| 74 |
{ |
| 75 |
private readonly Generate<TState, TResult> _parent; |
| 76 |
|
| 77 |
public SelectorA(Generate<TState, TResult> parent, IObserver<TResult> observer, IDisposable cancel) |
| 78 |
: base(observer, cancel) |
| 79 |
{ |
| 80 |
_parent = parent; |
| 81 |
} |
| 82 |
|
| 83 |
private bool _first; |
| 84 |
private bool _hasResult; |
| 85 |
private TResult _result; |
| 86 |
|
| 87 |
public IDisposable Run() |
| 88 |
{ |
| 89 |
_first = true; |
| 90 |
_hasResult = false; |
| 91 |
_result = default(TResult); |
| 92 |
|
| 93 |
return _parent._scheduler.Schedule(_parent._initialState, InvokeRec); |
| 94 |
} |
| 95 |
|
| 96 |
private IDisposable InvokeRec(IScheduler self, TState state) |
| 97 |
{ |
| 98 |
var time = default(DateTimeOffset); |
| 99 |
|
| 100 |
if (_hasResult) |
| 101 |
base._observer.OnNext(_result); |
| 102 |
try |
| 103 |
{ |
| 104 |
if (_first) |
| 105 |
_first = false; |
| 106 |
else |
| 107 |
state = _parent._iterate(state); |
| 108 |
_hasResult = _parent._condition(state); |
| 109 |
if (_hasResult) |
| 110 |
{ |
| 111 |
_result = _parent._resultSelector(state); |
| 112 |
time = _parent._timeSelectorA(state); |
| 113 |
} |
| 114 |
} |
| 115 |
catch (Exception exception) |
| 116 |
{ |
| 117 |
base._observer.OnError(exception); |
| 118 |
base.Dispose(); |
| 119 |
return Disposable.Empty; |
| 120 |
} |
| 121 |
|
| 122 |
if (!_hasResult) |
| 123 |
{ |
| 124 |
base._observer.OnCompleted(); |
| 125 |
base.Dispose(); |
| 126 |
return Disposable.Empty; |
| 127 |
} |
| 128 |
|
| 129 |
return self.Schedule(state, time, InvokeRec); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
class Delta : Sink<TResult> |
| 134 |
{ |
| 135 |
private readonly Generate<TState, TResult> _parent; |
| 136 |
|
| 137 |
public Delta(Generate<TState, TResult> parent, IObserver<TResult> observer, IDisposable cancel) |
| 138 |
: base(observer, cancel) |
| 139 |
{ |
| 140 |
_parent = parent; |
| 141 |
} |
| 142 |
|
| 143 |
private bool _first; |
| 144 |
private bool _hasResult; |
| 145 |
private TResult _result; |
| 146 |
|
| 147 |
public IDisposable Run() |
| 148 |
{ |
| 149 |
_first = true; |
| 150 |
_hasResult = false; |
| 151 |
_result = default(TResult); |
| 152 |
|
| 153 |
return _parent._scheduler.Schedule(_parent._initialState, InvokeRec); |
| 154 |
} |
| 155 |
|
| 156 |
private IDisposable InvokeRec(IScheduler self, TState state) |
| 157 |
{ |
| 158 |
var time = default(TimeSpan); |
| 159 |
|
| 160 |
if (_hasResult) |
| 161 |
base._observer.OnNext(_result); |
| 162 |
try |
| 163 |
{ |
| 164 |
if (_first) |
| 165 |
_first = false; |
| 166 |
else |
| 167 |
state = _parent._iterate(state); |
| 168 |
_hasResult = _parent._condition(state); |
| 169 |
if (_hasResult) |
| 170 |
{ |
| 171 |
_result = _parent._resultSelector(state); |
| 172 |
time = _parent._timeSelectorR(state); |
| 173 |
} |
| 174 |
} |
| 175 |
catch (Exception exception) |
| 176 |
{ |
| 177 |
base._observer.OnError(exception); |
| 178 |
base.Dispose(); |
| 179 |
return Disposable.Empty; |
| 180 |
} |
| 181 |
|
| 182 |
if (!_hasResult) |
| 183 |
{ |
| 184 |
base._observer.OnCompleted(); |
| 185 |
base.Dispose(); |
| 186 |
return Disposable.Empty; |
| 187 |
} |
| 188 |
|
| 189 |
return self.Schedule(state, time, InvokeRec); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
class _ : Sink<TResult> |
| 194 |
{ |
| 195 |
private readonly Generate<TState, TResult> _parent; |
| 196 |
|
| 197 |
public _(Generate<TState, TResult> parent, IObserver<TResult> observer, IDisposable cancel) |
| 198 |
: base(observer, cancel) |
| 199 |
{ |
| 200 |
_parent = parent; |
| 201 |
} |
| 202 |
|
| 203 |
private TState _state; |
| 204 |
private bool _first; |
| 205 |
|
| 206 |
public IDisposable Run() |
| 207 |
{ |
| 208 |
_state = _parent._initialState; |
| 209 |
_first = true; |
| 210 |
|
| 211 |
var longRunning = _parent._scheduler.AsLongRunning(); |
| 212 |
if (longRunning != null) |
| 213 |
{ |
| 214 |
return longRunning.ScheduleLongRunning(Loop); |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
return _parent._scheduler.Schedule(LoopRec); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
private void Loop(ICancelable cancel) |
| 223 |
{ |
| 224 |
while (!cancel.IsDisposed) |
| 225 |
{ |
| 226 |
var hasResult = false; |
| 227 |
var result = default(TResult); |
| 228 |
try |
| 229 |
{ |
| 230 |
if (_first) |
| 231 |
_first = false; |
| 232 |
else |
| 233 |
_state = _parent._iterate(_state); |
| 234 |
hasResult = _parent._condition(_state); |
| 235 |
if (hasResult) |
| 236 |
result = _parent._resultSelector(_state); |
| 237 |
} |
| 238 |
catch (Exception exception) |
| 239 |
{ |
| 240 |
base._observer.OnError(exception); |
| 241 |
base.Dispose(); |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
if (hasResult) |
| 246 |
base._observer.OnNext(result); |
| 247 |
else |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
if (!cancel.IsDisposed) |
| 252 |
base._observer.OnCompleted(); |
| 253 |
|
| 254 |
base.Dispose(); |
| 255 |
} |
| 256 |
|
| 257 |
private void LoopRec(Action recurse) |
| 258 |
{ |
| 259 |
var hasResult = false; |
| 260 |
var result = default(TResult); |
| 261 |
try |
| 262 |
{ |
| 263 |
if (_first) |
| 264 |
_first = false; |
| 265 |
else |
| 266 |
_state = _parent._iterate(_state); |
| 267 |
hasResult = _parent._condition(_state); |
| 268 |
if (hasResult) |
| 269 |
result = _parent._resultSelector(_state); |
| 270 |
} |
| 271 |
catch (Exception exception) |
| 272 |
{ |
| 273 |
base._observer.OnError(exception); |
| 274 |
base.Dispose(); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
if (hasResult) |
| 279 |
{ |
| 280 |
base._observer.OnNext(result); |
| 281 |
recurse(); |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
base._observer.OnCompleted(); |
| 286 |
base.Dispose(); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
#endif |