Add isActiveThread and simplify broadcast-and-wait accordingly

This commit is contained in:
Tim Mickel 2016-08-29 10:01:31 -04:00
parent 64b82f4dc2
commit 40c90bbcc7
2 changed files with 13 additions and 8 deletions

View file

@ -71,14 +71,10 @@ Scratch3EventBlocks.prototype.broadcastAndWait = function (args, util) {
} }
} }
// We've run before; check if the wait is still going on. // We've run before; check if the wait is still going on.
var waiting = false; var instance = this;
for (var i = 0; i < util.stackFrame.triggeredThreads.length; i++) { var waiting = util.stackFrame.triggeredThreads.some(function(thread) {
var thread = util.stackFrame.triggeredThreads[i]; return instance.runtime.isActiveThread(thread);
var activeThreads = this.runtime.threads; });
if (activeThreads.indexOf(thread) > -1) { // @todo: A cleaner way?
waiting = true;
}
}
if (waiting) { if (waiting) {
util.yieldFrame(); util.yieldFrame();
} }

View file

@ -211,6 +211,15 @@ Runtime.prototype._removeThread = function (thread) {
} }
}; };
/**
* Return whether a thread is currently active/running.
* @param {?Thread} thread Thread object to check.
* @return {Boolean} True if the thread is active/running.
*/
Runtime.prototype.isActiveThread = function (thread) {
return this.threads.indexOf(thread) > -1;
};
/** /**
* Toggle a script. * Toggle a script.
* @param {!string} topBlockId ID of block that starts the script. * @param {!string} topBlockId ID of block that starts the script.