From b1355c6b0b6ac4c9ade1a9fc547eb42740fb18a2 Mon Sep 17 00:00:00 2001 From: DD Liu Date: Wed, 10 May 2017 15:47:06 -0400 Subject: [PATCH] Fire remove monitors VM event --- src/engine/blocks.js | 9 +++++++-- src/engine/runtime.js | 18 +++++++++++++++++- src/virtual-machine.js | 7 +++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index fb6634838..d0d2f82d0 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -215,7 +215,7 @@ class Blocks { element: e.element, name: e.name, value: e.newValue - }); + }, optRuntime); break; case 'move': this.moveBlock({ @@ -270,11 +270,13 @@ class Blocks { /** * Block management: change block field values * @param {!object} args Blockly change event to be processed + * @param {?Runtime} optRuntime Optional runtime to allow changeBlock to emit actions. */ - changeBlock (args) { + changeBlock (args, optRuntime) { // Validate if (['field', 'mutation', 'checkbox'].indexOf(args.element) === -1) return; const block = this._blocks[args.id]; + let wasMonitored = block.isMonitored; if (typeof block === 'undefined') return; switch (args.element) { @@ -288,6 +290,9 @@ class Blocks { break; case 'checkbox': block.isMonitored = args.value; + if (optRuntime && wasMonitored && !block.isMonitored) { + optRuntime.removeMonitors([{id: block.id}]); + } break; } } diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 17690ee7f..d94b53c0f 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -241,6 +241,14 @@ class Runtime extends EventEmitter { return 'MONITORS_UPDATE'; } + /** + * Event name for monitors removed. + * @const {string} + */ + static get MONITORS_REMOVED () { + return 'MONITORS_REMOVED'; + } + /** * How rapidly we try to step threads by default, in ms. */ @@ -859,12 +867,20 @@ class Runtime extends EventEmitter { /** * Emit a monitor update which adds or updates if exists the given monitors. - * @param {!Array} monitors Array of all monitors + * @param {!Array} monitors Array of monitors to update. */ updateMonitors (monitors) { this.emit(Runtime.MONITORS_UPDATE, monitors); } + /** + * Emit a monitor update which removes if exists the given monitors. + * @param {!Array} monitors Array of monitors to remove. + */ + removeMonitors (monitors) { + this.emit(Runtime.MONITORS_REMOVED, monitors); + } + /** * Get a target by its id. * @param {string} targetId Id of target to find. diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 0e5048f52..953336d84 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -55,8 +55,11 @@ class VirtualMachine extends EventEmitter { instance.runtime.on(Runtime.SPRITE_INFO_REPORT, spriteInfo => { instance.emit(Runtime.SPRITE_INFO_REPORT, spriteInfo); }); - instance.runtime.on(Runtime.MONITORS_UPDATE, data => { - instance.emit(Runtime.MONITORS_UPDATE, data); + instance.runtime.on(Runtime.MONITORS_UPDATE, monitorList => { + instance.emit(Runtime.MONITORS_UPDATE, monitorList); + }); + instance.runtime.on(Runtime.MONITORS_REMOVED, monitorList => { + instance.emit(Runtime.MONITORS_REMOVED, monitorList); }); this.blockListener = this.blockListener.bind(this);