TriggerEvent.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using System.Threading;
  3. namespace Cysharp.Threading.Tasks
  4. {
  5. public interface ITriggerHandler<T>
  6. {
  7. void OnNext(T value);
  8. void OnError(Exception ex);
  9. void OnCompleted();
  10. void OnCanceled(CancellationToken cancellationToken);
  11. // set/get from TriggerEvent<T>
  12. ITriggerHandler<T> Prev { get; set; }
  13. ITriggerHandler<T> Next { get; set; }
  14. }
  15. // be careful to use, itself is struct.
  16. public struct TriggerEvent<T>
  17. {
  18. ITriggerHandler<T> head; // head.prev is last
  19. ITriggerHandler<T> iteratingHead;
  20. bool preserveRemoveSelf;
  21. ITriggerHandler<T> iteratingNode;
  22. void LogError(Exception ex)
  23. {
  24. #if UNITY_2018_3_OR_NEWER
  25. UnityEngine.Debug.LogException(ex);
  26. #else
  27. Console.WriteLine(ex);
  28. #endif
  29. }
  30. public void SetResult(T value)
  31. {
  32. if (iteratingNode != null)
  33. {
  34. throw new InvalidOperationException("Can not trigger itself in iterating.");
  35. }
  36. var h = head;
  37. while (h != null)
  38. {
  39. iteratingNode = h;
  40. try
  41. {
  42. h.OnNext(value);
  43. }
  44. catch (Exception ex)
  45. {
  46. LogError(ex);
  47. Remove(h);
  48. }
  49. if (preserveRemoveSelf)
  50. {
  51. preserveRemoveSelf = false;
  52. iteratingNode = null;
  53. var next = h.Next;
  54. Remove(h);
  55. h = next;
  56. }
  57. else
  58. {
  59. h = h.Next;
  60. }
  61. }
  62. iteratingNode = null;
  63. if (iteratingHead != null)
  64. {
  65. Add(iteratingHead);
  66. iteratingHead = null;
  67. }
  68. }
  69. public void SetCanceled(CancellationToken cancellationToken)
  70. {
  71. if (iteratingNode != null)
  72. {
  73. throw new InvalidOperationException("Can not trigger itself in iterating.");
  74. }
  75. var h = head;
  76. while (h != null)
  77. {
  78. iteratingNode = h;
  79. try
  80. {
  81. h.OnCanceled(cancellationToken);
  82. }
  83. catch (Exception ex)
  84. {
  85. LogError(ex);
  86. }
  87. preserveRemoveSelf = false;
  88. iteratingNode = null;
  89. var next = h.Next;
  90. Remove(h);
  91. h = next;
  92. }
  93. iteratingNode = null;
  94. if (iteratingHead != null)
  95. {
  96. Add(iteratingHead);
  97. iteratingHead = null;
  98. }
  99. }
  100. public void SetCompleted()
  101. {
  102. if (iteratingNode != null)
  103. {
  104. throw new InvalidOperationException("Can not trigger itself in iterating.");
  105. }
  106. var h = head;
  107. while (h != null)
  108. {
  109. iteratingNode = h;
  110. try
  111. {
  112. h.OnCompleted();
  113. }
  114. catch (Exception ex)
  115. {
  116. LogError(ex);
  117. }
  118. preserveRemoveSelf = false;
  119. iteratingNode = null;
  120. var next = h.Next;
  121. Remove(h);
  122. h = next;
  123. }
  124. iteratingNode = null;
  125. if (iteratingHead != null)
  126. {
  127. Add(iteratingHead);
  128. iteratingHead = null;
  129. }
  130. }
  131. public void SetError(Exception exception)
  132. {
  133. if (iteratingNode != null)
  134. {
  135. throw new InvalidOperationException("Can not trigger itself in iterating.");
  136. }
  137. var h = head;
  138. while (h != null)
  139. {
  140. iteratingNode = h;
  141. try
  142. {
  143. h.OnError(exception);
  144. }
  145. catch (Exception ex)
  146. {
  147. LogError(ex);
  148. }
  149. preserveRemoveSelf = false;
  150. iteratingNode = null;
  151. var next = h.Next;
  152. Remove(h);
  153. h = next;
  154. }
  155. iteratingNode = null;
  156. if (iteratingHead != null)
  157. {
  158. Add(iteratingHead);
  159. iteratingHead = null;
  160. }
  161. }
  162. public void Add(ITriggerHandler<T> handler)
  163. {
  164. if (handler == null) throw new ArgumentNullException(nameof(handler));
  165. // zero node.
  166. if (head == null)
  167. {
  168. head = handler;
  169. return;
  170. }
  171. if (iteratingNode != null)
  172. {
  173. if (iteratingHead == null)
  174. {
  175. iteratingHead = handler;
  176. return;
  177. }
  178. var last = iteratingHead.Prev;
  179. if (last == null)
  180. {
  181. // single node.
  182. iteratingHead.Prev = handler;
  183. iteratingHead.Next = handler;
  184. handler.Prev = iteratingHead;
  185. }
  186. else
  187. {
  188. // multi node
  189. iteratingHead.Prev = handler;
  190. last.Next = handler;
  191. handler.Prev = last;
  192. }
  193. }
  194. else
  195. {
  196. var last = head.Prev;
  197. if (last == null)
  198. {
  199. // single node.
  200. head.Prev = handler;
  201. head.Next = handler;
  202. handler.Prev = head;
  203. }
  204. else
  205. {
  206. // multi node
  207. head.Prev = handler;
  208. last.Next = handler;
  209. handler.Prev = last;
  210. }
  211. }
  212. }
  213. public void Remove(ITriggerHandler<T> handler)
  214. {
  215. if (handler == null) throw new ArgumentNullException(nameof(handler));
  216. if (iteratingNode != null && iteratingNode == handler)
  217. {
  218. // if remove self, reserve remove self after invoke completed.
  219. preserveRemoveSelf = true;
  220. }
  221. else
  222. {
  223. var prev = handler.Prev;
  224. var next = handler.Next;
  225. if (next != null)
  226. {
  227. next.Prev = prev;
  228. }
  229. if (handler == head)
  230. {
  231. head = next;
  232. }
  233. else if (handler == iteratingHead)
  234. {
  235. iteratingHead = next;
  236. }
  237. else
  238. {
  239. // when handler is head, prev indicate last so don't use it.
  240. if (prev != null)
  241. {
  242. prev.Next = next;
  243. }
  244. }
  245. if (head != null)
  246. {
  247. if (head.Prev == handler)
  248. {
  249. if (prev != head)
  250. {
  251. head.Prev = prev;
  252. }
  253. else
  254. {
  255. head.Prev = null;
  256. }
  257. }
  258. }
  259. if (iteratingHead != null)
  260. {
  261. if (iteratingHead.Prev == handler)
  262. {
  263. if (prev != iteratingHead.Prev)
  264. {
  265. iteratingHead.Prev = prev;
  266. }
  267. else
  268. {
  269. iteratingHead.Prev = null;
  270. }
  271. }
  272. }
  273. handler.Prev = null;
  274. handler.Next = null;
  275. }
  276. }
  277. }
  278. }