Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAtsushi Eno <atsushieno@veritas-vos-liberabit.com>2013-01-22 12:25:22 +0400
committerAtsushi Eno <atsushieno@veritas-vos-liberabit.com>2013-01-22 12:25:22 +0400
commitcde9fc6a8fe569203cb991121a35c2a9c7f4c420 (patch)
tree8633a637be4973b221d9c7e9378af5e0a08b5670 /Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation
parent8911e1d3f169a0e378b4e237926269d9218c8fd3 (diff)
import 2b5dbddd740b, new directory structure in the original rx.
Diffstat (limited to 'Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation')
-rw-r--r--Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoExtensions.cs249
-rw-r--r--Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoToObservableBridge.cs101
2 files changed, 350 insertions, 0 deletions
diff --git a/Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoExtensions.cs b/Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoExtensions.cs
new file mode 100644
index 0000000..087cce0
--- /dev/null
+++ b/Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoExtensions.cs
@@ -0,0 +1,249 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+#if HAS_WINRT
+using System.Reactive.Disposables;
+using System.Reactive.Linq;
+using System.Reactive.Subjects;
+using System.Threading;
+using Windows.Foundation;
+
+namespace System.Reactive.Windows.Foundation
+{
+ /// <summary>
+ /// Provides conversions from Windows Runtime asynchronous actions and operations to observable sequences.
+ /// </summary>
+ public static class AsyncInfoObservableExtensions
+ {
+ #region IAsyncAction and IAsyncActionWithProgress
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous action to an observable sequence.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the action's successful or exceptional completion.
+ /// </summary>
+ /// <param name="source">Asynchronous action to convert.</param>
+ /// <returns>An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+ public static IObservable<Unit> ToObservable(this IAsyncAction source)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+
+ return new AsyncInfoToObservableBridge<Unit, Unit>(
+ source,
+ (iai, a) => ((IAsyncAction)iai).Completed += new AsyncActionCompletedHandler((iaa, status) => a(iaa, status)),
+ iai => Unit.Default,
+ onProgress: null,
+ progress: null,
+ multiValue: false
+ );
+ }
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous action to an observable sequence, ignoring its progress notifications.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the action's successful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TProgress">The type of the reported progress objects, which get ignored by this conversion.</typeparam>
+ /// <param name="source">Asynchronous action to convert.</param>
+ /// <returns>An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+ public static IObservable<Unit> ToObservable<TProgress>(this IAsyncActionWithProgress<TProgress> source)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+
+ return source.ToObservable_(null);
+ }
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous action to an observable sequence, reporting its progress through the supplied progress object.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the action's successful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
+ /// <param name="source">Asynchronous action to convert.</param>
+ /// <param name="progress">Progress object to receive progress notifications on.</param>
+ /// <returns>An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progress"/> is null.</exception>
+ public static IObservable<Unit> ToObservable<TProgress>(this IAsyncActionWithProgress<TProgress> source, IProgress<TProgress> progress)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+ if (progress == null)
+ throw new ArgumentNullException("progress");
+
+ return source.ToObservable_(progress);
+ }
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous action to an observable sequence reporting its progress.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
+ /// <param name="source">Asynchronous action to convert.</param>
+ /// <returns>An observable sequence that produces progress values from the asynchronous action and notifies observers about the action's completion.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+ public static IObservable<TProgress> ToObservableProgress<TProgress>(this IAsyncActionWithProgress<TProgress> source)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+
+ return Observable.Create<TProgress>(observer =>
+ {
+ var progress = observer.ToProgress();
+ var src = source.ToObservable_(progress);
+ return src.Subscribe(_ => { }, observer.OnError, observer.OnCompleted);
+ });
+ }
+
+ private static IObservable<Unit> ToObservable_<TProgress>(this IAsyncActionWithProgress<TProgress> source, IProgress<TProgress> progress)
+ {
+ return new AsyncInfoToObservableBridge<Unit, TProgress>(
+ source,
+ (iai, a) => ((IAsyncActionWithProgress<TProgress>)iai).Completed += new AsyncActionWithProgressCompletedHandler<TProgress>((iaa, status) => a(iaa, status)),
+ iai => Unit.Default,
+ (iai, a) => ((IAsyncActionWithProgress<TProgress>)iai).Progress += new AsyncActionProgressHandler<TProgress>((iap, p) => a(iap, p)),
+ progress,
+ multiValue: false
+ );
+ }
+
+ #endregion
+
+ #region IAsyncOperation and IAsyncOperationWithProgress
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its result.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the operation's single result and its successful exceptional completion.
+ /// </summary>
+ /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
+ /// <param name="source">Asynchronous operation to convert.</param>
+ /// <returns>An observable sequence that notifies observers about the asynchronous operation's result value and completion.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+ public static IObservable<TResult> ToObservable<TResult>(this IAsyncOperation<TResult> source)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+
+ return new AsyncInfoToObservableBridge<TResult, Unit>(
+ source,
+ (iai, a) => ((IAsyncOperation<TResult>)iai).Completed += new AsyncOperationCompletedHandler<TResult>((iao, status) => a(iao, status)),
+ iai => ((IAsyncOperation<TResult>)iai).GetResults(),
+ onProgress: null,
+ progress: null,
+ multiValue: false
+ );
+ }
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its result but ignoring its progress notifications.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the operations's single result and its successful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
+ /// <typeparam name="TProgress">The type of the reported progress objects, which get ignored by this conversion.</typeparam>
+ /// <param name="source">Asynchronous action to convert.</param>
+ /// <returns>An observable sequence that notifies observers about the asynchronous operation's result value and completion.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+ public static IObservable<TResult> ToObservable<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+
+ return source.ToObservable_(null, false);
+ }
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its result and reporting its progress through the supplied progress object.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the operations's single result and its successful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
+ /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
+ /// <param name="source">Asynchronous action to convert.</param>
+ /// <param name="progress">Progress object to receive progress notifications on.</param>
+ /// <returns>An observable sequence that notifies observers about the asynchronous operation's result value and completion.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progress"/> is null.</exception>
+ public static IObservable<TResult> ToObservable<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+ if (progress == null)
+ throw new ArgumentNullException("progress");
+
+ return source.ToObservable_(progress, false);
+ }
+
+ /// <summary>
+ /// Converts a Windows Runtime asynchronous operation to an observable sequence reporting its progress but ignoring its result value.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TResult">The type of the asynchronous operation's result, which gets ignored by this conversion.</typeparam>
+ /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
+ /// <param name="source">Asynchronous action to convert.</param>
+ /// <returns>An observable sequence that produces progress values from the asynchronous operatin and notifies observers about the operations's completion.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+ public static IObservable<TProgress> ToObservableProgress<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+
+ return Observable.Create<TProgress>(observer =>
+ {
+ var progress = observer.ToProgress();
+ var src = source.ToObservable_(progress, false);
+ return src.Subscribe(_ => { }, observer.OnError, observer.OnCompleted);
+ });
+ }
+
+ /// <summary>
+ /// 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.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
+ /// <typeparam name="TProgress">The type of the reported progress objects, which are used internally in the conversion but aren't exposed.</typeparam>
+ /// <param name="source">Asynchronous operation to convert.</param>
+ /// <returns>An observable sequence that notifies observers about the asynchronous operation's (incremental) result value(s) and completion.</returns>
+ /// <remarks>This conversion can be used with Windows Runtime APIs that support incremental retrieval of results during an asynchronous operation's execution.</remarks>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+ public static IObservable<TResult> ToObservableMultiple<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+
+ return source.ToObservable_(null, true);
+ }
+
+ /// <summary>
+ /// 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.
+ /// Each observer subscribed to the resulting observable sequence will be notified about the action's succesful or exceptional completion.
+ /// </summary>
+ /// <typeparam name="TResult">The type of the asynchronous operation's result.</typeparam>
+ /// <typeparam name="TProgress">The type of the reported progress objects.</typeparam>
+ /// <param name="source">Asynchronous operation to convert.</param>
+ /// <param name="progress">Progress object to receive progress notifications on.</param>
+ /// <returns>An observable sequence that notifies observers about the asynchronous operation's (incremental) result value(s) and completion.</returns>
+ /// <remarks>This conversion can be used with Windows Runtime APIs that support incremental retrieval of results during an asynchronous operation's execution.</remarks>
+ /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progress"/> is null.</exception>
+ public static IObservable<TResult> ToObservableMultiple<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress)
+ {
+ if (source == null)
+ throw new ArgumentNullException("source");
+ if (progress == null)
+ throw new ArgumentNullException("progress");
+
+ return source.ToObservable_(progress, true);
+ }
+
+ private static IObservable<TResult> ToObservable_<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress, bool supportsMultiple)
+ {
+ return new AsyncInfoToObservableBridge<TResult, TProgress>(
+ source,
+ (iai, a) => ((IAsyncOperationWithProgress<TResult, TProgress>)iai).Completed += new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>((iao, status) => a(iao, status)),
+ iai => ((IAsyncOperationWithProgress<TResult, TProgress>)iai).GetResults(),
+ (iai, a) => ((IAsyncOperationWithProgress<TResult, TProgress>)iai).Progress += new AsyncOperationProgressHandler<TResult, TProgress>((iap, p) => a(iap, p)),
+ progress,
+ supportsMultiple
+ );
+ }
+
+ #endregion
+ }
+}
+#endif \ No newline at end of file
diff --git a/Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoToObservableBridge.cs b/Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoToObservableBridge.cs
new file mode 100644
index 0000000..3475817
--- /dev/null
+++ b/Rx/NET/Source/System.Reactive.WindowsRuntime/Foundation/AsyncInfoToObservableBridge.cs
@@ -0,0 +1,101 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+#if HAS_WINRT
+using System.Reactive.Disposables;
+using System.Reactive.Subjects;
+using Windows.Foundation;
+
+namespace System.Reactive.Windows.Foundation
+{
+ class AsyncInfoToObservableBridge<TResult, TProgress> : ObservableBase<TResult>
+ {
+ private readonly Action<IAsyncInfo, Action<IAsyncInfo, AsyncStatus>> _onCompleted;
+ private readonly Func<IAsyncInfo, TResult> _getResult;
+ private readonly AsyncSubject<TResult> _subject;
+
+ public AsyncInfoToObservableBridge(IAsyncInfo info, Action<IAsyncInfo, Action<IAsyncInfo, AsyncStatus>> onCompleted, Func<IAsyncInfo, TResult> getResult, Action<IAsyncInfo, Action<IAsyncInfo, TProgress>> onProgress, IProgress<TProgress> progress, bool multiValue)
+ {
+ _onCompleted = onCompleted;
+ _getResult = getResult;
+
+ _subject = new AsyncSubject<TResult>();
+
+ if (onProgress != null)
+ {
+ onProgress(info, (iai, p) =>
+ {
+ if (multiValue && getResult != null)
+ _subject.OnNext(getResult(iai));
+
+ if (progress != null)
+ progress.Report(p);
+ });
+ }
+
+ Done(info, info.Status, true);
+ }
+
+ private void Done(IAsyncInfo info, AsyncStatus status, bool initial)
+ {
+ var error = default(Exception);
+ var result = default(TResult);
+
+ //
+ // Initial interactions with the IAsyncInfo object. Those could fail, which indicates
+ // a rogue implementation. Failure is just propagated out.
+ //
+ switch (status)
+ {
+ case AsyncStatus.Error:
+ error = info.ErrorCode;
+ if (error == null)
+ throw new InvalidOperationException("The asynchronous operation failed with a null error code.");
+ break;
+ case AsyncStatus.Canceled:
+ error = new OperationCanceledException();
+ break;
+ case AsyncStatus.Completed:
+ if (_getResult != null)
+ result = _getResult(info);
+ break;
+ default:
+ if (!initial)
+ throw new InvalidOperationException("The asynchronous operation completed unexpectedly.");
+
+ _onCompleted(info, (iai, s) => Done(iai, s, false));
+ return;
+ }
+
+ //
+ // Close as early as possible, before running continuations which could fail. In case of
+ // failure above, we don't close out the object in order to allow for debugging of the
+ // rogue implementation without losing state prematurely. Notice _getResults is merely
+ // an indirect call to the appropriate GetResults method, which is not supposed to throw.
+ // Instead, an Error status should be returned.
+ //
+ info.Close();
+
+ //
+ // Now we run the continuations, which could take a long time. Failure here is catastrophic
+ // and under control of the upstream subscriber.
+ //
+ if (error != null)
+ {
+ _subject.OnError(error);
+ }
+ else
+ {
+ if (_getResult != null)
+ _subject.OnNext(result);
+
+ _subject.OnCompleted();
+ }
+ }
+
+ protected override IDisposable SubscribeCore(IObserver<TResult> observer)
+ {
+ return _subject.Subscribe(observer);
+ }
+ }
+}
+#endif \ No newline at end of file