AsyncInfoObservable.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if HAS_WINRT
  3. using System.Reactive.Disposables;
  4. using System.Reactive.Linq;
  5. using System.Reactive.Subjects;
  6. using System.Reactive.Threading.Tasks;
  7. using System.Runtime.InteropServices.WindowsRuntime;
  8. using System.Threading.Tasks;
  9. using Windows.Foundation;
  10. namespace System.Reactive.Linq
  11. {
  12. /// <summary>
  13. /// Provides a set of extension methods to expose observable sequences as Windows Runtime asynchronous actions and operations.
  14. /// </summary>
  15. public static class AsyncInfoObservable
  16. {
  17. #region IAsyncAction
  18. /// <summary>
  19. /// Creates a Windows Runtime asynchronous action that represents the completion of the observable sequence.
  20. /// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
  21. /// </summary>
  22. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  23. /// <param name="source">Source sequence to expose as an asynchronous action.</param>
  24. /// <returns>Windows Runtime asynchronous action object representing the completion of the observable sequence.</returns>
  25. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  26. public static IAsyncAction ToAsyncAction<TSource>(this IObservable<TSource> source)
  27. {
  28. if (source == null)
  29. throw new ArgumentNullException("source");
  30. return AsyncInfo.Run(ct => (Task)source.DefaultIfEmpty().ToTask(ct));
  31. }
  32. #region Progress
  33. /// <summary>
  34. /// Creates a Windows Runtime asynchronous action that represents the completion of the observable sequence, reporting incremental progress for each element produced by the sequence.
  35. /// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
  36. /// </summary>
  37. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  38. /// <param name="source">Source sequence to expose as an asynchronous action.</param>
  39. /// <returns>Windows Runtime asynchronous action object representing the completion of the observable sequence, reporting incremental progress for each source sequence element.</returns>
  40. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  41. public static IAsyncActionWithProgress<int> ToAsyncActionWithProgress<TSource>(this IObservable<TSource> source)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException("source");
  45. return AsyncInfo.Run<int>((ct, progress) =>
  46. {
  47. var i = 0;
  48. return (Task)source.Do(_ => progress.Report(i++)).DefaultIfEmpty().ToTask(ct);
  49. });
  50. }
  51. /// <summary>
  52. /// Creates a Windows Runtime asynchronous action that represents the completion of the observable sequence, using a selector function to map the source sequence on a progress reporting sequence.
  53. /// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
  54. /// </summary>
  55. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  56. /// <typeparam name="TProgress">The type of the elements in the progress sequence.</typeparam>
  57. /// <param name="source">Source sequence to expose as an asynchronous action and to compute a progress sequence that gets reported through the asynchronous action.</param>
  58. /// <param name="progressSelector">Selector function to map the source sequence on a progress reporting sequence.</param>
  59. /// <returns>Windows Runtime asynchronous action object representing the completion of the result sequence, reporting progress computed through the progress sequence.</returns>
  60. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progressSelector"/> is null.</exception>
  61. public static IAsyncActionWithProgress<TProgress> ToAsyncActionWithProgress<TSource, TProgress>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TProgress>> progressSelector)
  62. {
  63. if (source == null)
  64. throw new ArgumentNullException("source");
  65. if (progressSelector == null)
  66. throw new ArgumentNullException("progressSelector");
  67. return AsyncInfo.Run<TProgress>((ct, progress) =>
  68. {
  69. return (Task)Observable.Create<TSource>(observer =>
  70. {
  71. var obs = Observer.Synchronize(observer);
  72. var data = source.Publish();
  73. var progressSubscription = progressSelector(data).Subscribe(progress.Report, obs.OnError);
  74. var dataSubscription = data.DefaultIfEmpty().Subscribe(obs);
  75. var connection = data.Connect();
  76. return StableCompositeDisposable.Create(progressSubscription, dataSubscription, connection);
  77. }).ToTask(ct);
  78. });
  79. }
  80. #endregion
  81. #endregion
  82. #region IAsyncOperation<T>
  83. /// <summary>
  84. /// Creates a Windows Runtime asynchronous operation that returns the last element of the observable sequence.
  85. /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
  86. /// </summary>
  87. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  88. /// <param name="source">Source sequence to expose as an asynchronous operation.</param>
  89. /// <returns>Windows Runtime asynchronous operation object that returns the last element of the observable sequence.</returns>
  90. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  91. public static IAsyncOperation<TSource> ToAsyncOperation<TSource>(this IObservable<TSource> source)
  92. {
  93. if (source == null)
  94. throw new ArgumentNullException("source");
  95. return AsyncInfo.Run(ct => source.ToTask(ct));
  96. }
  97. /// <summary>
  98. /// Creates a Windows Runtime asynchronous operation that returns the last element of the observable sequence, reporting incremental progress for each element produced by the sequence.
  99. /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
  100. /// </summary>
  101. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  102. /// <param name="source">Source sequence to expose as an asynchronous operation.</param>
  103. /// <returns>Windows Runtime asynchronous operation object that returns the last element of the observable sequence, reporting incremental progress for each source sequence element.</returns>
  104. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  105. public static IAsyncOperationWithProgress<TSource, int> ToAsyncOperationWithProgress<TSource>(this IObservable<TSource> source)
  106. {
  107. if (source == null)
  108. throw new ArgumentNullException("source");
  109. return AsyncInfo.Run<TSource, int>((ct, progress) =>
  110. {
  111. var i = 0;
  112. return source.Do(_ => progress.Report(i++)).ToTask(ct);
  113. });
  114. }
  115. #region Progress
  116. /// <summary>
  117. /// Creates a Windows Runtime asynchronous operation that returns the last element of the result sequence, reporting incremental progress for each element produced by the source sequence.
  118. /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
  119. /// </summary>
  120. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  121. /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
  122. /// <param name="source">Source sequence to compute a result sequence that gets exposed as an asynchronous operation.</param>
  123. /// <param name="resultSelector">Selector function to map the source sequence on a result sequence.</param>
  124. /// <returns>Windows Runtime asynchronous operation object that returns the last element of the result sequence, reporting incremental progress for each source sequence element.</returns>
  125. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="resultSelector"/> is null.</exception>
  126. public static IAsyncOperationWithProgress<TResult, int> ToAsyncOperationWithProgress<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> resultSelector)
  127. {
  128. if (source == null)
  129. throw new ArgumentNullException("source");
  130. if (resultSelector == null)
  131. throw new ArgumentNullException("resultSelector");
  132. return AsyncInfo.Run<TResult, int>((ct, progress) =>
  133. {
  134. var i = 0;
  135. return resultSelector(source.Do(_ => progress.Report(i++))).ToTask(ct);
  136. });
  137. }
  138. /// <summary>
  139. /// Creates a Windows Runtime asynchronous operation that returns the last element of the result sequence, using a selector function to map the source sequence on a progress reporting sequence.
  140. /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
  141. /// </summary>
  142. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  143. /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
  144. /// <typeparam name="TProgress">The type of the elements in the progress sequence.</typeparam>
  145. /// <param name="source">Source sequence to compute a result sequence that gets exposed as an asynchronous operation and a progress sequence that gets reported through the asynchronous operation.</param>
  146. /// <param name="resultSelector">Selector function to map the source sequence on a result sequence.</param>
  147. /// <param name="progressSelector">Selector function to map the source sequence on a progress reporting sequence.</param>
  148. /// <returns>Windows Runtime asynchronous operation object that returns the last element of the result sequence, reporting progress computed through the progress sequence.</returns>
  149. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="resultSelector"/> or <paramref name="progressSelector"/> is null.</exception>
  150. public static IAsyncOperationWithProgress<TResult, TProgress> ToAsyncOperationWithProgress<TSource, TResult, TProgress>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> resultSelector, Func<IObservable<TSource>, IObservable<TProgress>> progressSelector)
  151. {
  152. if (source == null)
  153. throw new ArgumentNullException("source");
  154. if (resultSelector == null)
  155. throw new ArgumentNullException("resultSelector");
  156. if (progressSelector == null)
  157. throw new ArgumentNullException("progressSelector");
  158. return AsyncInfo.Run<TResult, TProgress>((ct, progress) =>
  159. {
  160. return Observable.Create<TResult>(observer =>
  161. {
  162. var obs = Observer.Synchronize(observer);
  163. var data = source.Publish();
  164. var progressSubscription = progressSelector(data).Subscribe(progress.Report, obs.OnError);
  165. var dataSubscription = resultSelector(data).Subscribe(obs);
  166. var connection = data.Connect();
  167. return StableCompositeDisposable.Create(progressSubscription, dataSubscription, connection);
  168. }).ToTask(ct);
  169. });
  170. }
  171. #endregion
  172. #endregion
  173. }
  174. }
  175. #endif