SkipWhile.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. namespace UniRx.Operators
  3. {
  4. internal class SkipWhileObservable<T> : OperatorObservableBase<T>
  5. {
  6. readonly IObservable<T> source;
  7. readonly Func<T, bool> predicate;
  8. readonly Func<T, int, bool> predicateWithIndex;
  9. public SkipWhileObservable(IObservable<T> source, Func<T, bool> predicate)
  10. : base(source.IsRequiredSubscribeOnCurrentThread())
  11. {
  12. this.source = source;
  13. this.predicate = predicate;
  14. }
  15. public SkipWhileObservable(IObservable<T> source, Func<T, int, bool> predicateWithIndex)
  16. : base(source.IsRequiredSubscribeOnCurrentThread())
  17. {
  18. this.source = source;
  19. this.predicateWithIndex = predicateWithIndex;
  20. }
  21. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  22. {
  23. if (predicate != null)
  24. {
  25. return new SkipWhile(this, observer, cancel).Run();
  26. }
  27. else
  28. {
  29. return new SkipWhile_(this, observer, cancel).Run();
  30. }
  31. }
  32. class SkipWhile : OperatorObserverBase<T, T>
  33. {
  34. readonly SkipWhileObservable<T> parent;
  35. bool endSkip = false;
  36. public SkipWhile(SkipWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  37. {
  38. this.parent = parent;
  39. }
  40. public IDisposable Run()
  41. {
  42. return parent.source.Subscribe(this);
  43. }
  44. public override void OnNext(T value)
  45. {
  46. if (!endSkip)
  47. {
  48. try
  49. {
  50. endSkip = !parent.predicate(value);
  51. }
  52. catch (Exception ex)
  53. {
  54. try { observer.OnError(ex); } finally { Dispose(); }
  55. return;
  56. }
  57. if (!endSkip) return;
  58. }
  59. observer.OnNext(value);
  60. }
  61. public override void OnError(Exception error)
  62. {
  63. try { observer.OnError(error); } finally { Dispose(); }
  64. }
  65. public override void OnCompleted()
  66. {
  67. try { observer.OnCompleted(); } finally { Dispose(); }
  68. }
  69. }
  70. class SkipWhile_ : OperatorObserverBase<T, T>
  71. {
  72. readonly SkipWhileObservable<T> parent;
  73. bool endSkip = false;
  74. int index = 0;
  75. public SkipWhile_(SkipWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  76. {
  77. this.parent = parent;
  78. }
  79. public IDisposable Run()
  80. {
  81. return parent.source.Subscribe(this);
  82. }
  83. public override void OnNext(T value)
  84. {
  85. if (!endSkip)
  86. {
  87. try
  88. {
  89. endSkip = !parent.predicateWithIndex(value, index++);
  90. }
  91. catch (Exception ex)
  92. {
  93. try { observer.OnError(ex); } finally { Dispose(); }
  94. return;
  95. }
  96. if (!endSkip) return;
  97. }
  98. observer.OnNext(value);
  99. }
  100. public override void OnError(Exception error)
  101. {
  102. try { observer.OnError(error); } finally { Dispose(); }
  103. }
  104. public override void OnCompleted()
  105. {
  106. try { observer.OnCompleted(); } finally { Dispose(); }
  107. }
  108. }
  109. }
  110. }