Catch.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. internal sealed class Catch<TSource> : Producer<TSource, Catch<TSource>._>
  10. {
  11. private readonly IEnumerable<IObservable<TSource>> _sources;
  12. public Catch(IEnumerable<IObservable<TSource>> sources)
  13. {
  14. _sources = sources;
  15. }
  16. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  17. protected override void Run(_ sink) => sink.Run(_sources);
  18. internal sealed class _ : TailRecursiveSink<TSource>
  19. {
  20. public _(IObserver<TSource> observer)
  21. : base(observer)
  22. {
  23. }
  24. protected override IEnumerable<IObservable<TSource>> Extract(IObservable<TSource> source)
  25. {
  26. if (source is Catch<TSource> @catch)
  27. return @catch._sources;
  28. return null;
  29. }
  30. private Exception _lastException;
  31. public override void OnError(Exception error)
  32. {
  33. _lastException = error;
  34. Recurse();
  35. }
  36. protected override void Done()
  37. {
  38. if (_lastException != null)
  39. ForwardOnError(_lastException);
  40. else
  41. ForwardOnCompleted();
  42. }
  43. protected override bool Fail(Exception error)
  44. {
  45. //
  46. // Note that the invocation of _recurse in OnError will
  47. // cause the next MoveNext operation to be enqueued, so
  48. // we will still return to the caller immediately.
  49. //
  50. OnError(error);
  51. return true;
  52. }
  53. }
  54. }
  55. internal sealed class Catch<TSource, TException> : Producer<TSource, Catch<TSource, TException>._> where TException : Exception
  56. {
  57. private readonly IObservable<TSource> _source;
  58. private readonly Func<TException, IObservable<TSource>> _handler;
  59. public Catch(IObservable<TSource> source, Func<TException, IObservable<TSource>> handler)
  60. {
  61. _source = source;
  62. _handler = handler;
  63. }
  64. protected override _ CreateSink(IObserver<TSource> observer) => new _(_handler, observer);
  65. protected override void Run(_ sink) => sink.Run(_source);
  66. internal sealed class _ : IdentitySink<TSource>
  67. {
  68. private readonly Func<TException, IObservable<TSource>> _handler;
  69. public _(Func<TException, IObservable<TSource>> handler, IObserver<TSource> observer)
  70. : base(observer)
  71. {
  72. _handler = handler;
  73. }
  74. bool _once;
  75. private IDisposable _subscription;
  76. public override void Run(IObservable<TSource> source)
  77. {
  78. Disposable.TrySetSingle(ref _subscription, source.SubscribeSafe(this));
  79. }
  80. protected override void Dispose(bool disposing)
  81. {
  82. if (disposing)
  83. {
  84. Disposable.TryDispose(ref _subscription);
  85. }
  86. base.Dispose(disposing);
  87. }
  88. public override void OnError(Exception error)
  89. {
  90. if (!Volatile.Read(ref _once) && error is TException e)
  91. {
  92. var result = default(IObservable<TSource>);
  93. try
  94. {
  95. result = _handler(e);
  96. }
  97. catch (Exception ex)
  98. {
  99. ForwardOnError(ex);
  100. return;
  101. }
  102. Volatile.Write(ref _once, true);
  103. Disposable.TrySetSerial(ref _subscription, result.SubscribeSafe(this));
  104. }
  105. else
  106. {
  107. ForwardOnError(error);
  108. }
  109. }
  110. }
  111. }
  112. }