LastOrDefault.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Create(
  27. source,
  28. predicate,
  29. default(TSource),
  30. (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LastOrDefault(observer, predicate)));
  31. }
  32. public static IAsyncObservable<TSource> LastOrDefault<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (predicate == null)
  37. throw new ArgumentNullException(nameof(predicate));
  38. return Create(
  39. source,
  40. predicate,
  41. default(TSource),
  42. (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LastOrDefault(observer, predicate)));
  43. }
  44. }
  45. public partial class AsyncObserver
  46. {
  47. public static IAsyncObserver<TSource> LastOrDefault<TSource>(IAsyncObserver<TSource> observer)
  48. {
  49. if (observer == null)
  50. throw new ArgumentNullException(nameof(observer));
  51. var hasValue = false;
  52. var lastValue = default(TSource);
  53. return Create<TSource>(
  54. x =>
  55. {
  56. hasValue = true;
  57. lastValue = x;
  58. return default;
  59. },
  60. observer.OnErrorAsync,
  61. async () =>
  62. {
  63. await observer.OnNextAsync(hasValue ? lastValue : default).ConfigureAwait(false);
  64. await observer.OnCompletedAsync().ConfigureAwait(false);
  65. }
  66. );
  67. }
  68. public static IAsyncObserver<TSource> LastOrDefault<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)
  69. {
  70. if (observer == null)
  71. throw new ArgumentNullException(nameof(observer));
  72. if (predicate == null)
  73. throw new ArgumentNullException(nameof(predicate));
  74. return Where(LastOrDefault(observer), predicate);
  75. }
  76. public static IAsyncObserver<TSource> LastOrDefault<TSource>(IAsyncObserver<TSource> observer, Func<TSource, ValueTask<bool>> predicate)
  77. {
  78. if (observer == null)
  79. throw new ArgumentNullException(nameof(observer));
  80. if (predicate == null)
  81. throw new ArgumentNullException(nameof(predicate));
  82. return Where(LastOrDefault(observer), predicate);
  83. }
  84. }
  85. }