Promise.cs 4.1 KB

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