Observable.Awaiter.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if HAS_AWAIT
  3. using System.Threading;
  4. using System.Reactive.Disposables;
  5. using System.Reactive.Subjects;
  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. throw new ArgumentNullException("source");
  22. return s_impl.GetAwaiter<TSource>(source);
  23. }
  24. /// <summary>
  25. /// Gets an awaiter that returns the last value of the observable sequence or throws an exception if the sequence is empty.
  26. /// This operation subscribes and connects to the observable sequence, making it hot.
  27. /// </summary>
  28. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  29. /// <param name="source">Source sequence to await.</param>
  30. /// <returns>Object that can be awaited.</returns>
  31. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  32. public static AsyncSubject<TSource> GetAwaiter<TSource>(this IConnectableObservable<TSource> source)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException("source");
  36. return s_impl.GetAwaiter<TSource>(source);
  37. }
  38. /// <summary>
  39. /// Gets an awaiter that returns the last value of the observable sequence or throws an exception if the sequence is empty.
  40. /// This operation subscribes to the observable sequence, making it hot. The supplied CancellationToken can be used to cancel the subscription.
  41. /// </summary>
  42. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  43. /// <param name="source">Source sequence to await.</param>
  44. /// <param name="cancellationToken">Cancellation token.</param>
  45. /// <returns>Object that can be awaited.</returns>
  46. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  47. public static AsyncSubject<TSource> RunAsync<TSource>(this IObservable<TSource> source, CancellationToken cancellationToken)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException("source");
  51. return s_impl.RunAsync<TSource>(source, cancellationToken);
  52. }
  53. /// <summary>
  54. /// Gets an awaiter that returns the last value of the observable sequence or throws an exception if the sequence is empty.
  55. /// This operation subscribes and connects to the observable sequence, making it hot. The supplied CancellationToken can be used to cancel the subscription and connection.
  56. /// </summary>
  57. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  58. /// <param name="source">Source sequence to await.</param>
  59. /// <param name="cancellationToken">Cancellation token.</param>
  60. /// <returns>Object that can be awaited.</returns>
  61. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  62. public static AsyncSubject<TSource> RunAsync<TSource>(this IConnectableObservable<TSource> source, CancellationToken cancellationToken)
  63. {
  64. if (source == null)
  65. throw new ArgumentNullException("source");
  66. return s_impl.RunAsync<TSource>(source, cancellationToken);
  67. }
  68. }
  69. }
  70. #endif