Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,204 @@
// 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.Reactive.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
namespace System.Reactive.Linq
{
/// <summary>
/// Provides a set of extension methods to expose observable sequences as Windows Runtime asynchronous actions and operations.
/// </summary>
public static class AsyncInfoObservable
{
#region IAsyncAction
/// <summary>
/// Creates a Windows Runtime asynchronous action that represents the completion of the observable sequence.
/// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source sequence to expose as an asynchronous action.</param>
/// <returns>Windows Runtime asynchronous action object representing the completion of the observable sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IAsyncAction ToAsyncAction<TSource>(this IObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException("source");
return AsyncInfo.Run(ct => (Task)source.DefaultIfEmpty().ToTask(ct));
}
#region Progress
/// <summary>
/// Creates a Windows Runtime asynchronous action that represents the completion of the observable sequence, reporting incremental progress for each element produced by the sequence.
/// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source sequence to expose as an asynchronous action.</param>
/// <returns>Windows Runtime asynchronous action object representing the completion of the observable sequence, reporting incremental progress for each source sequence element.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IAsyncActionWithProgress<int> ToAsyncActionWithProgress<TSource>(this IObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException("source");
return AsyncInfo.Run<int>((ct, progress) =>
{
var i = 0;
return (Task)source.Do(_ => progress.Report(i++)).DefaultIfEmpty().ToTask(ct);
});
}
/// <summary>
/// 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.
/// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TProgress">The type of the elements in the progress sequence.</typeparam>
/// <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>
/// <param name="progressSelector">Selector function to map the source sequence on a progress reporting sequence.</param>
/// <returns>Windows Runtime asynchronous action object representing the completion of the result sequence, reporting progress computed through the progress sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="progressSelector"/> is null.</exception>
public static IAsyncActionWithProgress<TProgress> ToAsyncActionWithProgress<TSource, TProgress>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TProgress>> progressSelector)
{
if (source == null)
throw new ArgumentNullException("source");
if (progressSelector == null)
throw new ArgumentNullException("progressSelector");
return AsyncInfo.Run<TProgress>((ct, progress) =>
{
return (Task)Observable.Create<TSource>(observer =>
{
var obs = Observer.Synchronize(observer);
var data = source.Publish();
var progressSubscription = progressSelector(data).Subscribe(progress.Report, obs.OnError);
var dataSubscription = data.DefaultIfEmpty().Subscribe(obs);
var connection = data.Connect();
return new CompositeDisposable(progressSubscription, dataSubscription, connection);
}).ToTask(ct);
});
}
#endregion
#endregion
#region IAsyncOperation<T>
/// <summary>
/// Creates a Windows Runtime asynchronous operation that returns the last element of the observable sequence.
/// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source sequence to expose as an asynchronous operation.</param>
/// <returns>Windows Runtime asynchronous operation object that returns the last element of the observable sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IAsyncOperation<TSource> ToAsyncOperation<TSource>(this IObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException("source");
return AsyncInfo.Run(ct => source.ToTask(ct));
}
/// <summary>
/// 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.
/// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source sequence to expose as an asynchronous operation.</param>
/// <returns>Windows Runtime asynchronous operation object that returns the last element of the observable sequence, reporting incremental progress for each source sequence element.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IAsyncOperationWithProgress<TSource, int> ToAsyncOperationWithProgress<TSource>(this IObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException("source");
return AsyncInfo.Run<TSource, int>((ct, progress) =>
{
var i = 0;
return source.Do(_ => progress.Report(i++)).ToTask(ct);
});
}
#region Progress
/// <summary>
/// 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.
/// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
/// <param name="source">Source sequence to compute a result sequence that gets exposed as an asynchronous operation.</param>
/// <param name="resultSelector">Selector function to map the source sequence on a result sequence.</param>
/// <returns>Windows Runtime asynchronous operation object that returns the last element of the result sequence, reporting incremental progress for each source sequence element.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="resultSelector"/> is null.</exception>
public static IAsyncOperationWithProgress<TResult, int> ToAsyncOperationWithProgress<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> resultSelector)
{
if (source == null)
throw new ArgumentNullException("source");
if (resultSelector == null)
throw new ArgumentNullException("resultSelector");
return AsyncInfo.Run<TResult, int>((ct, progress) =>
{
var i = 0;
return resultSelector(source.Do(_ => progress.Report(i++))).ToTask(ct);
});
}
/// <summary>
/// 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.
/// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
/// <typeparam name="TProgress">The type of the elements in the progress sequence.</typeparam>
/// <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>
/// <param name="resultSelector">Selector function to map the source sequence on a result sequence.</param>
/// <param name="progressSelector">Selector function to map the source sequence on a progress reporting sequence.</param>
/// <returns>Windows Runtime asynchronous operation object that returns the last element of the result sequence, reporting progress computed through the progress sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="resultSelector"/> or <paramref name="progressSelector"/> is null.</exception>
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)
{
if (source == null)
throw new ArgumentNullException("source");
if (resultSelector == null)
throw new ArgumentNullException("resultSelector");
if (progressSelector == null)
throw new ArgumentNullException("progressSelector");
return AsyncInfo.Run<TResult, TProgress>((ct, progress) =>
{
return Observable.Create<TResult>(observer =>
{
var obs = Observer.Synchronize(observer);
var data = source.Publish();
var progressSubscription = progressSelector(data).Subscribe(progress.Report, obs.OnError);
var dataSubscription = resultSelector(data).Subscribe(obs);
var connection = data.Connect();
return new CompositeDisposable(progressSubscription, dataSubscription, connection);
}).ToTask(ct);
});
}
#endregion
#endregion
}
}
#endif

