PromiseTimer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ZenFulcrum.EmbeddedBrowser
  6. {
  7. /// <summary>
  8. /// A class that wraps a pending promise with it's predicate and time data
  9. /// </summary>
  10. internal class PredicateWait
  11. {
  12. /// <summary>
  13. /// Predicate for resolving the promise
  14. /// </summary>
  15. public Func<TimeData, bool> predicate;
  16. /// <summary>
  17. /// The time the promise was started
  18. /// </summary>
  19. public float timeStarted;
  20. /// <summary>
  21. /// The pending promise which is an interface for a promise that can be rejected or resolved.
  22. /// </summary>
  23. public IPendingPromise pendingPromise;
  24. /// <summary>
  25. /// The time data specific to this pending promise. Includes elapsed time and delta time.
  26. /// </summary>
  27. public TimeData timeData;
  28. }
  29. /// <summary>
  30. /// Time data specific to a particular pending promise.
  31. /// </summary>
  32. public struct TimeData
  33. {
  34. /// <summary>
  35. /// The amount of time that has elapsed since the pending promise started running
  36. /// </summary>
  37. public float elapsedTime;
  38. /// <summary>
  39. /// The amount of time since the last time the pending promise was updated.
  40. /// </summary>
  41. public float deltaTime;
  42. }
  43. public interface IPromiseTimer
  44. {
  45. /// <summary>
  46. /// Resolve the returned promise once the time has elapsed
  47. /// </summary>
  48. IPromise WaitFor(float seconds);
  49. /// <summary>
  50. /// Resolve the returned promise once the predicate evaluates to true
  51. /// </summary>
  52. IPromise WaitUntil(Func<TimeData, bool> predicate);
  53. /// <summary>
  54. /// Resolve the returned promise once the predicate evaluates to false
  55. /// </summary>
  56. IPromise WaitWhile(Func<TimeData, bool> predicate);
  57. /// <summary>
  58. /// Update all pending promises. Must be called for the promises to progress and resolve at all.
  59. /// </summary>
  60. void Update(float deltaTime);
  61. }
  62. public class PromiseTimer : IPromiseTimer
  63. {
  64. /// <summary>
  65. /// The current running total for time that this PromiseTimer has run for
  66. /// </summary>
  67. private float curTime;
  68. /// <summary>
  69. /// Currently pending promises
  70. /// </summary>
  71. private List<PredicateWait> waiting = new List<PredicateWait>();
  72. /// <summary>
  73. /// Resolve the returned promise once the time has elapsed
  74. /// </summary>
  75. public IPromise WaitFor(float seconds)
  76. {
  77. return WaitUntil(t => t.elapsedTime >= seconds);
  78. }
  79. /// <summary>
  80. /// Resolve the returned promise once the predicate evaluates to false
  81. /// </summary>
  82. public IPromise WaitWhile(Func<TimeData, bool> predicate)
  83. {
  84. return WaitUntil(t => !predicate(t));
  85. }
  86. /// <summary>
  87. /// Resolve the returned promise once the predicate evalutes to true
  88. /// </summary>
  89. public IPromise WaitUntil(Func<TimeData, bool> predicate)
  90. {
  91. var promise = new Promise();
  92. var wait = new PredicateWait()
  93. {
  94. timeStarted = curTime,
  95. pendingPromise = promise,
  96. timeData = new TimeData(),
  97. predicate = predicate
  98. };
  99. waiting.Add(wait);
  100. return promise;
  101. }
  102. /// <summary>
  103. /// Update all pending promises. Must be called for the promises to progress and resolve at all.
  104. /// </summary>
  105. public void Update(float deltaTime)
  106. {
  107. curTime += deltaTime;
  108. int i = 0;
  109. while (i < waiting.Count)
  110. {
  111. var wait = waiting[i];
  112. var newElapsedTime = curTime - wait.timeStarted;
  113. wait.timeData.deltaTime = newElapsedTime - wait.timeData.elapsedTime;
  114. wait.timeData.elapsedTime = newElapsedTime;
  115. bool result;
  116. try
  117. {
  118. result = wait.predicate(wait.timeData);
  119. }
  120. catch (Exception ex)
  121. {
  122. wait.pendingPromise.Reject(ex);
  123. waiting.RemoveAt(i);
  124. continue;
  125. }
  126. if (result)
  127. {
  128. wait.pendingPromise.Resolve();
  129. waiting.RemoveAt(i);
  130. }
  131. else
  132. {
  133. i++;
  134. }
  135. }
  136. }
  137. }
  138. }