Last.cs 3.4 KB

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