Last.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. namespace UniRx.Operators
  3. {
  4. internal class LastObservable<T> : OperatorObservableBase<T>
  5. {
  6. readonly IObservable<T> source;
  7. readonly bool useDefault;
  8. readonly Func<T, bool> predicate;
  9. public LastObservable(IObservable<T> source, bool useDefault)
  10. : base(source.IsRequiredSubscribeOnCurrentThread())
  11. {
  12. this.source = source;
  13. this.useDefault = useDefault;
  14. }
  15. public LastObservable(IObservable<T> source, Func<T, bool> predicate, bool useDefault)
  16. : base(source.IsRequiredSubscribeOnCurrentThread())
  17. {
  18. this.source = source;
  19. this.predicate = predicate;
  20. this.useDefault = useDefault;
  21. }
  22. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  23. {
  24. if (predicate == null)
  25. {
  26. return source.Subscribe(new Last(this, observer, cancel));
  27. }
  28. else
  29. {
  30. return source.Subscribe(new Last_(this, observer, cancel));
  31. }
  32. }
  33. class Last : OperatorObserverBase<T, T>
  34. {
  35. readonly LastObservable<T> parent;
  36. bool notPublished;
  37. T lastValue;
  38. public Last(LastObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  39. {
  40. this.parent = parent;
  41. this.notPublished = true;
  42. }
  43. public override void OnNext(T value)
  44. {
  45. notPublished = false;
  46. lastValue = value;
  47. }
  48. public override void OnError(Exception error)
  49. {
  50. try { observer.OnError(error); }
  51. finally { Dispose(); }
  52. }
  53. public override void OnCompleted()
  54. {
  55. if (parent.useDefault)
  56. {
  57. if (notPublished)
  58. {
  59. observer.OnNext(default(T));
  60. }
  61. else
  62. {
  63. observer.OnNext(lastValue);
  64. }
  65. try { observer.OnCompleted(); }
  66. finally { Dispose(); }
  67. }
  68. else
  69. {
  70. if (notPublished)
  71. {
  72. try { observer.OnError(new InvalidOperationException("sequence is empty")); }
  73. finally { Dispose(); }
  74. }
  75. else
  76. {
  77. observer.OnNext(lastValue);
  78. try { observer.OnCompleted(); }
  79. finally { Dispose(); }
  80. }
  81. }
  82. }
  83. }
  84. class Last_ : OperatorObserverBase<T, T>
  85. {
  86. readonly LastObservable<T> parent;
  87. bool notPublished;
  88. T lastValue;
  89. public Last_(LastObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  90. {
  91. this.parent = parent;
  92. this.notPublished = true;
  93. }
  94. public override void OnNext(T value)
  95. {
  96. bool isPassed;
  97. try
  98. {
  99. isPassed = parent.predicate(value);
  100. }
  101. catch (Exception ex)
  102. {
  103. try { observer.OnError(ex); }
  104. finally { Dispose(); }
  105. return;
  106. }
  107. if (isPassed)
  108. {
  109. notPublished = false;
  110. lastValue = value;
  111. }
  112. }
  113. public override void OnError(Exception error)
  114. {
  115. try { observer.OnError(error); }
  116. finally { Dispose(); }
  117. }
  118. public override void OnCompleted()
  119. {
  120. if (parent.useDefault)
  121. {
  122. if (notPublished)
  123. {
  124. observer.OnNext(default(T));
  125. }
  126. else
  127. {
  128. observer.OnNext(lastValue);
  129. }
  130. try { observer.OnCompleted(); }
  131. finally { Dispose(); }
  132. }
  133. else
  134. {
  135. if (notPublished)
  136. {
  137. try { observer.OnError(new InvalidOperationException("sequence is empty")); }
  138. finally { Dispose(); }
  139. }
  140. else
  141. {
  142. observer.OnNext(lastValue);
  143. try { observer.OnCompleted(); }
  144. finally { Dispose(); }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }