Last.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. public partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source) => Last(source);
  10. public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => Last(source, predicate);
  11. public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => Last(source, predicate);
  12. public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return Create(
  17. source,
  18. (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer)));
  19. }
  20. public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. if (predicate == null)
  25. throw new ArgumentNullException(nameof(predicate));
  26. return CreateAsyncObservable<TSource>.From(
  27. source,
  28. predicate,
  29. static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer, predicate)));
  30. }
  31. public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (predicate == null)
  36. throw new ArgumentNullException(nameof(predicate));
  37. return CreateAsyncObservable<TSource>.From(
  38. source,
  39. predicate,
  40. static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer, predicate)));
  41. }
  42. }
  43. public partial class AsyncObserver
  44. {
  45. public static IAsyncObserver<TSource> Last<TSource>(IAsyncObserver<TSource> observer)
  46. {
  47. if (observer == null)
  48. throw new ArgumentNullException(nameof(observer));
  49. var hasValue = false;
  50. var lastValue = default(TSource);
  51. return Create<TSource>(
  52. x =>
  53. {
  54. hasValue = true;
  55. lastValue = x;
  56. return default;
  57. },
  58. observer.OnErrorAsync,
  59. async () =>
  60. {
  61. if (!hasValue)
  62. {
  63. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  64. }
  65. else
  66. {
  67. await observer.OnNextAsync(lastValue).ConfigureAwait(false);
  68. await observer.OnCompletedAsync().ConfigureAwait(false);
  69. }
  70. }
  71. );
  72. }
  73. public static IAsyncObserver<TSource> Last<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)
  74. {
  75. if (observer == null)
  76. throw new ArgumentNullException(nameof(observer));
  77. if (predicate == null)
  78. throw new ArgumentNullException(nameof(predicate));
  79. return Where(Last(observer), predicate);
  80. }
  81. public static IAsyncObserver<TSource> Last<TSource>(IAsyncObserver<TSource> observer, Func<TSource, ValueTask<bool>> predicate)
  82. {
  83. if (observer == null)
  84. throw new ArgumentNullException(nameof(observer));
  85. if (predicate == null)
  86. throw new ArgumentNullException(nameof(predicate));
  87. return Where(Last(observer), predicate);
  88. }
  89. }
  90. }