add BlocksRuntimeCache; rewrite startHats

This commit is contained in:
Michael "Z" Goddard 2019-01-18 17:21:35 -05:00
parent 84de5cdc7e
commit 96f3e3414e
No known key found for this signature in database
GPG key ID: 762CD40DD5349872
3 changed files with 155 additions and 42 deletions
src/engine

View file

@ -5,6 +5,7 @@ const MonitorRecord = require('./monitor-record');
const Clone = require('../util/clone');
const {Map} = require('immutable');
const BlocksExecuteCache = require('./blocks-execute-cache');
const BlocksRuntimeCache = require('./blocks-runtime-cache');
const log = require('../util/log');
const Variable = require('./variable');
const getMonitorIdForBlockWithArgs = require('../util/get-monitor-id');
@ -71,7 +72,13 @@ class Blocks {
* actively monitored.
* @type {Array<{blockId: string, target: Target}>}
*/
_monitored: null
_monitored: null,
/**
* A cache of hat opcodes to collection of theads to execute.
* @type {object.<string, object>}
*/
scripts: {}
};
/**
@ -504,6 +511,7 @@ class Blocks {
this._cache.procedureDefinitions = {};
this._cache._executeCached = {};
this._cache._monitored = null;
this._cache.scripts = {};
}
/**
@ -1178,4 +1186,35 @@ BlocksExecuteCache.getCached = function (blocks, blockId, CacheType) {
return cached;
};
/**
* Cache class constructor for runtime. Used to consider what threads should
* start based on hat data.
* @type {function}
*/
const RuntimeScriptCache = BlocksRuntimeCache._RuntimeScriptCache;
/**
* Get an array of scripts from a block container prefiltered to match opcode.
* @param {Blocks} blocks - Container of blocks
* @param {string} opcode - Opcode to filter top blocks by
* @returns {Array.<RuntimeScriptCache>} - Array of RuntimeScriptCache cache
* objects
*/
BlocksRuntimeCache.getScripts = function (blocks, opcode) {
let scripts = blocks._cache.scripts[opcode];
if (!scripts) {
scripts = blocks._cache.scripts[opcode] = [];
const allScripts = blocks._scripts;
for (let i = 0; i < allScripts.length; i++) {
const topBlockId = allScripts[i];
const block = blocks.getBlock(topBlockId);
if (block.opcode === opcode) {
scripts.push(new RuntimeScriptCache(blocks, topBlockId));
}
}
}
return scripts;
};
module.exports = Blocks;