First.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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> FirstAsync<TSource>(this IAsyncObservable<TSource> source) => First(source);
  10. public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => First(source, predicate);
  11. public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Task<bool>> predicate) => First(source, predicate);
  12. public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.First(observer)));
  17. }
  18. public static IAsyncObservable<TSource> First<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.First(observer, predicate)));
  25. }
  26. public static IAsyncObservable<TSource> First<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.First(observer, predicate)));
  33. }
  34. }
  35. partial class AsyncObserver
  36. {
  37. public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer)
  38. {
  39. if (observer == null)
  40. throw new ArgumentNullException(nameof(observer));
  41. return Create<TSource>(
  42. async x =>
  43. {
  44. await observer.OnNextAsync(x).ConfigureAwait(false);
  45. await observer.OnCompletedAsync().ConfigureAwait(false);
  46. },
  47. observer.OnErrorAsync,
  48. async () =>
  49. {
  50. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  51. }
  52. );
  53. }
  54. public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)
  55. {
  56. if (observer == null)
  57. throw new ArgumentNullException(nameof(observer));
  58. if (predicate == null)
  59. throw new ArgumentNullException(nameof(predicate));
  60. return Where(First(observer), predicate);
  61. }
  62. public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer, Func<TSource, Task<bool>> predicate)
  63. {
  64. if (observer == null)
  65. throw new ArgumentNullException(nameof(observer));
  66. if (predicate == null)
  67. throw new ArgumentNullException(nameof(predicate));
  68. return Where(First(observer), predicate);
  69. }
  70. }
  71. }