Skip to content

Commit 8979eee

Browse files
committed
fix: use block glow to track project run status
This means that: - Monitor blocks don't count, because they don't affect block glow (the monitor block container has forceNoGlow set) - Edge-activated hats with no stacks attached don't count (the block to glow is null) - Edge-activated hats whose predicates are false don't count (they're evaluated outside stepThreads so block glow isn't set to true) Basically, anything which would cause blocks to glow will cause the project to be considered "running".
1 parent de7a3ed commit 8979eee

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/engine/runtime.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class Runtime extends EventEmitter {
248248
this._scriptGlowsPreviousFrame = [];
249249

250250
/**
251-
* Whether any non-monitor threads ran during the previous frame.
251+
* Whether the project counted as "running" during the previous frame.
252252
* @type {boolean}
253253
*/
254254
this._projectRanLastFrame = false;
@@ -1636,13 +1636,16 @@ class Runtime extends EventEmitter {
16361636
* @param {?object} opts optional arguments
16371637
* @param {?boolean} opts.stackClick true if the script was activated by clicking on the stack
16381638
* @param {?boolean} opts.updateMonitor true if the script should update a monitor value
1639+
* @param {?boolean} opts.edgeActivated true if the script's top block is an edge-activated hat block
16391640
* @return {!Thread} The newly created thread.
16401641
*/
16411642
_pushThread (id, target, opts) {
16421643
const thread = new Thread(id);
16431644
thread.target = target;
1645+
// TODO: make these mutually exclusive (convert to an enum?)
16441646
thread.stackClick = Boolean(opts && opts.stackClick);
16451647
thread.updateMonitor = Boolean(opts && opts.updateMonitor);
1648+
thread.edgeActivated = Boolean(opts && opts.edgeActivated);
16461649
thread.blockContainer = thread.updateMonitor ?
16471650
this.monitorBlocks :
16481651
target.blocks;
@@ -1817,6 +1820,7 @@ class Runtime extends EventEmitter {
18171820
const newThreads = [];
18181821
// Look up metadata for the relevant hat.
18191822
const hatMeta = instance._hats[requestedHatOpcode];
1823+
const edgeActivated = Boolean(hatMeta.edgeActivated);
18201824

18211825
for (const opts in optMatchFields) {
18221826
if (!optMatchFields.hasOwnProperty(opts)) continue;
@@ -1869,7 +1873,7 @@ class Runtime extends EventEmitter {
18691873
}
18701874
}
18711875
// Start the thread with this top block.
1872-
newThreads.push(this._pushThread(topBlockId, target));
1876+
newThreads.push(this._pushThread(topBlockId, target, {edgeActivated}));
18731877
}, optTarget);
18741878
// For compatibility with Scratch 2, edge triggered hats need to be processed before
18751879
// threads are stepped. See ScratchRuntime.as for original implementation
@@ -2094,11 +2098,15 @@ class Runtime extends EventEmitter {
20942098
}
20952099
this._updateGlows(doneThreads);
20962100

2097-
const threadNonMonitor = thread => !thread.updateMonitor;
2101+
// Threads count as "running" if a block glowed in the thread this frame.
2102+
// This excludes edge-activated hat predicates and monitor blocks.
2103+
const threadCountsTowardsRunStatus = thread => thread.requestScriptGlowInFrame &&
2104+
thread.blockGlowInFrame !== null;
20982105
// Add done threads so that even if a thread finishes within 1 frame, the green
20992106
// flag will still indicate that a script ran.
2100-
const anyNonMonitorThreadsRunning = this.threads.some(threadNonMonitor) || doneThreads.some(threadNonMonitor);
2101-
this._emitProjectRunStatus(anyNonMonitorThreadsRunning);
2107+
const anyThreadsRunning = this.threads.some(threadCountsTowardsRunStatus) ||
2108+
doneThreads.some(threadCountsTowardsRunStatus);
2109+
this._emitProjectRunStatus(anyThreadsRunning);
21022110
// Store threads that completed this iteration for testing and other
21032111
// internal purposes.
21042112
this._lastStepDoneThreads = doneThreads;

0 commit comments

Comments
 (0)