Promise.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2025 TerraByte Inc.
  2. //
  3. // The concrete implementation of the IPromise interface.
  4. // It manages the state and execution of an asynchronous operation.
  5. using System;
  6. using System.Threading;
  7. using UnityEngine.Scripting;
  8. namespace Terra.Arbitrator.Promises
  9. {
  10. [Preserve]
  11. public class Promise<TResult> : IPromise<TResult>, ITrackablePromise
  12. {
  13. private PromiseState State { get; set; }
  14. private TResult _resolvedValue;
  15. private Exception _rejectedException;
  16. private Action<TResult> _onResolvedCallback;
  17. private Action<Exception> _onRejectedCallback;
  18. private Action _onFinallyCallback;
  19. private volatile bool _isResolved;
  20. private volatile bool _isRejected;
  21. /// <summary>
  22. /// Creates a new promise and starts its execution on a background thread.
  23. /// </summary>
  24. /// <param name="executor">The function to execute, which takes resolve and reject actions.</param>
  25. public Promise(Action<Action<TResult>, Action<Exception>> executor)
  26. {
  27. _isRejected = false;
  28. State = PromiseState.Pending;
  29. // The manager will track this promise until it's settled.
  30. PromiseManager.Track(this);
  31. ThreadPool.QueueUserWorkItem(_ =>
  32. {
  33. try
  34. {
  35. executor(Resolve, Reject);
  36. }
  37. catch (Exception ex)
  38. {
  39. Reject(ex);
  40. }
  41. });
  42. }
  43. private void Resolve(TResult value)
  44. {
  45. if (State != PromiseState.Pending) return;
  46. _resolvedValue = value;
  47. State = PromiseState.Resolved;
  48. _isResolved = true; // Signal that we are done
  49. }
  50. private void Reject(Exception ex)
  51. {
  52. if (State != PromiseState.Pending) return;
  53. _rejectedException = ex;
  54. State = PromiseState.Rejected;
  55. _isRejected = true; // Signal that we are done
  56. }
  57. public IPromise<TResult> Then(Action<TResult> onResolved)
  58. {
  59. _onResolvedCallback = onResolved;
  60. return this;
  61. }
  62. public IPromise<TNewResult> Then<TNewResult>(Func<TResult, IPromise<TNewResult>> onResolved)
  63. {
  64. return new Promise<TNewResult>(LocalExecutor);
  65. void LocalExecutor(Action<TNewResult> resolve, Action<Exception> reject)
  66. {
  67. Then(OnResolved).Catch(reject);
  68. return;
  69. void OnResolved(TResult result)
  70. {
  71. try
  72. {
  73. var nextPromise = onResolved(result);
  74. nextPromise.Then(resolve).Catch(reject);
  75. }
  76. catch (Exception ex)
  77. {
  78. reject(ex);
  79. }
  80. }
  81. }
  82. }
  83. public IPromise<TResult> Catch(Action<Exception> onRejected)
  84. {
  85. _onRejectedCallback = onRejected;
  86. return this;
  87. }
  88. public void Finally(Action onFinally)
  89. {
  90. _onFinallyCallback = onFinally;
  91. }
  92. /// <summary>
  93. /// Called by the PromiseManager from the main thread to execute callbacks.
  94. /// </summary>
  95. /// <returns>True if the promise is settled, otherwise false.</returns>
  96. public bool Tick()
  97. {
  98. if (_isResolved)
  99. {
  100. try
  101. {
  102. _onResolvedCallback?.Invoke(_resolvedValue);
  103. }
  104. finally
  105. {
  106. _onFinallyCallback?.Invoke();
  107. }
  108. return true;
  109. }
  110. if (_isRejected)
  111. {
  112. try
  113. {
  114. _onRejectedCallback?.Invoke(_rejectedException);
  115. }
  116. finally
  117. {
  118. _onFinallyCallback?.Invoke();
  119. }
  120. return true;
  121. }
  122. return false;
  123. }
  124. }
  125. }