diff --git a/src/blocks/scratch3_event.js b/src/blocks/scratch3_event.js index 10ceb10cc3..1e50b4b981 100644 --- a/src/blocks/scratch3_event.js +++ b/src/blocks/scratch3_event.js @@ -83,9 +83,7 @@ class Scratch3EventBlocks { args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name); if (broadcastVar) { const broadcastOption = broadcastVar.name; - util.startHats('event_whenbroadcastreceived', { - BROADCAST_OPTION: broadcastOption - }); + this._broadcast(broadcastOption, util); } } @@ -99,11 +97,7 @@ class Scratch3EventBlocks { // Have we run before, starting threads? if (!util.stackFrame.startedThreads) { // No - start hats for this broadcast. - util.stackFrame.startedThreads = util.startHats( - 'event_whenbroadcastreceived', { - BROADCAST_OPTION: broadcastOption - } - ); + util.stackFrame.startedThreads = this._broadcast(broadcastOption, util); if (util.stackFrame.startedThreads.length === 0) { // Nothing was started. return; @@ -132,6 +126,14 @@ class Scratch3EventBlocks { } } } + + _broadcast (broadcastOption, util) { // used by compiler + const threads = util.startHats('event_whenbroadcastreceived', { + BROADCAST_OPTION: broadcastOption + }); + this.runtime.emit('BROADCAST_SENT', broadcastOption); + return threads; + } } module.exports = Scratch3EventBlocks; diff --git a/src/compiler/jsgen.js b/src/compiler/jsgen.js index ff07f3f0d5..4b8c180f17 100644 --- a/src/compiler/jsgen.js +++ b/src/compiler/jsgen.js @@ -910,11 +910,11 @@ class JSGenerator { break; case 'event.broadcast': - this.source += `startHats("event_whenbroadcastreceived", { BROADCAST_OPTION: ${this.descendInput(node.broadcast).asString()} });\n`; + this.source += `runtime.ext_scratch3_event._broadcast(${this.descendInput(node.broadcast).asString()}, {startHats});\n`; this.resetVariableInputs(); break; case 'event.broadcastAndWait': - this.source += `yield* waitThreads(startHats("event_whenbroadcastreceived", { BROADCAST_OPTION: ${this.descendInput(node.broadcast).asString()} }));\n`; + this.source += `yield* waitThreads(runtime.ext_scratch3_event._broadcast(${this.descendInput(node.broadcast).asString()}, {startHats}));\n`; this.yielded(); break;