SkipWhile.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 SkipWhile<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 SkipWhile(IObservable<TSource> source, Func<TSource, bool> predicate)
  14. {
  15. _source = source;
  16. _predicate = predicate;
  17. }
  18. public SkipWhile(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 SkipWhileImpl(this, observer, cancel);
  34. setSink(sink);
  35. return _source.SubscribeSafe(sink);
  36. }
  37. }
  38. class _ : Sink<TSource>, IObserver<TSource>
  39. {
  40. private readonly SkipWhile<TSource> _parent;
  41. private bool _running;
  42. public _(SkipWhile<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  43. : base(observer, cancel)
  44. {
  45. _parent = parent;
  46. _running = false;
  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. }
  63. if (_running)
  64. {
  65. base._observer.OnNext(value);
  66. }
  67. }
  68. public void OnError(Exception error)
  69. {
  70. base._observer.OnError(error);
  71. base.Dispose();
  72. }
  73. public void OnCompleted()
  74. {
  75. base._observer.OnCompleted();
  76. base.Dispose();
  77. }
  78. }
  79. class SkipWhileImpl : Sink<TSource>, IObserver<TSource>
  80. {
  81. private readonly SkipWhile<TSource> _parent;
  82. private bool _running;
  83. private int _index;
  84. public SkipWhileImpl(SkipWhile<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  85. : base(observer, cancel)
  86. {
  87. _parent = parent;
  88. _running = false;
  89. _index = 0;
  90. }
  91. public void OnNext(TSource value)
  92. {
  93. if (!_running)
  94. {
  95. try
  96. {
  97. _running = !_parent._predicateI(value, checked(_index++));
  98. }
  99. catch (Exception exception)
  100. {
  101. base._observer.OnError(exception);
  102. base.Dispose();
  103. return;
  104. }
  105. }
  106. if (_running)
  107. {
  108. base._observer.OnNext(value);
  109. }
  110. }
  111. public void OnError(Exception error)
  112. {
  113. base._observer.OnError(error);
  114. base.Dispose();
  115. }
  116. public void OnCompleted()
  117. {
  118. base._observer.OnCompleted();
  119. base.Dispose();
  120. }
  121. }
  122. }
  123. }
  124. #endif