// 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
{
///
/// Provides a set of static methods for importing typed events from Windows Runtime APIs.
///
public static partial class WindowsObservable
{
///
/// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
///
/// The type of the sender that raises the event.
/// The type of the event data generated by the event.
/// Action that attaches the given event handler to the underlying .NET event.
/// Action that detaches the given event handler from the underlying .NET event.
/// The observable sequence that contains data representations of invocations of the underlying typed event.
/// or is null.
///
public static IObservable> FromEventPattern(Action> addHandler, Action> removeHandler)
{
if (addHandler == null)
throw new ArgumentNullException("addHandler");
if (removeHandler == null)
throw new ArgumentNullException("removeHandler");
return Observable.Create>(observer =>
{
var h = new TypedEventHandler((sender, args) =>
{
observer.OnNext(new EventPattern(sender, args));
});
addHandler(h);
return () =>
{
removeHandler(h);
};
});
}
///
/// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
///
/// The delegate type of the event to be converted.
/// The type of the sender that raises the event.
/// The type of the event data generated by the event.
/// 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.
/// Action that attaches the given event handler to the underlying .NET event.
/// Action that detaches the given event handler from the underlying .NET event.
/// The observable sequence that contains data representations of invocations of the underlying typed event.
/// or or is null.
///
public static IObservable> FromEventPattern(Func, TDelegate> conversion, Action addHandler, Action 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>(observer =>
{
var h = conversion(new TypedEventHandler((sender, args) =>
{
observer.OnNext(new EventPattern(sender, args));
}));
addHandler(h);
return () =>
{
removeHandler(h);
};
});
}
///
/// Exposes an observable sequence as an object with a typed event.
///
/// The type of the sender that raises the event.
/// The type of the event data generated by the event.
/// Observable source sequence.
/// The event source object.
/// is null.
public static IEventPatternSource ToEventPattern(this IObservable> source)
{
if (source == null)
throw new ArgumentNullException("source");
return new EventPatternSource(source, (h, evt) => h(evt.Sender, evt.EventArgs));
}
}
}
#endif