1
0

LastAsync.cs 4.1 KB

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