WindowsObservable.Events.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if HAS_WINRT
  3. using System.Threading;
  4. using Windows.Foundation;
  5. namespace System.Reactive.Linq
  6. {
  7. /// <summary>
  8. /// Provides a set of static methods for importing typed events from Windows Runtime APIs.
  9. /// </summary>
  10. public static partial class WindowsObservable
  11. {
  12. /// <summary>
  13. /// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
  14. /// </summary>
  15. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  16. /// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
  17. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  18. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  19. /// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  21. /// <seealso cref="WindowsObservable.ToEventPattern"/>
  22. public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TSender, TResult>(Action<TypedEventHandler<TSender, TResult>> addHandler, Action<TypedEventHandler<TSender, TResult>> removeHandler)
  23. {
  24. if (addHandler == null)
  25. throw new ArgumentNullException("addHandler");
  26. if (removeHandler == null)
  27. throw new ArgumentNullException("removeHandler");
  28. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  29. {
  30. var h = new TypedEventHandler<TSender, TResult>((sender, args) =>
  31. {
  32. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  33. });
  34. addHandler(h);
  35. return () =>
  36. {
  37. removeHandler(h);
  38. };
  39. });
  40. }
  41. /// <summary>
  42. /// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
  43. /// </summary>
  44. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  45. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  46. /// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
  47. /// <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>
  48. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  49. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  50. /// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
  51. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  52. /// <seealso cref="WindowsObservable.ToEventPattern"/>
  53. public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TDelegate, TSender, TResult>(Func<TypedEventHandler<TSender, TResult>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  54. {
  55. if (conversion == null)
  56. throw new ArgumentNullException("conversion");
  57. if (addHandler == null)
  58. throw new ArgumentNullException("addHandler");
  59. if (removeHandler == null)
  60. throw new ArgumentNullException("removeHandler");
  61. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  62. {
  63. var h = conversion(new TypedEventHandler<TSender, TResult>((sender, args) =>
  64. {
  65. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  66. }));
  67. addHandler(h);
  68. return () =>
  69. {
  70. removeHandler(h);
  71. };
  72. });
  73. }
  74. /// <summary>
  75. /// Exposes an observable sequence as an object with a typed event.
  76. /// </summary>
  77. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  78. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  79. /// <param name="source">Observable source sequence.</param>
  80. /// <returns>The event source object.</returns>
  81. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  82. public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)
  83. {
  84. if (source == null)
  85. throw new ArgumentNullException("source");
  86. return new EventPatternSource<TSender, TEventArgs>(source, (h, evt) => h(evt.Sender, evt.EventArgs));
  87. }
  88. }
  89. }
  90. #endif