From 65ee6b7cacc829545c6fd6ea732f23c2bfb6a6a9 Mon Sep 17 00:00:00 2001
From: "Michael \"Z\" Goddard" <mzgoddard@gmail.com>
Date: Fri, 5 Oct 2018 14:38:44 -0400
Subject: [PATCH] broadcast and wait must wait for threads to be removed from
 runtime

---
 src/blocks/scratch3_event.js | 7 ++++++-
 src/blocks/scratch3_looks.js | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/blocks/scratch3_event.js b/src/blocks/scratch3_event.js
index 4da1dfae3..ae105c6b5 100644
--- a/src/blocks/scratch3_event.js
+++ b/src/blocks/scratch3_event.js
@@ -109,7 +109,12 @@ class Scratch3EventBlocks {
             }
             // We've run before; check if the wait is still going on.
             const instance = this;
-            const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
+            // Scratch 2 considers threads to be waiting if they are still in
+            // runtime.threads. Threads that have run all their blocks, or are
+            // marked done but still in runtime.threads are still considered to
+            // be waiting.
+            const waiting = util.stackFrame.startedThreads
+                .some(thread => instance.runtime.threads.indexOf(thread) !== -1);
             if (waiting) {
                 // If all threads are waiting for the next tick or later yield
                 // for a tick as well. Otherwise yield until the next loop of
diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js
index 373083afd..10045bf7d 100644
--- a/src/blocks/scratch3_looks.js
+++ b/src/blocks/scratch3_looks.js
@@ -415,7 +415,12 @@ class Scratch3LooksBlocks {
         }
         // We've run before; check if the wait is still going on.
         const instance = this;
-        const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
+        // Scratch 2 considers threads to be waiting if they are still in
+        // runtime.threads. Threads that have run all their blocks, or are
+        // marked done but still in runtime.threads are still considered to
+        // be waiting.
+        const waiting = util.stackFrame.startedThreads
+            .some(thread => instance.runtime.threads.indexOf(thread) !== -1);
         if (waiting) {
             // If all threads are waiting for the next tick or later yield
             // for a tick as well. Otherwise yield until the next loop of