1
0

WindowsObservable.Events.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #if HAS_WINRT
  5. using System.Reactive.WindowsRuntime;
  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="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. {
  29. throw new ArgumentNullException(nameof(addHandler));
  30. }
  31. if (removeHandler == null)
  32. {
  33. throw new ArgumentNullException(nameof(removeHandler));
  34. }
  35. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  36. {
  37. var h = new TypedEventHandler<TSender, TResult>((sender, args) =>
  38. {
  39. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  40. });
  41. addHandler(h);
  42. return () =>
  43. {
  44. removeHandler(h);
  45. };
  46. });
  47. }
  48. /// <summary>
  49. /// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
  50. /// </summary>
  51. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  52. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  53. /// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
  54. /// <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>
  55. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  56. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  57. /// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
  58. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  59. /// <seealso cref="ToEventPattern"/>
  60. public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TDelegate, TSender, TResult>(Func<TypedEventHandler<TSender, TResult>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  61. {
  62. if (conversion == null)
  63. {
  64. throw new ArgumentNullException(nameof(conversion));
  65. }
  66. if (addHandler == null)
  67. {
  68. throw new ArgumentNullException(nameof(addHandler));
  69. }
  70. if (removeHandler == null)
  71. {
  72. throw new ArgumentNullException(nameof(removeHandler));
  73. }
  74. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  75. {
  76. var h = conversion(new TypedEventHandler<TSender, TResult>((sender, args) =>
  77. {
  78. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  79. }));
  80. addHandler(h);
  81. return () =>
  82. {
  83. removeHandler(h);
  84. };
  85. });
  86. }
  87. /// <summary>
  88. /// Exposes an observable sequence as an object with a typed event.
  89. /// </summary>
  90. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  91. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  92. /// <param name="source">Observable source sequence.</param>
  93. /// <returns>The event source object.</returns>
  94. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  95. public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)
  96. {
  97. if (source == null)
  98. {
  99. throw new ArgumentNullException(nameof(source));
  100. }
  101. return new EventPatternSource<TSender, TEventArgs>(source, static (h, evt) => h(evt.Sender!, evt.EventArgs));
  102. }
  103. }
  104. }
  105. #endif