mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 15:02:52 -05:00
Merge pull request #1211 from mzgoddard/broadcast-and-wait-promise
yield a tick if broadcastandwait is waiting on threads waiting for a future tick
This commit is contained in:
commit
f5234c777a
5 changed files with 44 additions and 8 deletions
|
@ -109,10 +109,20 @@ class Scratch3EventBlocks {
|
||||||
const instance = this;
|
const instance = this;
|
||||||
const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
|
const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
|
||||||
if (waiting) {
|
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
|
||||||
|
// the threads.
|
||||||
|
if (
|
||||||
|
util.stackFrame.startedThreads
|
||||||
|
.every(thread => instance.runtime.isWaitingThread(thread))
|
||||||
|
) {
|
||||||
|
util.yieldTick();
|
||||||
|
} else {
|
||||||
util.yield();
|
util.yield();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = Scratch3EventBlocks;
|
module.exports = Scratch3EventBlocks;
|
||||||
|
|
|
@ -416,9 +416,19 @@ class Scratch3LooksBlocks {
|
||||||
const instance = this;
|
const instance = this;
|
||||||
const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
|
const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
|
||||||
if (waiting) {
|
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
|
||||||
|
// the threads.
|
||||||
|
if (
|
||||||
|
util.stackFrame.startedThreads
|
||||||
|
.every(thread => instance.runtime.isWaitingThread(thread))
|
||||||
|
) {
|
||||||
|
util.yieldTick();
|
||||||
|
} else {
|
||||||
util.yield();
|
util.yield();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
nextBackdrop () {
|
nextBackdrop () {
|
||||||
const stage = this.runtime.getTargetForStage();
|
const stage = this.runtime.getTargetForStage();
|
||||||
|
|
|
@ -231,9 +231,7 @@ class Scratch3SoundBlocks {
|
||||||
util.target.audioPlayer.setEffect(effect, soundState.effects[effect]);
|
util.target.audioPlayer.setEffect(effect, soundState.effects[effect]);
|
||||||
|
|
||||||
// Yield until the next tick.
|
// Yield until the next tick.
|
||||||
return new Promise(resolve => {
|
return Promise.resolve();
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_syncEffectsForTarget (target) {
|
_syncEffectsForTarget (target) {
|
||||||
|
@ -284,9 +282,7 @@ class Scratch3SoundBlocks {
|
||||||
util.target.audioPlayer.setVolume(util.target.volume);
|
util.target.audioPlayer.setVolume(util.target.volume);
|
||||||
|
|
||||||
// Yield until the next tick.
|
// Yield until the next tick.
|
||||||
return new Promise(resolve => {
|
return Promise.resolve();
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getVolume (args, util) {
|
getVolume (args, util) {
|
||||||
|
|
|
@ -57,6 +57,13 @@ class BlockUtility {
|
||||||
this.thread.status = Thread.STATUS_YIELD;
|
this.thread.status = Thread.STATUS_YIELD;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the thread to yield until the next tick of the runtime.
|
||||||
|
*/
|
||||||
|
yieldTick () {
|
||||||
|
this.thread.status = Thread.STATUS_YIELD_TICK;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start a branch in the current block.
|
* Start a branch in the current block.
|
||||||
* @param {number} branchNum Which branch to step to (i.e., 1, 2).
|
* @param {number} branchNum Which branch to step to (i.e., 1, 2).
|
||||||
|
|
|
@ -1017,6 +1017,19 @@ class Runtime extends EventEmitter {
|
||||||
this.threads.indexOf(thread) > -1);
|
this.threads.indexOf(thread) > -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether a thread is waiting for more information or done.
|
||||||
|
* @param {?Thread} thread Thread object to check.
|
||||||
|
* @return {boolean} True if the thread is waiting
|
||||||
|
*/
|
||||||
|
isWaitingThread (thread) {
|
||||||
|
return (
|
||||||
|
thread.status === Thread.STATUS_PROMISE_WAIT ||
|
||||||
|
thread.status === Thread.STATUS_YIELD_TICK ||
|
||||||
|
!this.isActiveThread(thread)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
|
Loading…
Reference in a new issue