TakeWhile.cs 4.3 KB

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