AsyncTriggerBase.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. using System;
  3. using System.Threading;
  4. using UnityEngine;
  5. namespace Cysharp.Threading.Tasks.Triggers
  6. {
  7. public abstract class AsyncTriggerBase<T> : MonoBehaviour, IUniTaskAsyncEnumerable<T>
  8. {
  9. TriggerEvent<T> triggerEvent;
  10. internal protected bool calledAwake;
  11. internal protected bool calledDestroy;
  12. void Awake()
  13. {
  14. calledAwake = true;
  15. }
  16. void OnDestroy()
  17. {
  18. if (calledDestroy) return;
  19. calledDestroy = true;
  20. triggerEvent.SetCompleted();
  21. }
  22. internal void AddHandler(ITriggerHandler<T> handler)
  23. {
  24. if (!calledAwake)
  25. {
  26. PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
  27. }
  28. triggerEvent.Add(handler);
  29. }
  30. internal void RemoveHandler(ITriggerHandler<T> handler)
  31. {
  32. if (!calledAwake)
  33. {
  34. PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
  35. }
  36. triggerEvent.Remove(handler);
  37. }
  38. protected void RaiseEvent(T value)
  39. {
  40. triggerEvent.SetResult(value);
  41. }
  42. public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  43. {
  44. return new AsyncTriggerEnumerator(this, cancellationToken);
  45. }
  46. sealed class AsyncTriggerEnumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHandler<T>
  47. {
  48. static Action<object> cancellationCallback = CancellationCallback;
  49. readonly AsyncTriggerBase<T> parent;
  50. CancellationToken cancellationToken;
  51. CancellationTokenRegistration registration;
  52. bool called;
  53. bool isDisposed;
  54. public AsyncTriggerEnumerator(AsyncTriggerBase<T> parent, CancellationToken cancellationToken)
  55. {
  56. this.parent = parent;
  57. this.cancellationToken = cancellationToken;
  58. }
  59. public void OnCanceled(CancellationToken cancellationToken = default)
  60. {
  61. completionSource.TrySetCanceled(cancellationToken);
  62. }
  63. public void OnNext(T value)
  64. {
  65. Current = value;
  66. completionSource.TrySetResult(true);
  67. }
  68. public void OnCompleted()
  69. {
  70. completionSource.TrySetResult(false);
  71. }
  72. public void OnError(Exception ex)
  73. {
  74. completionSource.TrySetException(ex);
  75. }
  76. static void CancellationCallback(object state)
  77. {
  78. var self = (AsyncTriggerEnumerator)state;
  79. self.DisposeAsync().Forget(); // sync
  80. self.completionSource.TrySetCanceled(self.cancellationToken);
  81. }
  82. public T Current { get; private set; }
  83. ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
  84. ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
  85. public UniTask<bool> MoveNextAsync()
  86. {
  87. cancellationToken.ThrowIfCancellationRequested();
  88. completionSource.Reset();
  89. if (!called)
  90. {
  91. called = true;
  92. TaskTracker.TrackActiveTask(this, 3);
  93. parent.AddHandler(this);
  94. if (cancellationToken.CanBeCanceled)
  95. {
  96. registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
  97. }
  98. }
  99. return new UniTask<bool>(this, completionSource.Version);
  100. }
  101. public UniTask DisposeAsync()
  102. {
  103. if (!isDisposed)
  104. {
  105. isDisposed = true;
  106. TaskTracker.RemoveTracking(this);
  107. registration.Dispose();
  108. parent.RemoveHandler(this);
  109. }
  110. return default;
  111. }
  112. }
  113. class AwakeMonitor : IPlayerLoopItem
  114. {
  115. readonly AsyncTriggerBase<T> trigger;
  116. public AwakeMonitor(AsyncTriggerBase<T> trigger)
  117. {
  118. this.trigger = trigger;
  119. }
  120. public bool MoveNext()
  121. {
  122. if (trigger.calledAwake) return false;
  123. if (trigger == null)
  124. {
  125. trigger.OnDestroy();
  126. return false;
  127. }
  128. return true;
  129. }
  130. }
  131. }
  132. public interface IAsyncOneShotTrigger
  133. {
  134. UniTask OneShotAsync();
  135. }
  136. public partial class AsyncTriggerHandler<T> : IAsyncOneShotTrigger
  137. {
  138. UniTask IAsyncOneShotTrigger.OneShotAsync()
  139. {
  140. core.Reset();
  141. return new UniTask((IUniTaskSource)this, core.Version);
  142. }
  143. }
  144. public sealed partial class AsyncTriggerHandler<T> : IUniTaskSource<T>, ITriggerHandler<T>, IDisposable
  145. {
  146. static Action<object> cancellationCallback = CancellationCallback;
  147. readonly AsyncTriggerBase<T> trigger;
  148. CancellationToken cancellationToken;
  149. CancellationTokenRegistration registration;
  150. bool isDisposed;
  151. bool callOnce;
  152. UniTaskCompletionSourceCore<T> core;
  153. internal CancellationToken CancellationToken => cancellationToken;
  154. ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
  155. ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
  156. internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, bool callOnce)
  157. {
  158. if (cancellationToken.IsCancellationRequested)
  159. {
  160. isDisposed = true;
  161. return;
  162. }
  163. this.trigger = trigger;
  164. this.cancellationToken = default;
  165. this.registration = default;
  166. this.callOnce = callOnce;
  167. trigger.AddHandler(this);
  168. TaskTracker.TrackActiveTask(this, 3);
  169. }
  170. internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, CancellationToken cancellationToken, bool callOnce)
  171. {
  172. if (cancellationToken.IsCancellationRequested)
  173. {
  174. isDisposed = true;
  175. return;
  176. }
  177. this.trigger = trigger;
  178. this.cancellationToken = cancellationToken;
  179. this.callOnce = callOnce;
  180. trigger.AddHandler(this);
  181. if (cancellationToken.CanBeCanceled)
  182. {
  183. registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
  184. }
  185. TaskTracker.TrackActiveTask(this, 3);
  186. }
  187. static void CancellationCallback(object state)
  188. {
  189. var self = (AsyncTriggerHandler<T>)state;
  190. self.Dispose();
  191. self.core.TrySetCanceled(self.cancellationToken);
  192. }
  193. public void Dispose()
  194. {
  195. if (!isDisposed)
  196. {
  197. isDisposed = true;
  198. TaskTracker.RemoveTracking(this);
  199. registration.Dispose();
  200. trigger.RemoveHandler(this);
  201. }
  202. }
  203. T IUniTaskSource<T>.GetResult(short token)
  204. {
  205. try
  206. {
  207. return core.GetResult(token);
  208. }
  209. finally
  210. {
  211. if (callOnce)
  212. {
  213. Dispose();
  214. }
  215. }
  216. }
  217. void ITriggerHandler<T>.OnNext(T value)
  218. {
  219. core.TrySetResult(value);
  220. }
  221. void ITriggerHandler<T>.OnCanceled(CancellationToken cancellationToken)
  222. {
  223. core.TrySetCanceled(cancellationToken);
  224. }
  225. void ITriggerHandler<T>.OnCompleted()
  226. {
  227. core.TrySetCanceled(CancellationToken.None);
  228. }
  229. void ITriggerHandler<T>.OnError(Exception ex)
  230. {
  231. core.TrySetException(ex);
  232. }
  233. void IUniTaskSource.GetResult(short token)
  234. {
  235. ((IUniTaskSource<T>)this).GetResult(token);
  236. }
  237. UniTaskStatus IUniTaskSource.GetStatus(short token)
  238. {
  239. return core.GetStatus(token);
  240. }
  241. UniTaskStatus IUniTaskSource.UnsafeGetStatus()
  242. {
  243. return core.UnsafeGetStatus();
  244. }
  245. void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
  246. {
  247. core.OnCompleted(continuation, state, token);
  248. }
  249. }
  250. }