WindowsObservable.Events.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Windows.Foundation;
  6. namespace System.Reactive.Linq
  7. {
  8. /// <summary>
  9. /// Provides a set of static methods for importing typed events from Windows Runtime APIs.
  10. /// </summary>
  11. [CLSCompliant(false)]
  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="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. {
  28. throw new ArgumentNullException(nameof(addHandler));
  29. }
  30. if (removeHandler == null)
  31. {
  32. throw new ArgumentNullException(nameof(removeHandler));
  33. }
  34. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  35. {
  36. var h = new TypedEventHandler<TSender, TResult>((sender, args) =>
  37. {
  38. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  39. });
  40. addHandler(h);
  41. return () =>
  42. {
  43. removeHandler(h);
  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="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. {
  63. throw new ArgumentNullException(nameof(conversion));
  64. }
  65. if (addHandler == null)
  66. {
  67. throw new ArgumentNullException(nameof(addHandler));
  68. }
  69. if (removeHandler == null)
  70. {
  71. throw new ArgumentNullException(nameof(removeHandler));
  72. }
  73. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  74. {
  75. var h = conversion(new TypedEventHandler<TSender, TResult>((sender, args) =>
  76. {
  77. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  78. }));
  79. addHandler(h);
  80. return () =>
  81. {
  82. removeHandler(h);
  83. };
  84. });
  85. }
  86. /// <summary>
  87. /// Exposes an observable sequence as an object with a typed event.
  88. /// </summary>
  89. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  90. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  91. /// <param name="source">Observable source sequence.</param>
  92. /// <returns>The event source object.</returns>
  93. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  94. public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)
  95. {
  96. if (source == null)
  97. {
  98. throw new ArgumentNullException(nameof(source));
  99. }
  100. return new EventPatternSource<TSender, TEventArgs>(source, static (h, evt) => h(evt.Sender, evt.EventArgs));
  101. }
  102. }
  103. }
  104. #endif