Merge pull request #10 from tmickel/feature/toggle-thread

Fix _removeThread, add toggleStack, green flag, stop button functions
This commit is contained in:
Tim Mickel 2016-05-02 10:50:29 -04:00
commit 7a5341b863
4 changed files with 133 additions and 14 deletions

View file

@ -172,13 +172,57 @@ Runtime.prototype._pushThread = function (id) {
/**
* Remove a thread from the list of threads.
* @param {!string} id ID of block that starts the stack
* @param {?Thread} thread Thread object to remove from actives
*/
Runtime.prototype._removeThread = function (id) {
var i = this.threads.indexOf(id);
Runtime.prototype._removeThread = function (thread) {
var i = this.threads.indexOf(thread);
if (i > -1) this.threads.splice(i, 1);
};
/**
* Toggle a stack
* @param {!string} stackId ID of block that starts the stack
*/
Runtime.prototype.toggleStack = function (stackId) {
// Remove any existing thread
for (var i = 0; i < this.threads.length; i++) {
if (this.threads[i].topBlock == stackId) {
this._removeThread(this.threads[i]);
return;
}
}
// Otherwise add it
this._pushThread(stackId);
};
/**
* Green flag, which stops currently running threads
* and adds all top-level stacks that start with the green flag
*/
Runtime.prototype.greenFlag = function () {
// Remove all existing threads
for (var i = 0; i < this.threads.length; i++) {
this._removeThread(this.threads[i]);
}
// Add all top stacks with green flag
for (var j = 0; j < this.stacks.length; j++) {
var topBlock = this.stacks[j];
if (this.blocks[topBlock].opcode === 'event_whenflagclicked') {
this._pushThread(this.stacks[j]);
}
}
};
/**
* Stop "everything"
*/
Runtime.prototype.stopAll = function () {
for (var i = 0; i < this.threads.length; i++) {
this._removeThread(this.threads[i]);
}
// @todo call stop function in all extensions/packages/WeDo stub
};
/**
* Repeatedly run `sequencer.stepThreads` and filter out
* inactive threads after each iteration.

View file

@ -4,6 +4,10 @@
* @constructor
*/
function Thread (firstBlock) {
/**
* Top block of the thread
*/
this.topBlock = firstBlock;
/**
* Next block that the thread will execute.
* @type {string}

81
vm.js
View file

@ -77,7 +77,7 @@
// Blocks
switch (e.type) {
case 'create':
instance.runtime.createBlock(adapter(e));
instance.runtime.createBlock(adapter(e), false);
break;
case 'change':
instance.runtime.changeBlock({
@ -103,6 +103,27 @@
break;
}
};
instance.flyoutBlockListener = function (e) {
switch (e.type) {
case 'create':
instance.runtime.createBlock(adapter(e), true);
break;
case 'change':
instance.runtime.changeBlock({
id: e.blockId,
element: e.element,
name: e.name,
value: e.newValue
});
break;
case 'delete':
instance.runtime.deleteBlock({
id: e.blockId
});
break;
}
};
}
/**
@ -1207,7 +1228,7 @@
* Block management: create blocks and stacks from a `create` event
* @param {!Object} block Blockly create event to be processed
*/
Runtime.prototype.createBlock = function (block) {
Runtime.prototype.createBlock = function (block, opt_isFlyoutBlock) {
// Create new block
this.blocks[block.id] = block;
@ -1224,7 +1245,9 @@
// Push block id to stacks array. New blocks are always a stack even if only
// momentary. If the new block is added to an existing stack this stack will
// be removed by the `moveBlock` method below.
if (!opt_isFlyoutBlock) {
this.stacks.push(block.id);
}
};
/**
@ -1327,13 +1350,57 @@
/**
* Remove a thread from the list of threads.
* @param {!string} id ID of block that starts the stack
* @param {?Thread} thread Thread object to remove from actives
*/
Runtime.prototype._removeThread = function (id) {
var i = this.threads.indexOf(id);
Runtime.prototype._removeThread = function (thread) {
var i = this.threads.indexOf(thread);
if (i > -1) this.threads.splice(i, 1);
};
/**
* Toggle a stack
* @param {!string} stackId ID of block that starts the stack
*/
Runtime.prototype.toggleStack = function (stackId) {
// Remove any existing thread
for (var i = 0; i < this.threads.length; i++) {
if (this.threads[i].topBlock == stackId) {
this._removeThread(this.threads[i]);
return;
}
}
// Otherwise add it
this._pushThread(stackId);
};
/**
* Green flag, which stops currently running threads
* and adds all top-level stacks that start with the green flag
*/
Runtime.prototype.greenFlag = function () {
// Remove all existing threads
for (var i = 0; i < this.threads.length; i++) {
this._removeThread(this.threads[i]);
}
// Add all top stacks with green flag
for (var j = 0; j < this.stacks.length; j++) {
var topBlock = this.stacks[j];
if (this.blocks[topBlock].opcode === 'event_whenflagclicked') {
this._pushThread(this.stacks[j]);
}
}
};
/**
* Stop "everything"
*/
Runtime.prototype.stopAll = function () {
for (var i = 0; i < this.threads.length; i++) {
this._removeThread(this.threads[i]);
}
// @todo call stop function in all extensions/packages/WeDo stub
};
/**
* Repeatedly run `sequencer.stepThreads` and filter out
* inactive threads after each iteration.
@ -1499,6 +1566,10 @@
* @constructor
*/
function Thread (firstBlock) {
/**
* Top block of the thread
*/
this.topBlock = firstBlock;
/**
* Next block that the thread will execute.
* @type {string}

10
vm.min.js vendored

File diff suppressed because one or more lines are too long