TakeWhile.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. namespace System.Reactive.Linq.ObservableImpl
  5. {
  6. internal static class TakeWhile<TSource>
  7. {
  8. internal sealed class Predicate : Producer<TSource>
  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 IDisposable CreateSink(IObserver<TSource> observer, IDisposable cancel) => new _(_predicate, observer, cancel);
  18. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  19. {
  20. var sink = new _(_predicate, observer, cancel);
  21. setSink(sink);
  22. return _source.SubscribeSafe(sink);
  23. }
  24. private sealed class _ : Sink<TSource>, IObserver<TSource>
  25. {
  26. private readonly Func<TSource, bool> _predicate;
  27. private bool _running;
  28. public _(Func<TSource, bool> predicate, IObserver<TSource> observer, IDisposable cancel)
  29. : base(observer, cancel)
  30. {
  31. _predicate = predicate;
  32. _running = true;
  33. }
  34. public void OnNext(TSource value)
  35. {
  36. if (_running)
  37. {
  38. try
  39. {
  40. _running = _predicate(value);
  41. }
  42. catch (Exception exception)
  43. {
  44. base._observer.OnError(exception);
  45. base.Dispose();
  46. return;
  47. }
  48. if (_running)
  49. {
  50. base._observer.OnNext(value);
  51. }
  52. else
  53. {
  54. base._observer.OnCompleted();
  55. base.Dispose();
  56. }
  57. }
  58. }
  59. public void OnError(Exception error)
  60. {
  61. base._observer.OnError(error);
  62. base.Dispose();
  63. }
  64. public void OnCompleted()
  65. {
  66. base._observer.OnCompleted();
  67. base.Dispose();
  68. }
  69. }
  70. }
  71. internal sealed class PredicateIndexed : Producer<TSource>
  72. {
  73. private readonly IObservable<TSource> _source;
  74. private readonly Func<TSource, int, bool> _predicate;
  75. public PredicateIndexed(IObservable<TSource> source, Func<TSource, int, bool> predicate)
  76. {
  77. _source = source;
  78. _predicate = predicate;
  79. }
  80. protected override IDisposable CreateSink(IObserver<TSource> observer, IDisposable cancel) => new _(_predicate, observer, cancel);
  81. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  82. {
  83. var sink = new _(_predicate, observer, cancel);
  84. setSink(sink);
  85. return _source.SubscribeSafe(sink);
  86. }
  87. private sealed class _ : Sink<TSource>, IObserver<TSource>
  88. {
  89. private readonly Func<TSource, int, bool> _predicate;
  90. private bool _running;
  91. private int _index;
  92. public _(Func<TSource, int, bool> predicate, IObserver<TSource> observer, IDisposable cancel)
  93. : base(observer, cancel)
  94. {
  95. _predicate = predicate;
  96. _running = true;
  97. _index = 0;
  98. }
  99. public void OnNext(TSource value)
  100. {
  101. if (_running)
  102. {
  103. try
  104. {
  105. _running = _predicate(value, checked(_index++));
  106. }
  107. catch (Exception exception)
  108. {
  109. base._observer.OnError(exception);
  110. base.Dispose();
  111. return;
  112. }
  113. if (_running)
  114. {
  115. base._observer.OnNext(value);
  116. }
  117. else
  118. {
  119. base._observer.OnCompleted();
  120. base.Dispose();
  121. }
  122. }
  123. }
  124. public void OnError(Exception error)
  125. {
  126. base._observer.OnError(error);
  127. base.Dispose();
  128. }
  129. public void OnCompleted()
  130. {
  131. base._observer.OnCompleted();
  132. base.Dispose();
  133. }
  134. }
  135. }
  136. }
  137. }