View File

@@ -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.Threading;
using Windows.Foundation;
namespace System.Reactive.Linq
{
/// <summary>
/// Provides a set of static methods for importing typed events from Windows Runtime APIs.
/// </summary>
public static partial class WindowsObservable
{
/// <summary>
/// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
/// </summary>
/// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
/// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
/// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
/// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
/// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
/// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
/// <seealso cref="WindowsObservable.ToEventPattern"/>
public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TSender, TResult>(Action<TypedEventHandler<TSender, TResult>> addHandler, Action<TypedEventHandler<TSender, TResult>> removeHandler)
{
if (addHandler == null)
throw new ArgumentNullException("addHandler");
if (removeHandler == null)
throw new ArgumentNullException("removeHandler");
return Observable.Create<EventPattern<TSender, TResult>>(observer =>
{
var h = new TypedEventHandler<TSender, TResult>((sender, args) =>
{
observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
});
addHandler(h);
return () =>
{
removeHandler(h);
};
});
}
/// <summary>
/// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
/// </summary>
/// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
/// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
/// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
/// <param name="conversion">A function used to convert the given event handler to a delegate compatible with the underlying typed event. The resulting delegate is used in calls to the addHandler and removeHandler action parameters.</param>
/// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
/// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
/// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
/// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
/// <seealso cref="WindowsObservable.ToEventPattern"/>
public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TDelegate, TSender, TResult>(Func<TypedEventHandler<TSender, TResult>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
{
if (conversion == null)
throw new ArgumentNullException("conversion");
if (addHandler == null)
throw new ArgumentNullException("addHandler");
if (removeHandler == null)
throw new ArgumentNullException("removeHandler");
return Observable.Create<EventPattern<TSender, TResult>>(observer =>
{
var h = conversion(new TypedEventHandler<TSender, TResult>((sender, args) =>
{
observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
}));
addHandler(h);
return () =>
{
removeHandler(h);
};
});
}
/// <summary>
/// Exposes an observable sequence as an object with a typed event.
/// </summary>
/// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
/// <param name="source">Observable source sequence.</param>
/// <returns>The event source object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)
{
if (source == null)
throw new ArgumentNullException("source");
return new EventPatternSource<TSender, TEventArgs>(source, (h, evt) => h(evt.Sender, evt.EventArgs));
}
}
}
#endif

View File

@@ -0,0 +1,103 @@
// 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.Windows.Foundation;
using System.Threading;
using Windows.Foundation;
namespace System.Reactive.Linq
{
public static partial class WindowsObservable
{
/// <summary>
/// Projects each element of an observable sequence to a Windows Runtime asynchronous operation and merges all of the asynchronous operation results into one observable sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TResult">The type of the result produced by the projected asynchronous operations and the elements in the merged result sequence.</typeparam>
/// <param name="source">An observable sequence of elements to project.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>An observable sequence whose elements are the result of the asynchronous operations executed for each element of the input sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
/// <remarks>This overload supports composition of observable sequences and Windows Runtime asynchronous operations, without requiring manual conversion of the asynchronous operations to observable sequences using <see cref="AsyncInfoObservableExtensions.ToObservable&lt;TResult&gt;(IAsyncOperation&lt;TResult&gt;)"/>.</remarks>
public static IObservable<TResult> SelectMany<TSource, TResult>(this IObservable<TSource> source, Func<TSource, IAsyncOperation<TResult>> selector)
{
if (source == null)
throw new ArgumentNullException("source");
if (selector == null)
throw new ArgumentNullException("selector");
return source.SelectMany(x => selector(x).ToObservable());
}
/// <summary>
/// Projects each element of an observable sequence to a Windows Runtime asynchronous operation and merges all of the asynchronous operation results into one observable sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TResult">The type of the result produced by the projected asynchronous operations and the elements in the merged result sequence.</typeparam>
/// <typeparam name="TProgress">The type of the reported progress objects, which get ignored by this query operator.</typeparam>
/// <param name="source">An observable sequence of elements to project.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>An observable sequence whose elements are the result of the asynchronous operations executed for each element of the input sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
/// <remarks>This overload supports composition of observable sequences and Windows Runtime asynchronous operations, without requiring manual conversion of the asynchronous operations to observable sequences using <see cref="AsyncInfoObservableExtensions.ToObservable&lt;TResult&gt;(IAsyncOperation&lt;TResult&gt;)"/>.</remarks>
public static IObservable<TResult> SelectMany<TSource, TResult, TProgress>(this IObservable<TSource> source, Func<TSource, IAsyncOperationWithProgress<TResult, TProgress>> selector)
{
if (source == null)
throw new ArgumentNullException("source");
if (selector == null)
throw new ArgumentNullException("selector");
return source.SelectMany(x => selector(x).ToObservable());
}
/// <summary>
/// Projects each element of an observable sequence to a Windows Runtime asynchronous operation, invokes the result selector for the source element and the asynchronous operation result, and merges the results into one observable sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TAsyncOperationResult">The type of the results produced by the projected asynchronous operations.</typeparam>
/// <typeparam name="TResult">The type of the elements in the result sequence, obtained by using the selector to combine source sequence elements with their corresponding intermediate asynchronous operation results.</typeparam>
/// <param name="source">An observable sequence of elements to project.</param>
/// <param name="asyncOperationSelector">A transform function to apply to each element.</param>
/// <param name="resultSelector">A transform function to apply to each element of the intermediate sequence.</param>
/// <returns>An observable sequence whose elements are the result of obtaining an asynchronous operation for each element of the input sequence and then mapping the asynchronous operation's result and its corresponding source element to a result element.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="asyncOperationSelector"/> or <paramref name="resultSelector"/> is null.</exception>
/// <remarks>This overload supports using LINQ query comprehension syntax in C# and Visual Basic to compose observable sequences and Windows Runtime asynchronous operations, without requiring manual conversion of the asynchronous operations to observable sequences using <see cref="AsyncInfoObservableExtensions.ToObservable&lt;TResult&gt;(IAsyncOperation&lt;TResult&gt;)"/>.</remarks>
public static IObservable<TResult> SelectMany<TSource, TAsyncOperationResult, TResult>(this IObservable<TSource> source, Func<TSource, IAsyncOperation<TAsyncOperationResult>> asyncOperationSelector, Func<TSource, TAsyncOperationResult, TResult> resultSelector)
{
if (source == null)
throw new ArgumentNullException("source");
if (asyncOperationSelector == null)
throw new ArgumentNullException("asyncOperationSelector");
if (resultSelector == null)
throw new ArgumentNullException("resultSelector");
return source.SelectMany(x => asyncOperationSelector(x).ToObservable(), resultSelector);
}
/// <summary>
/// Projects each element of an observable sequence to a Windows Runtime asynchronous operation, invokes the result selector for the source element and the asynchronous operation result, and merges the results into one observable sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <typeparam name="TAsyncOperationResult">The type of the results produced by the projected asynchronous operations.</typeparam>
/// <typeparam name="TAsyncOperationProgress">The type of the reported progress objects, which get ignored by this query operator.</typeparam>
/// <typeparam name="TResult">The type of the elements in the result sequence, obtained by using the selector to combine source sequence elements with their corresponding intermediate asynchronous operation results.</typeparam>
/// <param name="source">An observable sequence of elements to project.</param>
/// <param name="asyncOperationSelector">A transform function to apply to each element.</param>
/// <param name="resultSelector">A transform function to apply to each element of the intermediate sequence.</param>
/// <returns>An observable sequence whose elements are the result of obtaining an asynchronous operation for each element of the input sequence and then mapping the asynchronous operation's result and its corresponding source element to a result element.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="asyncOperationSelector"/> or <paramref name="resultSelector"/> is null.</exception>
/// <remarks>This overload supports using LINQ query comprehension syntax in C# and Visual Basic to compose observable sequences and Windows Runtime asynchronous operations, without requiring manual conversion of the asynchronous operations to observable sequences using <see cref="AsyncInfoObservableExtensions.ToObservable&lt;TResult&gt;(IAsyncOperation&lt;TResult&gt;)"/>.</remarks>
public static IObservable<TResult> SelectMany<TSource, TAsyncOperationResult, TAsyncOperationProgress, TResult>(this IObservable<TSource> source, Func<TSource, IAsyncOperationWithProgress<TAsyncOperationResult, TAsyncOperationProgress>> asyncOperationSelector, Func<TSource, TAsyncOperationResult, TResult> resultSelector)
{
if (source == null)
throw new ArgumentNullException("source");
if (asyncOperationSelector == null)
throw new ArgumentNullException("asyncOperationSelector");
if (resultSelector == null)
throw new ArgumentNullException("resultSelector");
return source.SelectMany(x => asyncOperationSelector(x).ToObservable(), resultSelector);
}
}
}
#endif