Export the sprite and return an add function when deleting the sprite.

This commit is contained in:
Karishma Chadha 2018-08-08 18:29:54 -04:00
parent 6e6e9b49cb
commit 5ef246a2b0

View file

@ -836,6 +836,7 @@ class VirtualMachine extends EventEmitter {
/** /**
* Delete a sprite and all its clones. * Delete a sprite and all its clones.
* @param {string} targetId ID of a target whose sprite to delete. * @param {string} targetId ID of a target whose sprite to delete.
* @return {Function} Returns a function to restore the sprite that was deleted
*/ */
deleteSprite (targetId) { deleteSprite (targetId) {
const target = this.runtime.getTargetById(targetId); const target = this.runtime.getTargetById(targetId);
@ -849,6 +850,10 @@ class VirtualMachine extends EventEmitter {
if (!sprite) { if (!sprite) {
throw new Error('No sprite associated with this target.'); throw new Error('No sprite associated with this target.');
} }
const spritePromise = this.exportSprite(targetId, 'uint8array');
const restoreSprite = () => {
spritePromise.then(spriteBuffer => this.addSprite(spriteBuffer));
};
this.runtime.requestRemoveMonitorByTargetId(targetId); this.runtime.requestRemoveMonitorByTargetId(targetId);
const currentEditingTarget = this.editingTarget; const currentEditingTarget = this.editingTarget;
for (let i = 0; i < sprite.clones.length; i++) { for (let i = 0; i < sprite.clones.length; i++) {
@ -867,9 +872,10 @@ class VirtualMachine extends EventEmitter {
} }
// Sprite object should be deleted by GC. // Sprite object should be deleted by GC.
this.emitTargetsUpdate(); this.emitTargetsUpdate();
} else { return restoreSprite;
throw new Error('No target with the provided id.');
} }
throw new Error('No target with the provided id.');
} }
/** /**