Cleanly handle deleting running scripts (#162)

* Cleanly handle deleting running scripts

* Turn off glow request on retire thread; add null check
This commit is contained in:
Tim Mickel 2016-09-15 13:51:40 -04:00 committed by GitHub
parent 9b4433069e
commit 8987330853
4 changed files with 28 additions and 1 deletions

View file

@ -185,6 +185,10 @@ Blocks.prototype.blocklyListen = function (e, isFlyout, opt_runtime) {
if (this._blocks[e.blockId].shadow) {
return;
}
// Inform any runtime to forget about glows on this script.
if (opt_runtime && this._blocks[e.blockId].topLevel) {
opt_runtime.quietGlow(e.blockId);
}
this.deleteBlock({
id: e.blockId
});

View file

@ -22,6 +22,13 @@ var execute = function (sequencer, thread) {
var currentBlockId = thread.peekStack();
var currentStackFrame = thread.peekStackFrame();
// Verify that the block still exists.
if (!target ||
typeof target.blocks.getBlock(currentBlockId) === 'undefined') {
// No block found: stop the thread; script no longer exists.
sequencer.retireThread(thread);
return;
}
// Query info about the block.
var opcode = target.blocks.getOpcode(currentBlockId);
var blockFunction = runtime.getOpcodeFunction(opcode);

View file

@ -383,7 +383,9 @@ Runtime.prototype._updateScriptGlows = function () {
if (thread.requestScriptGlowInFrame && target == this._editingTarget) {
var blockForThread = thread.peekStack() || thread.topBlock;
var script = target.blocks.getTopLevelScript(blockForThread);
requestedGlowsThisFrame.push(script);
if (script) {
requestedGlowsThisFrame.push(script);
}
}
}
// Compare to previous frame.
@ -408,6 +410,19 @@ Runtime.prototype._updateScriptGlows = function () {
this._scriptGlowsPreviousFrame = finalScriptGlows;
};
/**
* "Quiet" a script's glow: stop the VM from generating glow/unglow events
* about that script. Use when a script has just been deleted, but we may
* still be tracking glow data about it.
* @param {!string} scriptBlockId Id of top-level block in script to quiet.
*/
Runtime.prototype.quietGlow = function (scriptBlockId) {
var index = this._scriptGlowsPreviousFrame.indexOf(scriptBlockId);
if (index > -1) {
this._scriptGlowsPreviousFrame.splice(index, 1);
}
};
/**
* Emit feedback for block glowing (used in the sequencer).
* @param {?string} blockId ID for the block to update glow

View file

@ -173,6 +173,7 @@ Sequencer.prototype.proceedThread = function (thread) {
Sequencer.prototype.retireThread = function (thread) {
thread.stack = [];
thread.stackFrame = [];
thread.requestScriptGlowInFrame = false;
thread.setStatus(Thread.STATUS_DONE);
};