TakeWhile.cs 4.4 KB

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