WindowsObservable.Events.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if HAS_WINRT
  5. using System.Threading;
  6. using Windows.Foundation;
  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. [CLSCompliant(false)]
  13. public static partial class WindowsObservable
  14. {
  15. /// <summary>
  16. /// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
  17. /// </summary>
  18. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  19. /// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
  20. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  21. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  22. /// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  24. /// <seealso cref="WindowsObservable.ToEventPattern"/>
  25. public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TSender, TResult>(Action<TypedEventHandler<TSender, TResult>> addHandler, Action<TypedEventHandler<TSender, TResult>> removeHandler)
  26. {
  27. if (addHandler == null)
  28. throw new ArgumentNullException(nameof(addHandler));
  29. if (removeHandler == null)
  30. throw new ArgumentNullException(nameof(removeHandler));
  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. addHandler(h);
  38. return () =>
  39. {
  40. removeHandler(h);
  41. };
  42. });
  43. }
  44. /// <summary>
  45. /// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
  46. /// </summary>
  47. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  48. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  49. /// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
  50. /// <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>
  51. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  52. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  53. /// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
  54. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  55. /// <seealso cref="WindowsObservable.ToEventPattern"/>
  56. public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TDelegate, TSender, TResult>(Func<TypedEventHandler<TSender, TResult>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  57. {
  58. if (conversion == null)
  59. throw new ArgumentNullException(nameof(conversion));
  60. if (addHandler == null)
  61. throw new ArgumentNullException(nameof(addHandler));
  62. if (removeHandler == null)
  63. throw new ArgumentNullException(nameof(removeHandler));
  64. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  65. {
  66. var h = conversion(new TypedEventHandler<TSender, TResult>((sender, args) =>
  67. {
  68. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  69. }));
  70. addHandler(h);
  71. return () =>
  72. {
  73. removeHandler(h);
  74. };
  75. });
  76. }
  77. /// <summary>
  78. /// Exposes an observable sequence as an object with a typed event.
  79. /// </summary>
  80. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  81. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  82. /// <param name="source">Observable source sequence.</param>
  83. /// <returns>The event source object.</returns>
  84. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  85. public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)
  86. {
  87. if (source == null)
  88. throw new ArgumentNullException(nameof(source));
  89. return new EventPatternSource<TSender, TEventArgs>(source, (h, evt) => h(evt.Sender, evt.EventArgs));
  90. }
  91. }
  92. }
  93. #endif