Observable.Awaiter.cs 4.2 KB

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