Stop emitting project changes within 'runtime._step()'. Emit project changes in scenarios that were previously taking advantage of runtime._step()

This commit is contained in:
Karishma Chadha 2019-01-29 17:43:30 -05:00
parent e276bcc9fd
commit dfcd09fedc
2 changed files with 32 additions and 10 deletions

View file

@ -1770,7 +1770,7 @@ class Runtime extends EventEmitter {
} }
if (this._refreshTargets) { if (this._refreshTargets) {
this.emit(Runtime.TARGETS_UPDATE); this.emit(Runtime.TARGETS_UPDATE, false /* Don't emit project changed */);
this._refreshTargets = false; this._refreshTargets = false;
} }

View file

@ -94,8 +94,8 @@ class VirtualMachine extends EventEmitter {
this.runtime.on(Runtime.VISUAL_REPORT, visualReport => { this.runtime.on(Runtime.VISUAL_REPORT, visualReport => {
this.emit(Runtime.VISUAL_REPORT, visualReport); this.emit(Runtime.VISUAL_REPORT, visualReport);
}); });
this.runtime.on(Runtime.TARGETS_UPDATE, () => { this.runtime.on(Runtime.TARGETS_UPDATE, emitProjectChanged => {
this.emitTargetsUpdate(); this.emitTargetsUpdate(emitProjectChanged);
}); });
this.runtime.on(Runtime.MONITORS_UPDATE, monitorList => { this.runtime.on(Runtime.MONITORS_UPDATE, monitorList => {
this.emit(Runtime.MONITORS_UPDATE, monitorList); this.emit(Runtime.MONITORS_UPDATE, monitorList);
@ -201,7 +201,7 @@ class VirtualMachine extends EventEmitter {
clear () { clear () {
this.runtime.dispose(); this.runtime.dispose();
this.editingTarget = null; this.editingTarget = null;
this.emitTargetsUpdate(); this.emitTargetsUpdate(false /* Don't emit project change */);
} }
/** /**
@ -562,10 +562,12 @@ class VirtualMachine extends EventEmitter {
.then(validatedInput => { .then(validatedInput => {
const projectVersion = validatedInput[0].projectVersion; const projectVersion = validatedInput[0].projectVersion;
if (projectVersion === 2) { if (projectVersion === 2) {
return this._addSprite2(validatedInput[0], validatedInput[1]); return this._addSprite2(validatedInput[0], validatedInput[1])
.then(() => this.runtime.emitProjectChanged());
} }
if (projectVersion === 3) { if (projectVersion === 3) {
return this._addSprite3(validatedInput[0], validatedInput[1]); return this._addSprite3(validatedInput[0], validatedInput[1])
.then(() => this.runtime.emitProjectChanged());
} }
return Promise.reject(`${errorPrefix} Unable to verify sprite version.`); return Promise.reject(`${errorPrefix} Unable to verify sprite version.`);
}) })
@ -627,6 +629,7 @@ class VirtualMachine extends EventEmitter {
target.setCostume( target.setCostume(
target.getCostumes().length - 1 target.getCostumes().length - 1
); );
this.runtime.emitProjectChanged();
}); });
} }
// If the target cannot be found by id, return a rejected promise // If the target cannot be found by id, return a rejected promise
@ -698,6 +701,7 @@ class VirtualMachine extends EventEmitter {
const deletedCostume = this.editingTarget.deleteCostume(costumeIndex); const deletedCostume = this.editingTarget.deleteCostume(costumeIndex);
if (deletedCostume) { if (deletedCostume) {
const target = this.editingTarget; const target = this.editingTarget;
this.runtime.emitProjectChanged();
return () => { return () => {
target.addCostume(deletedCostume); target.addCostume(deletedCostume);
this.emitTargetsUpdate(); this.emitTargetsUpdate();
@ -797,6 +801,7 @@ class VirtualMachine extends EventEmitter {
const target = this.editingTarget; const target = this.editingTarget;
const deletedSound = this.editingTarget.deleteSound(soundIndex); const deletedSound = this.editingTarget.deleteSound(soundIndex);
if (deletedSound) { if (deletedSound) {
this.runtime.emitProjectChanged();
const restoreFun = () => { const restoreFun = () => {
target.addSound(deletedSound); target.addSound(deletedSound);
this.emitTargetsUpdate(); this.emitTargetsUpdate();
@ -928,6 +933,7 @@ class VirtualMachine extends EventEmitter {
const stage = this.runtime.getTargetForStage(); const stage = this.runtime.getTargetForStage();
stage.addCostume(backdropObject); stage.addCostume(backdropObject);
stage.setCostume(stage.getCostumes().length - 1); stage.setCostume(stage.getCostumes().length - 1);
this.runtime.emitProjectChanged();
}); });
} }
@ -958,8 +964,9 @@ class VirtualMachine extends EventEmitter {
const currTarget = allTargets[i]; const currTarget = allTargets[i];
currTarget.blocks.updateAssetName(oldName, newName, 'sprite'); currTarget.blocks.updateAssetName(oldName, newName, 'sprite');
} }
if (newUnusedName !== oldName) this.emitTargetsUpdate();
} }
this.emitTargetsUpdate();
} else { } else {
throw new Error('No target with the provided id.'); throw new Error('No target with the provided id.');
} }
@ -1281,7 +1288,9 @@ class VirtualMachine extends EventEmitter {
// Currently editing target id. // Currently editing target id.
editingTarget: this.editingTarget ? this.editingTarget.id : null editingTarget: this.editingTarget ? this.editingTarget.id : null
}); });
if (triggerProjectChange) this.runtime.emitProjectChanged(); if (triggerProjectChange) {
this.runtime.emitProjectChanged();
}
} }
/** /**
@ -1383,7 +1392,11 @@ class VirtualMachine extends EventEmitter {
reorderCostume (targetId, costumeIndex, newIndex) { reorderCostume (targetId, costumeIndex, newIndex) {
const target = this.runtime.getTargetById(targetId); const target = this.runtime.getTargetById(targetId);
if (target) { if (target) {
return target.reorderCostume(costumeIndex, newIndex); const reorderSuccessful = target.reorderCostume(costumeIndex, newIndex);
if (reorderSuccessful) {
this.runtime.emitProjectChanged();
}
return reorderSuccessful;
} }
return false; return false;
} }
@ -1398,7 +1411,11 @@ class VirtualMachine extends EventEmitter {
reorderSound (targetId, soundIndex, newIndex) { reorderSound (targetId, soundIndex, newIndex) {
const target = this.runtime.getTargetById(targetId); const target = this.runtime.getTargetById(targetId);
if (target) { if (target) {
return target.reorderSound(soundIndex, newIndex); const reorderSuccessful = target.reorderSound(soundIndex, newIndex);
if (reorderSuccessful) {
this.runtime.emitProjectChanged();
}
return reorderSuccessful;
} }
return false; return false;
} }
@ -1440,6 +1457,11 @@ class VirtualMachine extends EventEmitter {
} else { } else {
this.editingTarget.postSpriteInfo(data); this.editingTarget.postSpriteInfo(data);
} }
// Post sprite info means the gui has changed something about a sprite,
// either through the sprite info pane fields (e.g. direction, size) or
// through dragging a sprite on the stage
// Emit a project changed event.
this.runtime.emitProjectChanged();
} }
/** /**