OnErrorResumeNext.cs 1.9 KB

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