TakeWhile.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 TakeWhile<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 readonly Func<TSource, bool> _predicate;
  22. private bool _running;
  23. public _(Func<TSource, bool> predicate, IObserver<TSource> observer)
  24. : base(observer)
  25. {
  26. _predicate = predicate;
  27. _running = true;
  28. }
  29. public override void OnNext(TSource value)
  30. {
  31. if (_running)
  32. {
  33. try
  34. {
  35. _running = _predicate(value);
  36. }
  37. catch (Exception exception)
  38. {
  39. ForwardOnError(exception);
  40. return;
  41. }
  42. if (_running)
  43. {
  44. ForwardOnNext(value);
  45. }
  46. else
  47. {
  48. ForwardOnCompleted();
  49. }
  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 readonly Func<TSource, int, bool> _predicate;
  68. private bool _running;
  69. private int _index;
  70. public _(Func<TSource, int, bool> predicate, IObserver<TSource> observer)
  71. : base(observer)
  72. {
  73. _predicate = predicate;
  74. _running = true;
  75. }
  76. public override void OnNext(TSource value)
  77. {
  78. if (_running)
  79. {
  80. try
  81. {
  82. _running = _predicate(value, checked(_index++));
  83. }
  84. catch (Exception exception)
  85. {
  86. ForwardOnError(exception);
  87. return;
  88. }
  89. if (_running)
  90. {
  91. ForwardOnNext(value);
  92. }
  93. else
  94. {
  95. ForwardOnCompleted();
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }