2
0

FromEvent.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace UniRx.Operators
  6. {
  7. // FromEvent, FromEventPattern
  8. internal class FromEventPatternObservable<TDelegate, TEventArgs> : OperatorObservableBase<EventPattern<TEventArgs>>
  9. where TEventArgs : EventArgs
  10. {
  11. readonly Func<EventHandler<TEventArgs>, TDelegate> conversion;
  12. readonly Action<TDelegate> addHandler;
  13. readonly Action<TDelegate> removeHandler;
  14. public FromEventPatternObservable(Func<EventHandler<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  15. : base(false)
  16. {
  17. this.conversion = conversion;
  18. this.addHandler = addHandler;
  19. this.removeHandler = removeHandler;
  20. }
  21. protected override IDisposable SubscribeCore(IObserver<EventPattern<TEventArgs>> observer, IDisposable cancel)
  22. {
  23. var fe = new FromEventPattern(this, observer);
  24. return fe.Register() ? fe : Disposable.Empty;
  25. }
  26. class FromEventPattern : IDisposable
  27. {
  28. readonly FromEventPatternObservable<TDelegate, TEventArgs> parent;
  29. readonly IObserver<EventPattern<TEventArgs>> observer;
  30. TDelegate handler;
  31. public FromEventPattern(FromEventPatternObservable<TDelegate, TEventArgs> parent, IObserver<EventPattern<TEventArgs>> observer)
  32. {
  33. this.parent = parent;
  34. this.observer = observer;
  35. }
  36. public bool Register()
  37. {
  38. handler = parent.conversion(OnNext);
  39. try
  40. {
  41. parent.addHandler(handler);
  42. }
  43. catch (Exception ex)
  44. {
  45. observer.OnError(ex);
  46. return false;
  47. }
  48. return true;
  49. }
  50. void OnNext(object sender, TEventArgs eventArgs)
  51. {
  52. observer.OnNext(new EventPattern<TEventArgs>(sender, eventArgs));
  53. }
  54. public void Dispose()
  55. {
  56. if (handler != null)
  57. {
  58. parent.removeHandler(handler);
  59. handler = default(TDelegate);
  60. }
  61. }
  62. }
  63. }
  64. internal class FromEventObservable<TDelegate> : OperatorObservableBase<Unit>
  65. {
  66. readonly Func<Action, TDelegate> conversion;
  67. readonly Action<TDelegate> addHandler;
  68. readonly Action<TDelegate> removeHandler;
  69. public FromEventObservable(Func<Action, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  70. : base(false)
  71. {
  72. this.conversion = conversion;
  73. this.addHandler = addHandler;
  74. this.removeHandler = removeHandler;
  75. }
  76. protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
  77. {
  78. var fe = new FromEvent(this, observer);
  79. return fe.Register() ? fe : Disposable.Empty;
  80. }
  81. class FromEvent : IDisposable
  82. {
  83. readonly FromEventObservable<TDelegate> parent;
  84. readonly IObserver<Unit> observer;
  85. TDelegate handler;
  86. public FromEvent(FromEventObservable<TDelegate> parent, IObserver<Unit> observer)
  87. {
  88. this.parent = parent;
  89. this.observer = observer;
  90. }
  91. public bool Register()
  92. {
  93. handler = parent.conversion(OnNext);
  94. try
  95. {
  96. parent.addHandler(handler);
  97. }
  98. catch (Exception ex)
  99. {
  100. observer.OnError(ex);
  101. return false;
  102. }
  103. return true;
  104. }
  105. void OnNext()
  106. {
  107. observer.OnNext(Unit.Default);
  108. }
  109. public void Dispose()
  110. {
  111. if (handler != null)
  112. {
  113. parent.removeHandler(handler);
  114. handler = default(TDelegate);
  115. }
  116. }
  117. }
  118. }
  119. internal class FromEventObservable<TDelegate, TEventArgs> : OperatorObservableBase<TEventArgs>
  120. {
  121. readonly Func<Action<TEventArgs>, TDelegate> conversion;
  122. readonly Action<TDelegate> addHandler;
  123. readonly Action<TDelegate> removeHandler;
  124. public FromEventObservable(Func<Action<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  125. : base(false)
  126. {
  127. this.conversion = conversion;
  128. this.addHandler = addHandler;
  129. this.removeHandler = removeHandler;
  130. }
  131. protected override IDisposable SubscribeCore(IObserver<TEventArgs> observer, IDisposable cancel)
  132. {
  133. var fe = new FromEvent(this, observer);
  134. return fe.Register() ? fe : Disposable.Empty;
  135. }
  136. class FromEvent : IDisposable
  137. {
  138. readonly FromEventObservable<TDelegate, TEventArgs> parent;
  139. readonly IObserver<TEventArgs> observer;
  140. TDelegate handler;
  141. public FromEvent(FromEventObservable<TDelegate, TEventArgs> parent, IObserver<TEventArgs> observer)
  142. {
  143. this.parent = parent;
  144. this.observer = observer;
  145. }
  146. public bool Register()
  147. {
  148. handler = parent.conversion(OnNext);
  149. try
  150. {
  151. parent.addHandler(handler);
  152. }
  153. catch (Exception ex)
  154. {
  155. observer.OnError(ex);
  156. return false;
  157. }
  158. return true;
  159. }
  160. void OnNext(TEventArgs args)
  161. {
  162. observer.OnNext(args);
  163. }
  164. public void Dispose()
  165. {
  166. if (handler != null)
  167. {
  168. parent.removeHandler(handler);
  169. handler = default(TDelegate);
  170. }
  171. }
  172. }
  173. }
  174. internal class FromEventObservable : OperatorObservableBase<Unit>
  175. {
  176. readonly Action<Action> addHandler;
  177. readonly Action<Action> removeHandler;
  178. public FromEventObservable(Action<Action> addHandler, Action<Action> removeHandler)
  179. : base(false)
  180. {
  181. this.addHandler = addHandler;
  182. this.removeHandler = removeHandler;
  183. }
  184. protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
  185. {
  186. var fe = new FromEvent(this, observer);
  187. return fe.Register() ? fe : Disposable.Empty;
  188. }
  189. class FromEvent : IDisposable
  190. {
  191. readonly FromEventObservable parent;
  192. readonly IObserver<Unit> observer;
  193. Action handler;
  194. public FromEvent(FromEventObservable parent, IObserver<Unit> observer)
  195. {
  196. this.parent = parent;
  197. this.observer = observer;
  198. this.handler = OnNext;
  199. }
  200. public bool Register()
  201. {
  202. try
  203. {
  204. parent.addHandler(handler);
  205. }
  206. catch (Exception ex)
  207. {
  208. observer.OnError(ex);
  209. return false;
  210. }
  211. return true;
  212. }
  213. void OnNext()
  214. {
  215. observer.OnNext(Unit.Default);
  216. }
  217. public void Dispose()
  218. {
  219. if (handler != null)
  220. {
  221. parent.removeHandler(handler);
  222. handler = null;
  223. }
  224. }
  225. }
  226. }
  227. internal class FromEventObservable_<T> : OperatorObservableBase<T>
  228. {
  229. readonly Action<Action<T>> addHandler;
  230. readonly Action<Action<T>> removeHandler;
  231. public FromEventObservable_(Action<Action<T>> addHandler, Action<Action<T>> removeHandler)
  232. : base(false)
  233. {
  234. this.addHandler = addHandler;
  235. this.removeHandler = removeHandler;
  236. }
  237. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  238. {
  239. var fe = new FromEvent(this, observer);
  240. return fe.Register() ? fe : Disposable.Empty;
  241. }
  242. class FromEvent : IDisposable
  243. {
  244. readonly FromEventObservable_<T> parent;
  245. readonly IObserver<T> observer;
  246. Action<T> handler;
  247. public FromEvent(FromEventObservable_<T> parent, IObserver<T> observer)
  248. {
  249. this.parent = parent;
  250. this.observer = observer;
  251. this.handler = OnNext;
  252. }
  253. public bool Register()
  254. {
  255. try
  256. {
  257. parent.addHandler(handler);
  258. }
  259. catch (Exception ex)
  260. {
  261. observer.OnError(ex);
  262. return false;
  263. }
  264. return true;
  265. }
  266. void OnNext(T value)
  267. {
  268. observer.OnNext(value);
  269. }
  270. public void Dispose()
  271. {
  272. if (handler != null)
  273. {
  274. parent.removeHandler(handler);
  275. handler = null;
  276. }
  277. }
  278. }
  279. }
  280. }