FirstAsync.cs 3.9 KB

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