Fire remove monitors VM event

This commit is contained in:
DD Liu 2017-05-10 15:47:06 -04:00
parent 138b4631e1
commit b1355c6b0b
3 changed files with 29 additions and 5 deletions

View file

@ -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;
}
}

View file

@ -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.

View file

@ -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);