| 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.Threading; |
| 5 |
using Windows.Foundation; |
| 6 |
|
| 7 |
namespace System.Reactive.Linq |
| 8 |
{ |
| 9 |
/// <summary> |
| 10 |
/// Provides a set of static methods for importing typed events from Windows Runtime APIs. |
| 11 |
/// </summary> |
| 12 |
public static partial class WindowsObservable |
| 13 |
{ |
| 14 |
/// <summary> |
| 15 |
/// Converts a typed event, conforming to the standard event pattern, to an observable sequence. |
| 16 |
/// </summary> |
| 17 |
/// <typeparam name="TSender">The type of the sender that raises the event.</typeparam> |
| 18 |
/// <typeparam name="TResult">The type of the event data generated by the event.</typeparam> |
| 19 |
/// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param> |
| 20 |
/// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param> |
| 21 |
/// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns> |
| 22 |
/// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception> |
| 23 |
/// <seealso cref="WindowsObservable.ToEventPattern"/> |
| 24 |
public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TSender, TResult>(Action<TypedEventHandler<TSender, TResult>> addHandler, Action<TypedEventHandler<TSender, TResult>> removeHandler) |
| 25 |
{ |
| 26 |
if (addHandler == null) |
| 27 |
throw new ArgumentNullException("addHandler"); |
| 28 |
if (removeHandler == null) |
| 29 |
throw new ArgumentNullException("removeHandler"); |
| 30 |
|
| 31 |
return Observable.Create<EventPattern<TSender, TResult>>(observer => |
| 32 |
{ |
| 33 |
var h = new TypedEventHandler<TSender, TResult>((sender, args) => |
| 34 |
{ |
| 35 |
observer.OnNext(new EventPattern<TSender, TResult>(sender, args)); |
| 36 |
}); |
| 37 |
|
| 38 |
addHandler(h); |
| 39 |
|
| 40 |
return () => |
| 41 |
{ |
| 42 |
removeHandler(h); |
| 43 |
}; |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
/// <summary> |
| 48 |
/// Converts a typed event, conforming to the standard event pattern, to an observable sequence. |
| 49 |
/// </summary> |
| 50 |
/// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam> |
| 51 |
/// <typeparam name="TSender">The type of the sender that raises the event.</typeparam> |
| 52 |
/// <typeparam name="TResult">The type of the event data generated by the event.</typeparam> |
| 53 |
/// <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> |
| 54 |
/// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param> |
| 55 |
/// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param> |
| 56 |
/// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns> |
| 57 |
/// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception> |
| 58 |
/// <seealso cref="WindowsObservable.ToEventPattern"/> |
| 59 |
public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TDelegate, TSender, TResult>(Func<TypedEventHandler<TSender, TResult>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler) |
| 60 |
{ |
| 61 |
if (conversion == null) |
| 62 |
throw new ArgumentNullException("conversion"); |
| 63 |
if (addHandler == null) |
| 64 |
throw new ArgumentNullException("addHandler"); |
| 65 |
if (removeHandler == null) |
| 66 |
throw new ArgumentNullException("removeHandler"); |
| 67 |
|
| 68 |
return Observable.Create<EventPattern<TSender, TResult>>(observer => |
| 69 |
{ |
| 70 |
var h = conversion(new TypedEventHandler<TSender, TResult>((sender, args) => |
| 71 |
{ |
| 72 |
observer.OnNext(new EventPattern<TSender, TResult>(sender, args)); |
| 73 |
})); |
| 74 |
|
| 75 |
addHandler(h); |
| 76 |
|
| 77 |
return () => |
| 78 |
{ |
| 79 |
removeHandler(h); |
| 80 |
}; |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
/// <summary> |
| 85 |
/// Exposes an observable sequence as an object with a typed event. |
| 86 |
/// </summary> |
| 87 |
/// <typeparam name="TSender">The type of the sender that raises the event.</typeparam> |
| 88 |
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam> |
| 89 |
/// <param name="source">Observable source sequence.</param> |
| 90 |
/// <returns>The event source object.</returns> |
| 91 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 92 |
public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source) |
| 93 |
{ |
| 94 |
if (source == null) |
| 95 |
throw new ArgumentNullException("source"); |
| 96 |
|
| 97 |
return new EventPatternSource<TSender, TEventArgs>(source, (h, evt) => h(evt.Sender, evt.EventArgs)); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
#endif |