1
0

SingleAsync.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 SingleAsync<TSource> : Producer<TSource>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly Func<TSource, bool> _predicate;
  12. private readonly bool _throwOnEmpty;
  13. public SingleAsync(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 SingleAsyncImpl(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 SingleAsync<TSource> _parent;
  37. private TSource _value;
  38. private bool _seenValue;
  39. public _(SingleAsync<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. if (_seenValue)
  49. {
  50. base._observer.OnError(new InvalidOperationException(Strings_Linq.MORE_THAN_ONE_ELEMENT));
  51. base.Dispose();
  52. return;
  53. }
  54. _value = value;
  55. _seenValue = true;
  56. }
  57. public void OnError(Exception error)
  58. {
  59. base._observer.OnError(error);
  60. base.Dispose();
  61. }
  62. public void OnCompleted()
  63. {
  64. if (!_seenValue && _parent._throwOnEmpty)
  65. {
  66. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  67. }
  68. else
  69. {
  70. base._observer.OnNext(_value);
  71. base._observer.OnCompleted();
  72. }
  73. base.Dispose();
  74. }
  75. }
  76. class SingleAsyncImpl : Sink<TSource>, IObserver<TSource>
  77. {
  78. private readonly SingleAsync<TSource> _parent;
  79. private TSource _value;
  80. private bool _seenValue;
  81. public SingleAsyncImpl(SingleAsync<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  82. : base(observer, cancel)
  83. {
  84. _parent = parent;
  85. _value = default(TSource);
  86. _seenValue = false;
  87. }
  88. public void OnNext(TSource value)
  89. {
  90. var b = false;
  91. try
  92. {
  93. b = _parent._predicate(value);
  94. }
  95. catch (Exception ex)
  96. {
  97. base._observer.OnError(ex);
  98. base.Dispose();
  99. return;
  100. }
  101. if (b)
  102. {
  103. if (_seenValue)
  104. {
  105. base._observer.OnError(new InvalidOperationException(Strings_Linq.MORE_THAN_ONE_MATCHING_ELEMENT));
  106. base.Dispose();
  107. return;
  108. }
  109. _value = value;
  110. _seenValue = true;
  111. }
  112. }
  113. public void OnError(Exception error)
  114. {
  115. base._observer.OnError(error);
  116. base.Dispose();
  117. }
  118. public void OnCompleted()
  119. {
  120. if (!_seenValue && _parent._throwOnEmpty)
  121. {
  122. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_MATCHING_ELEMENTS));
  123. }
  124. else
  125. {
  126. base._observer.OnNext(_value);
  127. base._observer.OnCompleted();
  128. }
  129. base.Dispose();
  130. }
  131. }
  132. }
  133. }
  134. #endif