// 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
{
///
/// Provides conversions from Windows Runtime asynchronous actions and operations to observable sequences.
///
public static class AsyncInfoObservableExtensions
{
#region IAsyncAction and IAsyncActionWithProgress
///
/// 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.
///
/// Asynchronous action to convert.
/// An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.
/// is null.
public static IObservable ToObservable(this IAsyncAction source)
{
if (source == null)
throw new ArgumentNullException("source");
return new AsyncInfoToObservableBridge(
source,
(iai, a) => ((IAsyncAction)iai).Completed += new AsyncActionCompletedHandler((iaa, status) => a(iaa, status)),
iai => Unit.Default,
onProgress: null,
progress: null,
multiValue: false
);
}
///
/// 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.
///
/// The type of the reported progress objects, which get ignored by this conversion.
/// Asynchronous action to convert.
/// An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.
/// is null.
public static IObservable ToObservable(this IAsyncActionWithProgress source)
{
if (source == null)
throw new ArgumentNullException("source");
return source.ToObservable_(null);
}
///
/// 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.
///
/// The type of the reported progress objects.
/// Asynchronous action to convert.
/// Progress object to receive progress notifications on.
/// An observable sequence that produces a unit value when the asynchronous action completes, or propagates the exception produced by the asynchronous action.
/// or is null.
public static IObservable ToObservable(this IAsyncActionWithProgress source, IProgress progress)
{
if (source == null)
throw new ArgumentNullException("source");
if (progress == null)
throw new ArgumentNullException("progress");
return source.ToObservable_(progress);
}
///
/// 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.
///
/// The type of the reported progress objects.
/// Asynchronous action to convert.
/// An observable sequence that produces progress values from the asynchronous action and notifies observers about the action's completion.
/// is null.
public static IObservable ToObservableProgress(this IAsyncActionWithProgress source)
{
if (source == null)
throw new ArgumentNullException("source");
return Observable.Create(observer =>
{
var progress = observer.ToProgress();
var src = source.ToObservable_(progress);
return src.Subscribe(_ => { }, observer.OnError, observer.OnCompleted);
});
}
private static IObservable ToObservable_(this IAsyncActionWithProgress source, IProgress progress)
{
return new AsyncInfoToObservableBridge(
source,
(iai, a) => ((IAsyncActionWithProgress)iai).Completed += new AsyncActionWithProgressCompletedHandler((iaa, status) => a(iaa, status)),
iai => Unit.Default,
(iai, a) => ((IAsyncActionWithProgress)iai).Progress += new AsyncActionProgressHandler((iap, p) => a(iap, p)),
progress,
multiValue: false
);
}
#endregion
#region IAsyncOperation and IAsyncOperationWithProgress
///
/// 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.
///
/// The type of the asynchronous operation's result.
/// Asynchronous operation to convert.
/// An observable sequence that notifies observers about the asynchronous operation's result value and completion.
/// is null.
public static IObservable ToObservable(this IAsyncOperation source)
{
if (source == null)
throw new ArgumentNullException("source");
return new AsyncInfoToObservableBridge(
source,
(iai, a) => ((IAsyncOperation)iai).Completed += new AsyncOperationCompletedHandler((iao, status) => a(iao, status)),
iai => ((IAsyncOperation)iai).GetResults(),
onProgress: null,
progress: null,
multiValue: false
);
}
///
/// 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.
///
/// The type of the asynchronous operation's result.
/// The type of the reported progress objects, which get ignored by this conversion.
/// Asynchronous action to convert.
/// An observable sequence that notifies observers about the asynchronous operation's result value and completion.
/// is null.
public static IObservable ToObservable(this IAsyncOperationWithProgress source)
{
if (source == null)
throw new ArgumentNullException("source");
return source.ToObservable_(null, false);
}
///
/// 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.
///
/// The type of the asynchronous operation's result.
/// The type of the reported progress objects.
/// Asynchronous action to convert.
/// Progress object to receive progress notifications on.
/// An observable sequence that notifies observers about the asynchronous operation's result value and completion.
/// or is null.
public static IObservable ToObservable(this IAsyncOperationWithProgress source, IProgress progress)
{
if (source == null)
throw new ArgumentNullException("source");
if (progress == null)
throw new ArgumentNullException("progress");
return source.ToObservable_(progress, false);
}
///
/// 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.
///
/// The type of the asynchronous operation's result, which gets ignored by this conversion.
/// The type of the reported progress objects.
/// Asynchronous action to convert.
/// An observable sequence that produces progress values from the asynchronous operatin and notifies observers about the operations's completion.
/// is null.
public static IObservable ToObservableProgress(this IAsyncOperationWithProgress source)
{
if (source == null)
throw new ArgumentNullException("source");
return Observable.Create(observer =>
{
var progress = observer.ToProgress();
var src = source.ToObservable_(progress, false);
return src.Subscribe(_ => { }, observer.OnError, observer.OnCompleted);
});
}
///
/// 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.
///
/// The type of the asynchronous operation's result.
/// The type of the reported progress objects, which are used internally in the conversion but aren't exposed.
/// Asynchronous operation to convert.
/// An observable sequence that notifies observers about the asynchronous operation's (incremental) result value(s) and completion.
/// This conversion can be used with Windows Runtime APIs that support incremental retrieval of results during an asynchronous operation's execution.
/// is null.
public static IObservable ToObservableMultiple(this IAsyncOperationWithProgress source)
{
if (source == null)
throw new ArgumentNullException("source");
return source.ToObservable_(null, true);
}
///
/// 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.
///
/// The type of the asynchronous operation's result.
/// The type of the reported progress objects.
/// Asynchronous operation to convert.
/// Progress object to receive progress notifications on.
/// An observable sequence that notifies observers about the asynchronous operation's (incremental) result value(s) and completion.
/// This conversion can be used with Windows Runtime APIs that support incremental retrieval of results during an asynchronous operation's execution.
/// or is null.
public static IObservable ToObservableMultiple(this IAsyncOperationWithProgress source, IProgress progress)
{
if (source == null)
throw new ArgumentNullException("source");
if (progress == null)
throw new ArgumentNullException("progress");
return source.ToObservable_(progress, true);
}
private static IObservable ToObservable_(this IAsyncOperationWithProgress source, IProgress progress, bool supportsMultiple)
{
return new AsyncInfoToObservableBridge(
source,
(iai, a) => ((IAsyncOperationWithProgress)iai).Completed += new AsyncOperationWithProgressCompletedHandler((iao, status) => a(iao, status)),
iai => ((IAsyncOperationWithProgress)iai).GetResults(),
(iai, a) => ((IAsyncOperationWithProgress)iai).Progress += new AsyncOperationProgressHandler((iap, p) => a(iap, p)),
progress,
supportsMultiple
);
}
#endregion
}
}
#endif