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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// 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.Collections.Generic;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
namespace System.Reactive.Linq.ObservableImpl
{
class ToObservable<TSource> : Producer<TSource>
{
private readonly IEnumerable<TSource> _source;
private readonly IScheduler _scheduler;
public ToObservable(IEnumerable<TSource> source, IScheduler scheduler)
{
_source = source;
_scheduler = scheduler;
}
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
{
var sink = new _(this, observer, cancel);
setSink(sink);
return sink.Run();
}
class _ : Sink<TSource>
{
private readonly ToObservable<TSource> _parent;
public _(ToObservable<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
: base(observer, cancel)
{
_parent = parent;
}
public IDisposable Run()
{
var e = default(IEnumerator<TSource>);
try
{
e = _parent._source.GetEnumerator();
}
catch (Exception exception)
{
base._observer.OnError(exception);
base.Dispose();
return Disposable.Empty;
}
var longRunning = _parent._scheduler.AsLongRunning();
if (longRunning != null)
{
//
// Long-running schedulers have the contract they should *never* prevent
// the work from starting, such that the scheduled work has the chance
// to observe the cancellation and perform proper clean-up. In this case,
// we're sure Loop will be entered, allowing us to dispose the enumerator.
//
return longRunning.ScheduleLongRunning(e, Loop);
}
else
{
//
// We never allow the scheduled work to be cancelled. Instead, the flag
// is used to have LoopRec bail out and perform proper clean-up of the
// enumerator.
//
var flag = new BooleanDisposable();
_parent._scheduler.Schedule(new State(flag, e), LoopRec);
return flag;
}
}
class State
{
public readonly ICancelable flag;
public readonly IEnumerator<TSource> enumerator;
public State(ICancelable flag, IEnumerator<TSource> enumerator)
{
this.flag = flag;
this.enumerator = enumerator;
}
}
private void LoopRec(State state, Action<State> recurse)
{
var hasNext = false;
var ex = default(Exception);
var current = default(TSource);
if (state.flag.IsDisposed)
{
state.enumerator.Dispose();
return;
}
try
{
hasNext = state.enumerator.MoveNext();
if (hasNext)
current = state.enumerator.Current;
}
catch (Exception exception)
{
ex = exception;
}
if (ex != null)
{
state.enumerator.Dispose();
base._observer.OnError(ex);
base.Dispose();
return;
}
if (!hasNext)
{
state.enumerator.Dispose();
base._observer.OnCompleted();
base.Dispose();
return;
}
base._observer.OnNext(current);
recurse(state);
}
private void Loop(IEnumerator<TSource> enumerator, ICancelable cancel)
{
while (!cancel.IsDisposed)
{
var hasNext = false;
var ex = default(Exception);
var current = default(TSource);
try
{
hasNext = enumerator.MoveNext();
if (hasNext)
current = enumerator.Current;
}
catch (Exception exception)
{
ex = exception;
}
if (ex != null)
{
base._observer.OnError(ex);
break;
}
if (!hasNext)
{
base._observer.OnCompleted();
break;
}
base._observer.OnNext(current);
}
enumerator.Dispose();
base.Dispose();
}
}
}
}
#endif
|