SkipWhile.cs 4.0 KB

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