Single.cs 3.7 KB

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