| 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.Collections.Generic; |
| 6 |
using System.Reactive.Concurrency; |
| 7 |
using System.Reactive.Disposables; |
| 8 |
|
| 9 |
namespace System.Reactive.Linq.ObservableImpl |
| 10 |
{ |
| 11 |
class ToObservable<TSource> : Producer<TSource> |
| 12 |
{ |
| 13 |
private readonly IEnumerable<TSource> _source; |
| 14 |
private readonly IScheduler _scheduler; |
| 15 |
|
| 16 |
public ToObservable(IEnumerable<TSource> source, IScheduler scheduler) |
| 17 |
{ |
| 18 |
_source = source; |
| 19 |
_scheduler = scheduler; |
| 20 |
} |
| 21 |
|
| 22 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 23 |
{ |
| 24 |
var sink = new _(this, observer, cancel); |
| 25 |
setSink(sink); |
| 26 |
return sink.Run(); |
| 27 |
} |
| 28 |
|
| 29 |
class _ : Sink<TSource> |
| 30 |
{ |
| 31 |
private readonly ToObservable<TSource> _parent; |
| 32 |
|
| 33 |
public _(ToObservable<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 34 |
: base(observer, cancel) |
| 35 |
{ |
| 36 |
_parent = parent; |
| 37 |
} |
| 38 |
|
| 39 |
public IDisposable Run() |
| 40 |
{ |
| 41 |
var e = default(IEnumerator<TSource>); |
| 42 |
try |
| 43 |
{ |
| 44 |
e = _parent._source.GetEnumerator(); |
| 45 |
} |
| 46 |
catch (Exception exception) |
| 47 |
{ |
| 48 |
base._observer.OnError(exception); |
| 49 |
base.Dispose(); |
| 50 |
return Disposable.Empty; |
| 51 |
} |
| 52 |
|
| 53 |
var longRunning = _parent._scheduler.AsLongRunning(); |
| 54 |
if (longRunning != null) |
| 55 |
{ |
| 56 |
// |
| 57 |
// Long-running schedulers have the contract they should *never* prevent |
| 58 |
// the work from starting, such that the scheduled work has the chance |
| 59 |
// to observe the cancellation and perform proper clean-up. In this case, |
| 60 |
// we're sure Loop will be entered, allowing us to dispose the enumerator. |
| 61 |
// |
| 62 |
return longRunning.ScheduleLongRunning(e, Loop); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
// |
| 67 |
// We never allow the scheduled work to be cancelled. Instead, the flag |
| 68 |
// is used to have LoopRec bail out and perform proper clean-up of the |
| 69 |
// enumerator. |
| 70 |
// |
| 71 |
var flag = new BooleanDisposable(); |
| 72 |
_parent._scheduler.Schedule(new State(flag, e), LoopRec); |
| 73 |
return flag; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
class State |
| 78 |
{ |
| 79 |
public readonly ICancelable flag; |
| 80 |
public readonly IEnumerator<TSource> enumerator; |
| 81 |
|
| 82 |
public State(ICancelable flag, IEnumerator<TSource> enumerator) |
| 83 |
{ |
| 84 |
this.flag = flag; |
| 85 |
this.enumerator = enumerator; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private void LoopRec(State state, Action<State> recurse) |
| 90 |
{ |
| 91 |
var hasNext = false; |
| 92 |
var ex = default(Exception); |
| 93 |
var current = default(TSource); |
| 94 |
|
| 95 |
if (state.flag.IsDisposed) |
| 96 |
{ |
| 97 |
state.enumerator.Dispose(); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
try |
| 102 |
{ |
| 103 |
hasNext = state.enumerator.MoveNext(); |
| 104 |
if (hasNext) |
| 105 |
current = state.enumerator.Current; |
| 106 |
} |
| 107 |
catch (Exception exception) |
| 108 |
{ |
| 109 |
ex = exception; |
| 110 |
} |
| 111 |
|
| 112 |
if (ex != null) |
| 113 |
{ |
| 114 |
state.enumerator.Dispose(); |
| 115 |
|
| 116 |
base._observer.OnError(ex); |
| 117 |
base.Dispose(); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if (!hasNext) |
| 122 |
{ |
| 123 |
state.enumerator.Dispose(); |
| 124 |
|
| 125 |
base._observer.OnCompleted(); |
| 126 |
base.Dispose(); |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
base._observer.OnNext(current); |
| 131 |
recurse(state); |
| 132 |
} |
| 133 |
|
| 134 |
private void Loop(IEnumerator<TSource> enumerator, ICancelable cancel) |
| 135 |
{ |
| 136 |
while (!cancel.IsDisposed) |
| 137 |
{ |
| 138 |
var hasNext = false; |
| 139 |
var ex = default(Exception); |
| 140 |
var current = default(TSource); |
| 141 |
|
| 142 |
try |
| 143 |
{ |
| 144 |
hasNext = enumerator.MoveNext(); |
| 145 |
if (hasNext) |
| 146 |
current = enumerator.Current; |
| 147 |
} |
| 148 |
catch (Exception exception) |
| 149 |
{ |
| 150 |
ex = exception; |
| 151 |
} |
| 152 |
|
| 153 |
if (ex != null) |
| 154 |
{ |
| 155 |
base._observer.OnError(ex); |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
if (!hasNext) |
| 160 |
{ |
| 161 |
base._observer.OnCompleted(); |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
base._observer.OnNext(current); |
| 166 |
} |
| 167 |
|
| 168 |
enumerator.Dispose(); |
| 169 |
base.Dispose(); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
#endif |