Catch.cs 5.7 KB

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