SkipWhile.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Reactive.Linq.ObservableImpl
  5. {
  6. internal static class SkipWhile<TSource>
  7. {
  8. internal sealed class Predicate : Producer<TSource, Predicate._>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly Func<TSource, bool> _predicate;
  12. public Predicate(IObservable<TSource> source, Func<TSource, bool> predicate)
  13. {
  14. _source = source;
  15. _predicate = predicate;
  16. }
  17. protected override _ CreateSink(IObserver<TSource> observer) => new _(_predicate, observer);
  18. protected override void Run(_ sink) => sink.Run(_source);
  19. internal sealed class _ : IdentitySink<TSource>
  20. {
  21. private Func<TSource, bool>? _predicate;
  22. public _(Func<TSource, bool> predicate, IObserver<TSource> observer)
  23. : base(observer)
  24. {
  25. _predicate = predicate;
  26. }
  27. public override void OnNext(TSource value)
  28. {
  29. if (_predicate != null)
  30. {
  31. bool shouldStart;
  32. try
  33. {
  34. shouldStart = !_predicate(value);
  35. }
  36. catch (Exception exception)
  37. {
  38. ForwardOnError(exception);
  39. return;
  40. }
  41. if (shouldStart)
  42. {
  43. _predicate = null;
  44. ForwardOnNext(value);
  45. }
  46. }
  47. else
  48. {
  49. ForwardOnNext(value);
  50. }
  51. }
  52. }
  53. }
  54. internal sealed class PredicateIndexed : Producer<TSource, PredicateIndexed._>
  55. {
  56. private readonly IObservable<TSource> _source;
  57. private readonly Func<TSource, int, bool> _predicate;
  58. public PredicateIndexed(IObservable<TSource> source, Func<TSource, int, bool> predicate)
  59. {
  60. _source = source;
  61. _predicate = predicate;
  62. }
  63. protected override _ CreateSink(IObserver<TSource> observer) => new _(_predicate, observer);
  64. protected override void Run(_ sink) => sink.Run(_source);
  65. internal sealed class _ : IdentitySink<TSource>
  66. {
  67. private Func<TSource, int, bool>? _predicate;
  68. private int _index;
  69. public _(Func<TSource, int, bool> predicate, IObserver<TSource> observer)
  70. : base(observer)
  71. {
  72. _predicate = predicate;
  73. }
  74. public override void OnNext(TSource value)
  75. {
  76. if (_predicate != null)
  77. {
  78. bool shouldStart;
  79. try
  80. {
  81. shouldStart = !_predicate(value, checked(_index++));
  82. }
  83. catch (Exception exception)
  84. {
  85. ForwardOnError(exception);
  86. return;
  87. }
  88. if (shouldStart)
  89. {
  90. _predicate = null;
  91. ForwardOnNext(value);
  92. }
  93. }
  94. else
  95. {
  96. ForwardOnNext(value);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }