Browse Source

Add tickRate option

This option, if given, will sleep for the given number of milliseconds
between each call to Execute.
ApisNecros 1 year ago
parent
commit
01e6b9abf6
1 changed files with 10 additions and 3 deletions
  1. 10 3
      IntComp/Computer.js

+ 10 - 3
IntComp/Computer.js

@@ -14,6 +14,7 @@ module.exports = class Computer {
      * @param {number[]} stack The initial memory to load into the computer
      * @param {Object} options Options that can be enabled within the computer
      * @param {boolean} options.followPointer When true, the memory will be dumped every call to Execute with the current instruction highlighted
+     * @param {number} options.tickRate The number of milliseconds between calls to Execute. Initializes to 0.
      */
     constructor(stack, options = {}) {
         this._initialMemory = DeepClone(stack);
@@ -42,6 +43,7 @@ module.exports = class Computer {
 
         this.options = {
             followPointer: options.followPointer ?? false,
+            tickRate: options.tickRate ?? 0,
         };
     }
 
@@ -52,9 +54,14 @@ module.exports = class Computer {
      * encountered, or an error is thrown.
      * @returns {void}
      */
-    Run() {
-        // eslint-disable-next-line no-empty
-        while (this.Execute(this.stack.Get(ComputerParameterMode.IMMEDIATE_MODE)) === true) { }
+    async Run() {
+        while (this.Execute(this.stack.Get(ComputerParameterMode.IMMEDIATE_MODE)) === true) {
+            if (this.options.tickRate) {
+                // Sleep
+                // eslint-disable-next-line no-await-in-loop, no-promise-executor-return, arrow-parens
+                await new Promise(resolve => setTimeout(resolve, this.options.tickRate));
+            }
+        }
     }
 
     /**