SkipWhile.cs 4.0 KB

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