Promise.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Callbacks to be executed on the main thread
  15. private Action<TResult> _onResolvedCallback;
  16. private Action<Exception> _onRejectedCallback;
  17. private Action _onFinallyCallback;
  18. // Thread-safe flags
  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<TResult> Catch(Action<Exception> onRejected)
  63. {
  64. _onRejectedCallback = onRejected;
  65. return this;
  66. }
  67. public void Finally(Action onFinally)
  68. {
  69. _onFinallyCallback = onFinally;
  70. }
  71. /// <summary>
  72. /// Called by the PromiseManager from the main thread to execute callbacks.
  73. /// </summary>
  74. /// <returns>True if the promise is settled, otherwise false.</returns>
  75. public bool Tick()
  76. {
  77. if (_isResolved)
  78. {
  79. try
  80. {
  81. _onResolvedCallback?.Invoke(_resolvedValue);
  82. }
  83. finally
  84. {
  85. _onFinallyCallback?.Invoke();
  86. }
  87. return true;
  88. }
  89. if (_isRejected)
  90. {
  91. try
  92. {
  93. _onRejectedCallback?.Invoke(_rejectedException);
  94. }
  95. finally
  96. {
  97. _onFinallyCallback?.Invoke();
  98. }
  99. return true;
  100. }
  101. return false;
  102. }
  103. }
  104. }