UniTask.WaitUntil.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using Cysharp.Threading.Tasks.Internal;
  6. namespace Cysharp.Threading.Tasks
  7. {
  8. public partial struct UniTask
  9. {
  10. public static UniTask WaitUntil(Func<bool> predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  11. {
  12. return new UniTask(WaitUntilPromise.Create(predicate, timing, cancellationToken, out var token), token);
  13. }
  14. public static UniTask WaitWhile(Func<bool> predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  15. {
  16. return new UniTask(WaitWhilePromise.Create(predicate, timing, cancellationToken, out var token), token);
  17. }
  18. public static UniTask WaitUntilCanceled(CancellationToken cancellationToken, PlayerLoopTiming timing = PlayerLoopTiming.Update)
  19. {
  20. return new UniTask(WaitUntilCanceledPromise.Create(cancellationToken, timing, out var token), token);
  21. }
  22. public static UniTask<U> WaitUntilValueChanged<T, U>(T target, Func<T, U> monitorFunction, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer<U> equalityComparer = null, CancellationToken cancellationToken = default(CancellationToken))
  23. where T : class
  24. {
  25. var unityObject = target as UnityEngine.Object;
  26. var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null)
  27. return new UniTask<U>(isUnityObject
  28. ? WaitUntilValueChangedUnityObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token)
  29. : WaitUntilValueChangedStandardObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out token), token);
  30. }
  31. sealed class WaitUntilPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitUntilPromise>
  32. {
  33. static TaskPool<WaitUntilPromise> pool;
  34. WaitUntilPromise nextNode;
  35. public ref WaitUntilPromise NextNode => ref nextNode;
  36. static WaitUntilPromise()
  37. {
  38. TaskPool.RegisterSizeGetter(typeof(WaitUntilPromise), () => pool.Size);
  39. }
  40. Func<bool> predicate;
  41. CancellationToken cancellationToken;
  42. UniTaskCompletionSourceCore<object> core;
  43. WaitUntilPromise()
  44. {
  45. }
  46. public static IUniTaskSource Create(Func<bool> predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
  47. {
  48. if (cancellationToken.IsCancellationRequested)
  49. {
  50. return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
  51. }
  52. if (!pool.TryPop(out var result))
  53. {
  54. result = new WaitUntilPromise();
  55. }
  56. result.predicate = predicate;
  57. result.cancellationToken = cancellationToken;
  58. TaskTracker.TrackActiveTask(result, 3);
  59. PlayerLoopHelper.AddAction(timing, result);
  60. token = result.core.Version;
  61. return result;
  62. }
  63. public void GetResult(short token)
  64. {
  65. try
  66. {
  67. core.GetResult(token);
  68. }
  69. finally
  70. {
  71. TryReturn();
  72. }
  73. }
  74. public UniTaskStatus GetStatus(short token)
  75. {
  76. return core.GetStatus(token);
  77. }
  78. public UniTaskStatus UnsafeGetStatus()
  79. {
  80. return core.UnsafeGetStatus();
  81. }
  82. public void OnCompleted(Action<object> continuation, object state, short token)
  83. {
  84. core.OnCompleted(continuation, state, token);
  85. }
  86. public bool MoveNext()
  87. {
  88. if (cancellationToken.IsCancellationRequested)
  89. {
  90. core.TrySetCanceled(cancellationToken);
  91. return false;
  92. }
  93. try
  94. {
  95. if (!predicate())
  96. {
  97. return true;
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. core.TrySetException(ex);
  103. return false;
  104. }
  105. core.TrySetResult(null);
  106. return false;
  107. }
  108. bool TryReturn()
  109. {
  110. TaskTracker.RemoveTracking(this);
  111. core.Reset();
  112. predicate = default;
  113. cancellationToken = default;
  114. return pool.TryPush(this);
  115. }
  116. }
  117. sealed class WaitWhilePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitWhilePromise>
  118. {
  119. static TaskPool<WaitWhilePromise> pool;
  120. WaitWhilePromise nextNode;
  121. public ref WaitWhilePromise NextNode => ref nextNode;
  122. static WaitWhilePromise()
  123. {
  124. TaskPool.RegisterSizeGetter(typeof(WaitWhilePromise), () => pool.Size);
  125. }
  126. Func<bool> predicate;
  127. CancellationToken cancellationToken;
  128. UniTaskCompletionSourceCore<object> core;
  129. WaitWhilePromise()
  130. {
  131. }
  132. public static IUniTaskSource Create(Func<bool> predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
  133. {
  134. if (cancellationToken.IsCancellationRequested)
  135. {
  136. return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
  137. }
  138. if (!pool.TryPop(out var result))
  139. {
  140. result = new WaitWhilePromise();
  141. }
  142. result.predicate = predicate;
  143. result.cancellationToken = cancellationToken;
  144. TaskTracker.TrackActiveTask(result, 3);
  145. PlayerLoopHelper.AddAction(timing, result);
  146. token = result.core.Version;
  147. return result;
  148. }
  149. public void GetResult(short token)
  150. {
  151. try
  152. {
  153. core.GetResult(token);
  154. }
  155. finally
  156. {
  157. TryReturn();
  158. }
  159. }
  160. public UniTaskStatus GetStatus(short token)
  161. {
  162. return core.GetStatus(token);
  163. }
  164. public UniTaskStatus UnsafeGetStatus()
  165. {
  166. return core.UnsafeGetStatus();
  167. }
  168. public void OnCompleted(Action<object> continuation, object state, short token)
  169. {
  170. core.OnCompleted(continuation, state, token);
  171. }
  172. public bool MoveNext()
  173. {
  174. if (cancellationToken.IsCancellationRequested)
  175. {
  176. core.TrySetCanceled(cancellationToken);
  177. return false;
  178. }
  179. try
  180. {
  181. if (predicate())
  182. {
  183. return true;
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. core.TrySetException(ex);
  189. return false;
  190. }
  191. core.TrySetResult(null);
  192. return false;
  193. }
  194. bool TryReturn()
  195. {
  196. TaskTracker.RemoveTracking(this);
  197. core.Reset();
  198. predicate = default;
  199. cancellationToken = default;
  200. return pool.TryPush(this);
  201. }
  202. }
  203. sealed class WaitUntilCanceledPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitUntilCanceledPromise>
  204. {
  205. static TaskPool<WaitUntilCanceledPromise> pool;
  206. WaitUntilCanceledPromise nextNode;
  207. public ref WaitUntilCanceledPromise NextNode => ref nextNode;
  208. static WaitUntilCanceledPromise()
  209. {
  210. TaskPool.RegisterSizeGetter(typeof(WaitUntilCanceledPromise), () => pool.Size);
  211. }
  212. CancellationToken cancellationToken;
  213. UniTaskCompletionSourceCore<object> core;
  214. WaitUntilCanceledPromise()
  215. {
  216. }
  217. public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, out short token)
  218. {
  219. if (cancellationToken.IsCancellationRequested)
  220. {
  221. return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
  222. }
  223. if (!pool.TryPop(out var result))
  224. {
  225. result = new WaitUntilCanceledPromise();
  226. }
  227. result.cancellationToken = cancellationToken;
  228. TaskTracker.TrackActiveTask(result, 3);
  229. PlayerLoopHelper.AddAction(timing, result);
  230. token = result.core.Version;
  231. return result;
  232. }
  233. public void GetResult(short token)
  234. {
  235. try
  236. {
  237. core.GetResult(token);
  238. }
  239. finally
  240. {
  241. TryReturn();
  242. }
  243. }
  244. public UniTaskStatus GetStatus(short token)
  245. {
  246. return core.GetStatus(token);
  247. }
  248. public UniTaskStatus UnsafeGetStatus()
  249. {
  250. return core.UnsafeGetStatus();
  251. }
  252. public void OnCompleted(Action<object> continuation, object state, short token)
  253. {
  254. core.OnCompleted(continuation, state, token);
  255. }
  256. public bool MoveNext()
  257. {
  258. if (cancellationToken.IsCancellationRequested)
  259. {
  260. core.TrySetResult(null);
  261. return false;
  262. }
  263. return true;
  264. }
  265. bool TryReturn()
  266. {
  267. TaskTracker.RemoveTracking(this);
  268. core.Reset();
  269. cancellationToken = default;
  270. return pool.TryPush(this);
  271. }
  272. }
  273. // where T : UnityEngine.Object, can not add constraint
  274. sealed class WaitUntilValueChangedUnityObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, ITaskPoolNode<WaitUntilValueChangedUnityObjectPromise<T, U>>
  275. {
  276. static TaskPool<WaitUntilValueChangedUnityObjectPromise<T, U>> pool;
  277. WaitUntilValueChangedUnityObjectPromise<T, U> nextNode;
  278. public ref WaitUntilValueChangedUnityObjectPromise<T, U> NextNode => ref nextNode;
  279. static WaitUntilValueChangedUnityObjectPromise()
  280. {
  281. TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedUnityObjectPromise<T, U>), () => pool.Size);
  282. }
  283. T target;
  284. UnityEngine.Object targetAsUnityObject;
  285. U currentValue;
  286. Func<T, U> monitorFunction;
  287. IEqualityComparer<U> equalityComparer;
  288. CancellationToken cancellationToken;
  289. UniTaskCompletionSourceCore<U> core;
  290. WaitUntilValueChangedUnityObjectPromise()
  291. {
  292. }
  293. public static IUniTaskSource<U> Create(T target, Func<T, U> monitorFunction, IEqualityComparer<U> equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
  294. {
  295. if (cancellationToken.IsCancellationRequested)
  296. {
  297. return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
  298. }
  299. if (!pool.TryPop(out var result))
  300. {
  301. result = new WaitUntilValueChangedUnityObjectPromise<T, U>();
  302. }
  303. result.target = target;
  304. result.targetAsUnityObject = target as UnityEngine.Object;
  305. result.monitorFunction = monitorFunction;
  306. result.currentValue = monitorFunction(target);
  307. result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
  308. result.cancellationToken = cancellationToken;
  309. TaskTracker.TrackActiveTask(result, 3);
  310. PlayerLoopHelper.AddAction(timing, result);
  311. token = result.core.Version;
  312. return result;
  313. }
  314. public U GetResult(short token)
  315. {
  316. try
  317. {
  318. return core.GetResult(token);
  319. }
  320. finally
  321. {
  322. TryReturn();
  323. }
  324. }
  325. void IUniTaskSource.GetResult(short token)
  326. {
  327. GetResult(token);
  328. }
  329. public UniTaskStatus GetStatus(short token)
  330. {
  331. return core.GetStatus(token);
  332. }
  333. public UniTaskStatus UnsafeGetStatus()
  334. {
  335. return core.UnsafeGetStatus();
  336. }
  337. public void OnCompleted(Action<object> continuation, object state, short token)
  338. {
  339. core.OnCompleted(continuation, state, token);
  340. }
  341. public bool MoveNext()
  342. {
  343. if (cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel.
  344. {
  345. core.TrySetCanceled(cancellationToken);
  346. return false;
  347. }
  348. U nextValue = default(U);
  349. try
  350. {
  351. nextValue = monitorFunction(target);
  352. if (equalityComparer.Equals(currentValue, nextValue))
  353. {
  354. return true;
  355. }
  356. }
  357. catch (Exception ex)
  358. {
  359. core.TrySetException(ex);
  360. return false;
  361. }
  362. core.TrySetResult(nextValue);
  363. return false;
  364. }
  365. bool TryReturn()
  366. {
  367. TaskTracker.RemoveTracking(this);
  368. core.Reset();
  369. target = default;
  370. currentValue = default;
  371. monitorFunction = default;
  372. equalityComparer = default;
  373. cancellationToken = default;
  374. return pool.TryPush(this);
  375. }
  376. }
  377. sealed class WaitUntilValueChangedStandardObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, ITaskPoolNode<WaitUntilValueChangedStandardObjectPromise<T, U>>
  378. where T : class
  379. {
  380. static TaskPool<WaitUntilValueChangedStandardObjectPromise<T, U>> pool;
  381. WaitUntilValueChangedStandardObjectPromise<T, U> nextNode;
  382. public ref WaitUntilValueChangedStandardObjectPromise<T, U> NextNode => ref nextNode;
  383. static WaitUntilValueChangedStandardObjectPromise()
  384. {
  385. TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedStandardObjectPromise<T, U>), () => pool.Size);
  386. }
  387. WeakReference<T> target;
  388. U currentValue;
  389. Func<T, U> monitorFunction;
  390. IEqualityComparer<U> equalityComparer;
  391. CancellationToken cancellationToken;
  392. UniTaskCompletionSourceCore<U> core;
  393. WaitUntilValueChangedStandardObjectPromise()
  394. {
  395. }
  396. public static IUniTaskSource<U> Create(T target, Func<T, U> monitorFunction, IEqualityComparer<U> equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
  397. {
  398. if (cancellationToken.IsCancellationRequested)
  399. {
  400. return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
  401. }
  402. if (!pool.TryPop(out var result))
  403. {
  404. result = new WaitUntilValueChangedStandardObjectPromise<T, U>();
  405. }
  406. result.target = new WeakReference<T>(target, false); // wrap in WeakReference.
  407. result.monitorFunction = monitorFunction;
  408. result.currentValue = monitorFunction(target);
  409. result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
  410. result.cancellationToken = cancellationToken;
  411. TaskTracker.TrackActiveTask(result, 3);
  412. PlayerLoopHelper.AddAction(timing, result);
  413. token = result.core.Version;
  414. return result;
  415. }
  416. public U GetResult(short token)
  417. {
  418. try
  419. {
  420. return core.GetResult(token);
  421. }
  422. finally
  423. {
  424. TryReturn();
  425. }
  426. }
  427. void IUniTaskSource.GetResult(short token)
  428. {
  429. GetResult(token);
  430. }
  431. public UniTaskStatus GetStatus(short token)
  432. {
  433. return core.GetStatus(token);
  434. }
  435. public UniTaskStatus UnsafeGetStatus()
  436. {
  437. return core.UnsafeGetStatus();
  438. }
  439. public void OnCompleted(Action<object> continuation, object state, short token)
  440. {
  441. core.OnCompleted(continuation, state, token);
  442. }
  443. public bool MoveNext()
  444. {
  445. if (cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t)) // doesn't find = cancel.
  446. {
  447. core.TrySetCanceled(cancellationToken);
  448. return false;
  449. }
  450. U nextValue = default(U);
  451. try
  452. {
  453. nextValue = monitorFunction(t);
  454. if (equalityComparer.Equals(currentValue, nextValue))
  455. {
  456. return true;
  457. }
  458. }
  459. catch (Exception ex)
  460. {
  461. core.TrySetException(ex);
  462. return false;
  463. }
  464. core.TrySetResult(nextValue);
  465. return false;
  466. }
  467. bool TryReturn()
  468. {
  469. TaskTracker.RemoveTracking(this);
  470. core.Reset();
  471. target = default;
  472. currentValue = default;
  473. monitorFunction = default;
  474. equalityComparer = default;
  475. cancellationToken = default;
  476. return pool.TryPush(this);
  477. }
  478. }
  479. }
  480. }