Catch<TSource, TException>
using System.
Reactive.
Disposables;
using System.
Threading;
namespace System.
Reactive.
Linq.
ObservableImpl
{
internal sealed class Catch<
TSource,
TException> :
Producer<
TSource,
Catch<
TSource,
TException>.
_>
where TException :
Exception
{
internal sealed class _ :
IdentitySink<
TSource>
{
private readonly Func<
TException,
IObservable<
TSource>>
_handler;
private bool _once;
private IDisposable _subscription;
public _(
Func<
TException,
IObservable<
TSource>>
handler,
IObserver<
TSource>
observer)
:
base(
observer)
{
_handler =
handler;
}
public override void Run(
IObservable<
TSource>
source)
{
Disposable.
TrySetSingle(
ref _subscription,
ObservableExtensions.
SubscribeSafe<
TSource>(
source, (
IObserver<
TSource>)
this));
}
protected override void Dispose(
bool disposing)
{
if (
disposing)
Disposable.
TryDispose(
ref _subscription);
base.
Dispose(
disposing);
}
public override void OnError(
Exception error)
{
if (!
Volatile.
Read(
ref _once)) {
TException val =
error as TException;
if (
val !=
null) {
IObservable<
TSource>
observable =
null;
try {
observable =
_handler(
val);
}
catch (
Exception error2) {
ForwardOnError(
error2);
return;
}
Volatile.
Write(
ref _once,
true);
Disposable.
TrySetSerial(
ref _subscription,
ObservableExtensions.
SubscribeSafe<
TSource>(
observable, (
IObserver<
TSource>)
this));
return;
}
}
ForwardOnError(
error);
}
}
private readonly IObservable<
TSource>
_source;
private readonly Func<
TException,
IObservable<
TSource>>
_handler;
public Catch(
IObservable<
TSource>
source,
Func<
TException,
IObservable<
TSource>>
handler)
{
_source =
source;
_handler =
handler;
}
protected override _ CreateSink(
IObserver<
TSource>
observer)
{
return new _(
_handler,
observer);
}
protected override void Run(
_ sink)
{
sink.
Run(
_source);
}
}
}