From 5ef246a2b0b7587d8c133a175c7c320503415f0d Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 8 Aug 2018 18:29:54 -0400 Subject: [PATCH] Export the sprite and return an add function when deleting the sprite. --- src/virtual-machine.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/virtual-machine.js b/src/virtual-machine.js index b1b186972..9dff04722 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -836,6 +836,7 @@ class VirtualMachine extends EventEmitter { /** * Delete a sprite and all its clones. * @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) { const target = this.runtime.getTargetById(targetId); @@ -849,6 +850,10 @@ class VirtualMachine extends EventEmitter { if (!sprite) { 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); const currentEditingTarget = this.editingTarget; for (let i = 0; i < sprite.clones.length; i++) { @@ -867,9 +872,10 @@ class VirtualMachine extends EventEmitter { } // Sprite object should be deleted by GC. this.emitTargetsUpdate(); - } else { - throw new Error('No target with the provided id.'); + return restoreSprite; } + + throw new Error('No target with the provided id.'); } /**