OnErrorResumeNext.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. class OnErrorResumeNext<TSource> : Producer<TSource>
  10. {
  11. private readonly IEnumerable<IObservable<TSource>> _sources;
  12. public OnErrorResumeNext(IEnumerable<IObservable<TSource>> sources)
  13. {
  14. _sources = sources;
  15. }
  16. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  17. {
  18. var sink = new _(observer, cancel);
  19. setSink(sink);
  20. return sink.Run(_sources);
  21. }
  22. class _ : TailRecursiveSink<TSource>
  23. {
  24. public _(IObserver<TSource> observer, IDisposable cancel)
  25. : base(observer, cancel)
  26. {
  27. }
  28. protected override IEnumerable<IObservable<TSource>> Extract(IObservable<TSource> source)
  29. {
  30. var oern = source as OnErrorResumeNext<TSource>;
  31. if (oern != null)
  32. return oern._sources;
  33. return null;
  34. }
  35. public override void OnNext(TSource value)
  36. {
  37. base._observer.OnNext(value);
  38. }
  39. public override void OnError(Exception error)
  40. {
  41. _recurse();
  42. }
  43. public override void OnCompleted()
  44. {
  45. _recurse();
  46. }
  47. protected override bool Fail(Exception error)
  48. {
  49. //
  50. // Note that the invocation of _recurse in OnError will
  51. // cause the next MoveNext operation to be enqueued, so
  52. // we will still return to the caller immediately.
  53. //
  54. OnError(error);
  55. return true;
  56. }
  57. }
  58. }
  59. }
  60. #endif