Single.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Threading.Tasks;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource> SingleAsync<TSource>(this IAsyncObservable<TSource> source) => Single(source);
  10. public static IAsyncObservable<TSource> SingleAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => Single(source, predicate);
  11. public static IAsyncObservable<TSource> SingleAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Task<bool>> predicate) => Single(source, predicate);
  12. public static IAsyncObservable<TSource> Single<TSource>(this IAsyncObservable<TSource> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Single(observer)));
  17. }
  18. public static IAsyncObservable<TSource> Single<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException(nameof(source));
  22. if (predicate == null)
  23. throw new ArgumentNullException(nameof(predicate));
  24. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Single(observer, predicate)));
  25. }
  26. public static IAsyncObservable<TSource> Single<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Task<bool>> predicate)
  27. {
  28. if (source == null)
  29. throw new ArgumentNullException(nameof(source));
  30. if (predicate == null)
  31. throw new ArgumentNullException(nameof(predicate));
  32. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Single(observer, predicate)));
  33. }
  34. }
  35. partial class AsyncObserver
  36. {
  37. public static IAsyncObserver<TSource> Single<TSource>(IAsyncObserver<TSource> observer)
  38. {
  39. if (observer == null)
  40. throw new ArgumentNullException(nameof(observer));
  41. var hasValue = false;
  42. var value = default(TSource);
  43. return Create<TSource>(
  44. async x =>
  45. {
  46. if (hasValue)
  47. {
  48. await observer.OnErrorAsync(new InvalidOperationException("The sequence contains more than one element.")).ConfigureAwait(false);
  49. return;
  50. }
  51. hasValue = true;
  52. value = x;
  53. },
  54. observer.OnErrorAsync,
  55. async () =>
  56. {
  57. if (!hasValue)
  58. {
  59. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  60. }
  61. else
  62. {
  63. await observer.OnNextAsync(value).ConfigureAwait(false);
  64. await observer.OnCompletedAsync().ConfigureAwait(false);
  65. }
  66. }
  67. );
  68. }
  69. public static IAsyncObserver<TSource> Single<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)
  70. {
  71. if (observer == null)
  72. throw new ArgumentNullException(nameof(observer));
  73. if (predicate == null)
  74. throw new ArgumentNullException(nameof(predicate));
  75. return Where(Single(observer), predicate);
  76. }
  77. public static IAsyncObserver<TSource> Single<TSource>(IAsyncObserver<TSource> observer, Func<TSource, Task<bool>> predicate)
  78. {
  79. if (observer == null)
  80. throw new ArgumentNullException(nameof(observer));
  81. if (predicate == null)
  82. throw new ArgumentNullException(nameof(predicate));
  83. return Where(Single(observer), predicate);
  84. }
  85. }
  86. }