Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.WindowsRuntime/Linq/AsyncInfoObservable.cs

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