MoveNextSource.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. namespace Cysharp.Threading.Tasks
  3. {
  4. public abstract class MoveNextSource : IUniTaskSource<bool>
  5. {
  6. protected UniTaskCompletionSourceCore<bool> completionSource;
  7. public bool GetResult(short token)
  8. {
  9. return completionSource.GetResult(token);
  10. }
  11. public UniTaskStatus GetStatus(short token)
  12. {
  13. return completionSource.GetStatus(token);
  14. }
  15. public void OnCompleted(Action<object> continuation, object state, short token)
  16. {
  17. completionSource.OnCompleted(continuation, state, token);
  18. }
  19. public UniTaskStatus UnsafeGetStatus()
  20. {
  21. return completionSource.UnsafeGetStatus();
  22. }
  23. void IUniTaskSource.GetResult(short token)
  24. {
  25. completionSource.GetResult(token);
  26. }
  27. protected bool TryGetResult<T>(UniTask<T>.Awaiter awaiter, out T result)
  28. {
  29. try
  30. {
  31. result = awaiter.GetResult();
  32. return true;
  33. }
  34. catch (Exception ex)
  35. {
  36. completionSource.TrySetException(ex);
  37. result = default;
  38. return false;
  39. }
  40. }
  41. protected bool TryGetResult(UniTask.Awaiter awaiter)
  42. {
  43. try
  44. {
  45. awaiter.GetResult();
  46. return true;
  47. }
  48. catch (Exception ex)
  49. {
  50. completionSource.TrySetException(ex);
  51. return false;
  52. }
  53. }
  54. }
  55. }