First.cs 3.1 KB

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