FirstOrDefaultAsync.cs 3.8 KB

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