LastOrDefault.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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> LastOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source) => LastOrDefault(source);
  10. public static IAsyncObservable<TSource> LastOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => LastOrDefault(source, predicate);
  11. public static IAsyncObservable<TSource> LastOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => LastOrDefault(source, predicate);
  12. public static IAsyncObservable<TSource> LastOrDefault<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.LastOrDefault(observer)));
  19. }
  20. public static IAsyncObservable<TSource> LastOrDefault<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.LastOrDefault(observer, predicate)));
  30. }
  31. public static IAsyncObservable<TSource> LastOrDefault<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.LastOrDefault(observer, predicate)));
  41. }
  42. }
  43. public partial class AsyncObserver
  44. {
  45. public static IAsyncObserver<TSource> LastOrDefault<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. await observer.OnNextAsync(hasValue ? lastValue : default).ConfigureAwait(false);
  62. await observer.OnCompletedAsync().ConfigureAwait(false);
  63. }
  64. );
  65. }
  66. public static IAsyncObserver<TSource> LastOrDefault<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(LastOrDefault(observer), predicate);
  73. }
  74. public static IAsyncObserver<TSource> LastOrDefault<TSource>(IAsyncObserver<TSource> observer, Func<TSource, ValueTask<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(LastOrDefault(observer), predicate);
  81. }
  82. }
  83. }