// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
#if HAS_WINRT
using System.Reactive.WindowsRuntime;
using Windows.Foundation;
namespace System.Reactive.Linq
{
///
/// Provides a set of static methods for importing typed events from Windows Runtime APIs.
///
[CLSCompliant(false)]
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(nameof(addHandler));
}
if (removeHandler == null)
{
throw new ArgumentNullException(nameof(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(nameof(conversion));
}
if (addHandler == null)
{
throw new ArgumentNullException(nameof(addHandler));
}
if (removeHandler == null)
{
throw new ArgumentNullException(nameof(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(nameof(source));
}
return new EventPatternSource(source, static (h, evt) => h(evt.Sender!, evt.EventArgs));
}
}
}
#endif