Last.cs 3.9 KB

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