mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-25 07:22:33 -05:00
Consolidate some logic in requestAddMonitor and requestUpdateMonitor
This commit is contained in:
parent
99dc46ba80
commit
2cb276c476
1 changed files with 6 additions and 10 deletions
|
@ -1523,25 +1523,19 @@ class Runtime extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
requestAddMonitor (monitor) {
|
requestAddMonitor (monitor) {
|
||||||
const id = monitor.get('id');
|
const id = monitor.get('id');
|
||||||
if (this._monitorState.has(id)) {
|
if (!this.requestUpdateMonitor(monitor)) { // update monitor if it exists in the state
|
||||||
// Use mergeWith here to prevent undefined values from overwriting existing ones
|
// if the monitor did not exist in the state, add it
|
||||||
this._monitorState = this._monitorState.set(id, this._monitorState.get(id).mergeWith((prev, next) => {
|
|
||||||
if (typeof next === 'undefined' || next === null) {
|
|
||||||
return prev;
|
|
||||||
}
|
|
||||||
return next;
|
|
||||||
}, monitor));
|
|
||||||
} else {
|
|
||||||
this._monitorState = this._monitorState.set(id, monitor);
|
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. Does nothing and returns false if the monitor does not already
|
||||||
* exist in the state.
|
* 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.
|
||||||
|
* @returns {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');
|
||||||
|
@ -1554,7 +1548,9 @@ class Runtime extends EventEmitter {
|
||||||
}
|
}
|
||||||
return next;
|
return next;
|
||||||
}, monitor));
|
}, monitor));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue