Observable.Awaiter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #if HAS_AWAIT
  5. using System.Threading;
  6. using System.Reactive.Disposables;
  7. using System.Reactive.Subjects;
  8. namespace System.Reactive.Linq
  9. {
  10. public static partial class Observable
  11. {
  12. /// <summary>
  13. /// Gets an awaiter that returns the last value of the observable sequence or throws an exception if the sequence is empty.
  14. /// This operation subscribes to the observable sequence, making it hot.
  15. /// </summary>
  16. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  17. /// <param name="source">Source sequence to await.</param>
  18. /// <returns>Object that can be awaited.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  20. public static AsyncSubject<TSource> GetAwaiter<TSource>(this IObservable<TSource> source)
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. return s_impl.GetAwaiter<TSource>(source);
  25. }
  26. /// <summary>
  27. /// Gets an awaiter that returns the last value of the observable sequence or throws an exception if the sequence is empty.
  28. /// This operation subscribes and connects to the observable sequence, making it hot.
  29. /// </summary>
  30. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  31. /// <param name="source">Source sequence to await.</param>
  32. /// <returns>Object that can be awaited.</returns>
  33. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  34. public static AsyncSubject<TSource> GetAwaiter<TSource>(this IConnectableObservable<TSource> source)
  35. {
  36. if (source == null)
  37. throw new ArgumentNullException(nameof(source));
  38. return s_impl.GetAwaiter<TSource>(source);
  39. }
  40. /// <summary>
  41. /// Gets an awaiter that returns the last value of the observable sequence or throws an exception if the sequence is empty.
  42. /// This operation subscribes to the observable sequence, making it hot. The supplied CancellationToken can be used to cancel the subscription.
  43. /// </summary>
  44. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  45. /// <param name="source">Source sequence to await.</param>
  46. /// <param name="cancellationToken">Cancellation token.</param>
  47. /// <returns>Object that can be awaited.</returns>
  48. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  49. public static AsyncSubject<TSource> RunAsync<TSource>(this IObservable<TSource> source, CancellationToken cancellationToken)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. return s_impl.RunAsync<TSource>(source, cancellationToken);
  54. }
  55. /// <summary>
  56. /// Gets an awaiter that returns the last value of the observable sequence or throws an exception if the sequence is empty.
  57. /// This operation subscribes and connects to the observable sequence, making it hot. The supplied CancellationToken can be used to cancel the subscription and connection.
  58. /// </summary>
  59. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  60. /// <param name="source">Source sequence to await.</param>
  61. /// <param name="cancellationToken">Cancellation token.</param>
  62. /// <returns>Object that can be awaited.</returns>
  63. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  64. public static AsyncSubject<TSource> RunAsync<TSource>(this IConnectableObservable<TSource> source, CancellationToken cancellationToken)
  65. {
  66. if (source == null)
  67. throw new ArgumentNullException(nameof(source));
  68. return s_impl.RunAsync<TSource>(source, cancellationToken);
  69. }
  70. }
  71. }
  72. #endif