| 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 Catch<TSource> : Producer<TSource> |
| 12 |
{ |
| 13 |
private readonly IEnumerable<IObservable<TSource>> _sources; |
| 14 |
|
| 15 |
public Catch(IEnumerable<IObservable<TSource>> sources) |
| 16 |
{ |
| 17 |
_sources = sources; |
| 18 |
} |
| 19 |
|
| 20 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 21 |
{ |
| 22 |
var sink = new _(observer, cancel); |
| 23 |
setSink(sink); |
| 24 |
return sink.Run(_sources); |
| 25 |
} |
| 26 |
|
| 27 |
class _ : TailRecursiveSink<TSource> |
| 28 |
{ |
| 29 |
public _(IObserver<TSource> observer, IDisposable cancel) |
| 30 |
: base(observer, cancel) |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
protected override IEnumerable<IObservable<TSource>> Extract(IObservable<TSource> source) |
| 35 |
{ |
| 36 |
var @catch = source as Catch<TSource>; |
| 37 |
if (@catch != null) |
| 38 |
return @catch._sources; |
| 39 |
|
| 40 |
return null; |
| 41 |
} |
| 42 |
|
| 43 |
public override void OnNext(TSource value) |
| 44 |
{ |
| 45 |
base._observer.OnNext(value); |
| 46 |
} |
| 47 |
|
| 48 |
private Exception _lastException; |
| 49 |
|
| 50 |
public override void OnError(Exception error) |
| 51 |
{ |
| 52 |
_lastException = error; |
| 53 |
_recurse(); |
| 54 |
} |
| 55 |
|
| 56 |
public override void OnCompleted() |
| 57 |
{ |
| 58 |
base._observer.OnCompleted(); |
| 59 |
base.Dispose(); |
| 60 |
} |
| 61 |
|
| 62 |
protected override void Done() |
| 63 |
{ |
| 64 |
if (_lastException != null) |
| 65 |
base._observer.OnError(_lastException); |
| 66 |
else |
| 67 |
base._observer.OnCompleted(); |
| 68 |
|
| 69 |
base.Dispose(); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
class Catch<TSource, TException> : Producer<TSource> where TException : Exception |
| 75 |
{ |
| 76 |
private readonly IObservable<TSource> _source; |
| 77 |
private readonly Func<TException, IObservable<TSource>> _handler; |
| 78 |
|
| 79 |
public Catch(IObservable<TSource> source, Func<TException, IObservable<TSource>> handler) |
| 80 |
{ |
| 81 |
_source = source; |
| 82 |
_handler = handler; |
| 83 |
} |
| 84 |
|
| 85 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 86 |
{ |
| 87 |
var sink = new _(this, observer, cancel); |
| 88 |
setSink(sink); |
| 89 |
return sink.Run(); |
| 90 |
} |
| 91 |
|
| 92 |
class _ : Sink<TSource>, IObserver<TSource> |
| 93 |
{ |
| 94 |
private readonly Catch<TSource, TException> _parent; |
| 95 |
|
| 96 |
public _(Catch<TSource, TException> parent, IObserver<TSource> observer, IDisposable cancel) |
| 97 |
: base(observer, cancel) |
| 98 |
{ |
| 99 |
_parent = parent; |
| 100 |
} |
| 101 |
|
| 102 |
private SerialDisposable _subscription; |
| 103 |
|
| 104 |
public IDisposable Run() |
| 105 |
{ |
| 106 |
_subscription = new SerialDisposable(); |
| 107 |
|
| 108 |
var d1 = new SingleAssignmentDisposable(); |
| 109 |
_subscription.Disposable = d1; |
| 110 |
d1.Disposable = _parent._source.SubscribeSafe(this); |
| 111 |
|
| 112 |
return _subscription; |
| 113 |
} |
| 114 |
|
| 115 |
public void OnNext(TSource value) |
| 116 |
{ |
| 117 |
base._observer.OnNext(value); |
| 118 |
} |
| 119 |
|
| 120 |
public void OnError(Exception error) |
| 121 |
{ |
| 122 |
var e = error as TException; |
| 123 |
if (e != null) |
| 124 |
{ |
| 125 |
var result = default(IObservable<TSource>); |
| 126 |
try |
| 127 |
{ |
| 128 |
result = _parent._handler(e); |
| 129 |
} |
| 130 |
catch (Exception ex) |
| 131 |
{ |
| 132 |
base._observer.OnError(ex); |
| 133 |
base.Dispose(); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
var d = new SingleAssignmentDisposable(); |
| 138 |
_subscription.Disposable = d; |
| 139 |
d.Disposable = result.SubscribeSafe(new Impl(this)); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
base._observer.OnError(error); |
| 144 |
base.Dispose(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public void OnCompleted() |
| 149 |
{ |
| 150 |
base._observer.OnCompleted(); |
| 151 |
base.Dispose(); |
| 152 |
} |
| 153 |
|
| 154 |
class Impl : IObserver<TSource> |
| 155 |
{ |
| 156 |
private readonly _ _parent; |
| 157 |
|
| 158 |
public Impl(_ parent) |
| 159 |
{ |
| 160 |
_parent = parent; |
| 161 |
} |
| 162 |
|
| 163 |
public void OnNext(TSource value) |
| 164 |
{ |
| 165 |
_parent._observer.OnNext(value); |
| 166 |
} |
| 167 |
|
| 168 |
public void OnError(Exception error) |
| 169 |
{ |
| 170 |
_parent._observer.OnError(error); |
| 171 |
_parent.Dispose(); |
| 172 |
} |
| 173 |
|
| 174 |
public void OnCompleted() |
| 175 |
{ |
| 176 |
_parent._observer.OnCompleted(); |
| 177 |
_parent.Dispose(); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
#endif |