Observable.Awaiter.cs 4.2 KB

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