FirstAsync.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 FirstAsync<TSource> : Producer<TSource>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly Func<TSource, bool> _predicate;
  12. private readonly bool _throwOnEmpty;
  13. public FirstAsync(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 FirstAsyncImpl(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 FirstAsync<TSource> _parent;
  37. public _(FirstAsync<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  38. : base(observer, cancel)
  39. {
  40. _parent = parent;
  41. }
  42. public void OnNext(TSource value)
  43. {
  44. base._observer.OnNext(value);
  45. base._observer.OnCompleted();
  46. base.Dispose();
  47. }
  48. public void OnError(Exception error)
  49. {
  50. base._observer.OnError(error);
  51. base.Dispose();
  52. }
  53. public void OnCompleted()
  54. {
  55. if (_parent._throwOnEmpty)
  56. {
  57. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  58. }
  59. else
  60. {
  61. base._observer.OnNext(default(TSource));
  62. base._observer.OnCompleted();
  63. }
  64. base.Dispose();
  65. }
  66. }
  67. class FirstAsyncImpl : Sink<TSource>, IObserver<TSource>
  68. {
  69. private readonly FirstAsync<TSource> _parent;
  70. public FirstAsyncImpl(FirstAsync<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  71. : base(observer, cancel)
  72. {
  73. _parent = parent;
  74. }
  75. public void OnNext(TSource value)
  76. {
  77. var b = false;
  78. try
  79. {
  80. b = _parent._predicate(value);
  81. }
  82. catch (Exception ex)
  83. {
  84. base._observer.OnError(ex);
  85. base.Dispose();
  86. return;
  87. }
  88. if (b)
  89. {
  90. base._observer.OnNext(value);
  91. base._observer.OnCompleted();
  92. base.Dispose();
  93. }
  94. }
  95. public void OnError(Exception error)
  96. {
  97. base._observer.OnError(error);
  98. base.Dispose();
  99. }
  100. public void OnCompleted()
  101. {
  102. if (_parent._throwOnEmpty)
  103. {
  104. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_MATCHING_ELEMENTS));
  105. }
  106. else
  107. {
  108. base._observer.OnNext(default(TSource));
  109. base._observer.OnCompleted();
  110. }
  111. base.Dispose();
  112. }
  113. }
  114. }
  115. }
  116. #endif