Merge pull request #1218 from technoboy10/bugfix/gh-1165-monitor-import

Fix importing hidden monitors from Scratch 2 projects
This commit is contained in:
Connor Hudson 2018-06-11 10:27:45 -04:00 committed by GitHub
commit 244ffb38c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 16 deletions

View file

@ -522,8 +522,10 @@ class Blocks {
block.targetId = isSpriteSpecific ? optRuntime.getEditingTarget().id : null; block.targetId = isSpriteSpecific ? optRuntime.getEditingTarget().id : null;
if (wasMonitored && !block.isMonitored) { if (wasMonitored && !block.isMonitored) {
optRuntime.requestRemoveMonitor(block.id); optRuntime.requestHideMonitor(block.id);
} else if (!wasMonitored && block.isMonitored) { } else if (!wasMonitored && block.isMonitored) {
// Tries to show the monitor for specified block. If it doesn't exist, add the monitor.
if (!optRuntime.requestShowMonitor(block.id)) {
optRuntime.requestAddMonitor(MonitorRecord({ optRuntime.requestAddMonitor(MonitorRecord({
id: block.id, id: block.id,
targetId: block.targetId, targetId: block.targetId,
@ -535,6 +537,7 @@ class Blocks {
mode: block.opcode === 'data_listcontents' ? 'list' : 'default' mode: block.opcode === 'data_listcontents' ? 'list' : 'default'
})); }));
} }
}
break; break;
} }
} }

View file

@ -1518,26 +1518,38 @@ class Runtime extends EventEmitter {
/** /**
* Add a monitor to the state. If the monitor already exists in the state, * Add a monitor to the state. If the monitor already exists in the state,
* overwrites it. * updates those properties that are defined in the given monitor record.
* @param {!MonitorRecord} monitor Monitor to add. * @param {!MonitorRecord} monitor Monitor to add.
*/ */
requestAddMonitor (monitor) { requestAddMonitor (monitor) {
this._monitorState = this._monitorState.set(monitor.get('id'), monitor); const id = monitor.get('id');
if (!this.requestUpdateMonitor(monitor)) { // update monitor if it exists in the state
// if the monitor did not exist in the state, add it
this._monitorState = this._monitorState.set(id, monitor);
}
} }
/** /**
* Update a monitor in the state. Does nothing if the monitor does not already * Update a monitor in the state and report success/failure of update.
* exist in the state.
* @param {!Map} monitor Monitor values to update. Values on the monitor with overwrite * @param {!Map} monitor Monitor values to update. Values on the monitor with overwrite
* values on the old monitor with the same ID. If a value isn't defined on the new monitor, * values on the old monitor with the same ID. If a value isn't defined on the new monitor,
* the old monitor will keep its old value. * the old monitor will keep its old value.
* @return {boolean} true if monitor exists in the state and was updated, false if it did not exist.
*/ */
requestUpdateMonitor (monitor) { requestUpdateMonitor (monitor) {
const id = monitor.get('id'); const id = monitor.get('id');
if (this._monitorState.has(id)) { if (this._monitorState.has(id)) {
this._monitorState = this._monitorState =
this._monitorState.set(id, this._monitorState.get(id).merge(monitor)); // Use mergeWith here to prevent undefined values from overwriting existing ones
this._monitorState.set(id, this._monitorState.get(id).mergeWith((prev, next) => {
if (typeof next === 'undefined' || next === null) {
return prev;
} }
return next;
}, monitor));
return true;
}
return false;
} }
/** /**
@ -1549,6 +1561,31 @@ class Runtime extends EventEmitter {
this._monitorState = this._monitorState.delete(monitorId); this._monitorState = this._monitorState.delete(monitorId);
} }
/**
* Hides a monitor and returns success/failure of action.
* @param {!string} monitorId ID of the monitor to hide.
* @return {boolean} true if monitor exists and was updated, false otherwise
*/
requestHideMonitor (monitorId) {
return this.requestUpdateMonitor(new Map([
['id', monitorId],
['visible', false]
]));
}
/**
* Shows a monitor and returns success/failure of action.
* not exist in the state.
* @param {!string} monitorId ID of the monitor to show.
* @return {boolean} true if monitor exists and was updated, false otherwise
*/
requestShowMonitor (monitorId) {
return this.requestUpdateMonitor(new Map([
['id', monitorId],
['visible', true]
]));
}
/** /**
* Removes all monitors with the given target ID from the state. Does nothing if * Removes all monitors with the given target ID from the state. Does nothing if
* the monitor already does not exist in the state. * the monitor already does not exist in the state.