Add mechanism for per-block metadata

This commit is contained in:
Tim Mickel 2016-05-03 11:45:25 -04:00
parent c4138e6603
commit 4c6f08f665
2 changed files with 48 additions and 1 deletions

View file

@ -23,6 +23,12 @@ function Runtime () {
*/
this.blocks = {};
/**
* Primitive-accessible execution metadata for each block.
* @type {Object.<string, Object>}
*/
this.blockExecutionData = {};
/**
* All stacks in the workspace.
* A list of block IDs that represent stacks (first block in stack).
@ -90,6 +96,7 @@ Runtime.THREAD_STEP_INTERVAL = 1000 / 60;
Runtime.prototype.createBlock = function (block, opt_isFlyoutBlock) {
// Create new block
this.blocks[block.id] = block;
this.blockExecutionData[block.id] = {};
// Walk each field and add any shadow blocks
// @todo Expand this to cover vertical / nested blocks
@ -98,6 +105,7 @@ Runtime.prototype.createBlock = function (block, opt_isFlyoutBlock) {
for (var y in shadows) {
var shadow = shadows[y];
this.blocks[shadow.id] = shadow;
this.blockExecutionData[shadow.id] = {};
}
}
@ -194,6 +202,7 @@ Runtime.prototype.deleteBlock = function (e) {
// Delete block
delete this.blocks[e.id];
delete this.blockExecutionData[e.id];
};
// -----------------------------------------------------------------------------
@ -377,4 +386,24 @@ Runtime.prototype._getOpcode = function (id) {
return this.blocks[id].opcode;
};
/**
* Set block execution data
* @param {!string} id Block ID
* @param {!Any} key Data key
* @param {?Any} value Data value
*/
Runtime.prototype.setBlockExecutionData = function (id, key, value) {
this.blockExecutionData[id][key] = value;
};
/**
* Get block execution data
* @param {!string} id Block ID
* @param {!Any} key Data key
* @return {?Any} Data value
*/
Runtime.prototype.getBlockExecutionData = function (id, key) {
return this.blockExecutionData[id][key];
};
module.exports = Runtime;

View file

@ -110,6 +110,22 @@ Sequencer.prototype.stepThread = function (thread) {
instance.runtime.glowBlock(currentBlock, false);
};
/**
* A callback for the primitive to set data on the block level.
* @type {Function}
*/
var blockDataSetCallback = function (key, value) {
instance.runtime.setBlockExecutionData(currentBlock, key, value);
};
/**
* A callback for the primitive to get data on the block level.
* @type {Function}
*/
var blockDataGetCallback = function (key) {
return instance.runtime.getBlockExecutionData(currentBlock, key);
};
// @todo
var argValues = [];
@ -128,7 +144,9 @@ Sequencer.prototype.stepThread = function (thread) {
blockFunction(argValues, {
yield: threadYieldCallback,
done: threadDoneCallback,
timeout: YieldTimers.timeout
timeout: YieldTimers.timeout,
setData: blockDataSetCallback,
getData: blockDataGetCallback
});
}
catch(e) {