Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoExtensions.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.Threading;
8 using Windows.Foundation;
9
10 namespace System.Reactive.Windows.Foundation
11 {
12 /// <summary>
13 /// Provides conversions from Windows Runtime asynchronous actions and operations to observable sequences.
14 /// </summary>
15 public static class AsyncInfoObservableExtensions
16 {
17 #region IAsyncAction and IAsyncActionWithProgress
18
19 /// <summary>
20 /// Converts a Windows Runtime asynchronous action to an observable sequence.
21 /// Each observer subscribed to the resulting observable sequence will be notified about the action's successful or exceptional completion.
22 /// </summary>
23 /// <param name="source">Asynchronous action to convert.</param>
24 /// <returns>An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.</returns>
25 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
26 public static IObservable<Unit> ToObservable(this IAsyncAction source)
27 {
28 if (source == null)
29 throw new ArgumentNullException("source");
30
31 return new AsyncInfoToObservableBridge<Unit, Unit>(
32 source,
33 (iai, a) => ((IAsyncAction)iai).Completed += new AsyncActionCompletedHandler((iaa, status) => a(iaa, status)),
34 iai => Unit.Default,
35 onProgress: null,
36 progress: null,
37 multiValue: false
38 );
39 }
40
41 /// <summary>
42 /// Converts a Windows Runtime asynchronous action to an observable sequence, ignoring its progress notifications.
43 /// Each observer subscribed to the resulting observable sequence will be notified about the action's successful or exceptional completion.
44 /// </summary>
45 /// <typeparam name="TProgress">The type of the reported progress objects, which get ignored by this conversion.</typeparam>
46 /// <param name="source">Asynchronous action to convert.</param>
47 /// <returns>An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.</returns>
48 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
49 public static IObservable<Unit> ToObservable<TProgress>(this IAsyncActionWithProgress<TProgress> source)
50 {
51 if (source == null)
52 throw new ArgumentNullException("source");
53
54 return source.ToObservable_(null);
55 }
56
57 /// <summary>
58 /// Converts a Windows Runtime asynchronous action to an observable sequence, reporting its progress through the supplied progress object.
59 /// Each observer subscribed to the resulting observable sequence will be notified about the action's successful or exceptional completion.
60 /// </summary>
61 /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
62 /// <param name="source">Asynchronous action to convert.</param>
63 /// <param name="progress">Progress object to receive progress notifications on.</param>
64 /// <returns>An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.</returns>
65 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progress"/> is null.</exception>
66 public static IObservable<Unit> ToObservable<TProgress>(this IAsyncActionWithProgress<TProgress> source, IProgress<TProgress> progress)
67 {
68 if (source == null)
69 throw new ArgumentNullException("source");
70 if (progress == null)
71 throw new ArgumentNullException("progress");
72
73 return source.ToObservable_(progress);
74 }
75
76 /// <summary>
77 /// Converts a Windows Runtime asynchronous action to an observable sequence reporting its progress.
78 /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
79 /// </summary>
80 /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
81 /// <param name="source">Asynchronous action to convert.</param>
82 /// <returns>An observable sequence that produces progress values from the asynchronous action and notifies observers about the action's completion.</returns>
83 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
84 public static IObservable<TProgress> ToObservableProgress<TProgress>(this IAsyncActionWithProgress<TProgress> source)
85 {
86 if (source == null)
87 throw new ArgumentNullException("source");
88
89 return Observable.Create<TProgress>(observer =>
90 {
91 var progress = observer.ToProgress();
92 var src = source.ToObservable_(progress);
93 return src.Subscribe(_ => { }, observer.OnError, observer.OnCompleted);
94 });
95 }
96
97 private static IObservable<Unit> ToObservable_<TProgress>(this IAsyncActionWithProgress<TProgress> source, IProgress<TProgress> progress)
98 {
99 return new AsyncInfoToObservableBridge<Unit, TProgress>(
100 source,
101 (iai, a) => ((IAsyncActionWithProgress<TProgress>)iai).Completed += new AsyncActionWithProgressCompletedHandler<TProgress>((iaa, status) => a(iaa, status)),
102 iai => Unit.Default,
103 (iai, a) => ((IAsyncActionWithProgress<TProgress>)iai).Progress += new AsyncActionProgressHandler<TProgress>((iap, p) => a(iap, p)),
104 progress,
105 multiValue: false
106 );
107 }
108
109 #endregion
110
111 #region IAsyncOperation and IAsyncOperationWithProgress
112
113 /// <summary>
114 /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its result.
115 /// Each observer subscribed to the resulting observable sequence will be notified about the operation's single result and its successful exceptional completion.
116 /// </summary>
117 /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
118 /// <param name="source">Asynchronous operation to convert.</param>
119 /// <returns>An observable sequence that notifies observers about the asynchronous operation's result value and completion.</returns>
120 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
121 public static IObservable<TResult> ToObservable<TResult>(this IAsyncOperation<TResult> source)
122 {
123 if (source == null)
124 throw new ArgumentNullException("source");
125
126 return new AsyncInfoToObservableBridge<TResult, Unit>(
127 source,
128 (iai, a) => ((IAsyncOperation<TResult>)iai).Completed += new AsyncOperationCompletedHandler<TResult>((iao, status) => a(iao, status)),
129 iai => ((IAsyncOperation<TResult>)iai).GetResults(),
130 onProgress: null,
131 progress: null,
132 multiValue: false
133 );
134 }
135
136 /// <summary>
137 /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its result but ignoring its progress notifications.
138 /// Each observer subscribed to the resulting observable sequence will be notified about the operations's single result and its successful or exceptional completion.
139 /// </summary>
140 /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
141 /// <typeparam name="TProgress">The type of the reported progress objects, which get ignored by this conversion.</typeparam>
142 /// <param name="source">Asynchronous action to convert.</param>
143 /// <returns>An observable sequence that notifies observers about the asynchronous operation's result value and completion.</returns>
144 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
145 public static IObservable<TResult> ToObservable<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
146 {
147 if (source == null)
148 throw new ArgumentNullException("source");
149
150 return source.ToObservable_(null, false);
151 }
152
153 /// <summary>
154 /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its result and reporting its progress through the supplied progress object.
155 /// Each observer subscribed to the resulting observable sequence will be notified about the operations's single result and its successful or exceptional completion.
156 /// </summary>
157 /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
158 /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
159 /// <param name="source">Asynchronous action to convert.</param>
160 /// <param name="progress">Progress object to receive progress notifications on.</param>
161 /// <returns>An observable sequence that notifies observers about the asynchronous operation's result value and completion.</returns>
162 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progress"/> is null.</exception>
163 public static IObservable<TResult> ToObservable<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress)
164 {
165 if (source == null)
166 throw new ArgumentNullException("source");
167 if (progress == null)
168 throw new ArgumentNullException("progress");
169
170 return source.ToObservable_(progress, false);
171 }
172
173 /// <summary>
174 /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its progress but ignoring its result value.
175 /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
176 /// </summary>
177 /// <typeparam name="TResult">The type of the asynchronous operation's result, which gets ignored by this conversion.</typeparam>
178 /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
179 /// <param name="source">Asynchronous action to convert.</param>
180 /// <returns>An observable sequence that produces progress values from the asynchronous operatin and notifies observers about the operations's completion.</returns>
181 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
182 public static IObservable<TProgress> ToObservableProgress<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
183 {
184 if (source == null)
185 throw new ArgumentNullException("source");
186
187 return Observable.Create<TProgress>(observer =>
188 {
189 var progress = observer.ToProgress();
190 var src = source.ToObservable_(progress, false);
191 return src.Subscribe(_ => { }, observer.OnError, observer.OnCompleted);
192 });
193 }
194
195 /// <summary>
196 /// Converts a Windows Runtime asynchronous operation to an observable sequence by retrieving the operation's results whenever progress is reported and when the operation completes.
197 /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
198 /// </summary>
199 /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
200 /// <typeparam name="TProgress">The type of the reported progress objects, which are used internally in the conversion but aren't exposed.</typeparam>
201 /// <param name="source">Asynchronous operation to convert.</param>
202 /// <returns>An observable sequence that notifies observers about the asynchronous operation's (incremental) result value(s) and completion.</returns>
203 /// <remarks>This conversion can be used with Windows Runtime APIs that support incremental retrieval of results during an asynchronous operation's execution.</remarks>
204 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
205 public static IObservable<TResult> ToObservableMultiple<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
206 {
207 if (source == null)
208 throw new ArgumentNullException("source");
209
210 return source.ToObservable_(null, true);
211 }
212
213 /// <summary>
214 /// Converts a Windows Runtime asynchronous operation to an observable sequence by retrieving the operation's results whenever progress is reported and when the operation completes. The operation's progress is reported through the supplied progress object.
215 /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
216 /// </summary>
217 /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
218 /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
219 /// <param name="source">Asynchronous operation to convert.</param>
220 /// <param name="progress">Progress object to receive progress notifications on.</param>
221 /// <returns>An observable sequence that notifies observers about the asynchronous operation's (incremental) result value(s) and completion.</returns>
222 /// <remarks>This conversion can be used with Windows Runtime APIs that support incremental retrieval of results during an asynchronous operation's execution.</remarks>
223 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progress"/> is null.</exception>
224 public static IObservable<TResult> ToObservableMultiple<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress)
225 {
226 if (source == null)
227 throw new ArgumentNullException("source");
228 if (progress == null)
229 throw new ArgumentNullException("progress");
230
231 return source.ToObservable_(progress, true);
232 }
233
234 private static IObservable<TResult> ToObservable_<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress, bool supportsMultiple)
235 {
236 return new AsyncInfoToObservableBridge<TResult, TProgress>(
237 source,
238 (iai, a) => ((IAsyncOperationWithProgress<TResult, TProgress>)iai).Completed += new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>((iao, status) => a(iao, status)),
239 iai => ((IAsyncOperationWithProgress<TResult, TProgress>)iai).GetResults(),
240 (iai, a) => ((IAsyncOperationWithProgress<TResult, TProgress>)iai).Progress += new AsyncOperationProgressHandler<TResult, TProgress>((iap, p) => a(iap, p)),
241 progress,
242 supportsMultiple
243 );
244 }
245
246 #endregion
247 }
248 }
249 #endif