From dbfacece2c4fdeeb604cc3be8edb1dca90bd9ca5 Mon Sep 17 00:00:00 2001
From: Nilay Majorwar <nilaymajorwar@gmail.com>
Date: Thu, 20 Jan 2022 15:07:06 +0530
Subject: [PATCH] Revert to while-loop based execution

---
 engines/execution-controller.ts | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/engines/execution-controller.ts b/engines/execution-controller.ts
index 755f45c..9e3ef5c 100644
--- a/engines/execution-controller.ts
+++ b/engines/execution-controller.ts
@@ -15,7 +15,6 @@ class ExecutionController<RS> {
   private _breakpoints: number[] = [];
   private _result: StepExecutionResult<RS> | null;
   private _resolvePause: (() => void) | null = null;
-  private _execInterval: NodeJS.Timeout | null = null;
   private _isPaused: boolean = false;
 
   /**
@@ -105,20 +104,21 @@ class ExecutionController<RS> {
    * Execute the loaded program until stopped.
    * @param param0.interval Interval between two execution steps
    * @param param0.onResult Callback called with result on each execution step
+   * @returns Promise that resolves with result of last execution step
    */
   executeAll({ interval, onResult }: ExecuteAllArgs<RS>) {
     // Clear paused state
     this._isPaused = false;
 
-    // Run execution loop using an Interval
-    this._execInterval = setInterval(() => {
-      const doBreak = this.runExecLoopIteration();
-      onResult(this._result!);
-      if (doBreak) {
-        clearInterval(this._execInterval!);
-        this._execInterval = null;
+    return new Promise(async (resolve) => {
+      while (true) {
+        const doBreak = this.runExecLoopIteration();
+        onResult(this._result!);
+        if (doBreak) break;
+        await this.sleep(interval);
       }
-    }, interval);
+      resolve(this._result!);
+    });
   }
 
   /**
@@ -151,6 +151,11 @@ class ExecutionController<RS> {
 
     return false;
   }
+
+  /** Sleep for `ms` milliseconds */
+  private sleep(ms: number): Promise<void> {
+    return new Promise((resolve) => setTimeout(resolve, ms));
+  }
 }
 
 export default ExecutionController;