Smol work on compute graph

This commit is contained in:
hppeng 2022-05-21 15:51:09 -07:00
parent ac636c9498
commit ce5532e1d9
2 changed files with 56 additions and 3 deletions

View file

@ -1,15 +1,33 @@
let _ALL_NODES = new Map();
class ComputeNode() {
class ComputeNode {
/***
* Make a generic compute node.
* Adds the node to the global map of nodenames to nodes (for calling from html listeners).
*
* @param name : Name of the node (string). Must be unique. Must "fit in" a JS string (terminated by single quotes).
*/
constructor(name) {
if (_ALL_NODES.has(name)) {
throw 'Duplicate node name: ' + name;
}
_ALL_NODES.set(name, this)
this.inputs = [];
this.children = [];
this.value = 0;
this.compute_func = null;
this.callback_func = null;
this.name = name;
this.update_task = null;
this.update_time = Date.now();
}
update() {
update(timestamp) {
if (timestamp < this.update_time) {
return;
}
this.update_time = timestamp;
let value_map = Map();
for (const input of this.inputs) {
value_map.set(input.name, input.get_value());
@ -25,3 +43,38 @@ class ComputeNode() {
return this.value
}
}
/***
* Schedule a ComputeNode to be updated.
*
* @param node_name : ComputeNode name to schedule an update for.
*/
function calcSchedule(node_name) {
node = _ALL_NODES.get(node_name);
if (node.update_task !== null) {
clearTimeout(node.update_task);
}
node.update_task = setTimeout(function() {
const timestamp = Date.now();
node.update(timestamp);
node.update_task = null;
}, 500);
}
/***
* Node for getting an item's stats from an item input field.
*/
class ItemStats extends ComputeNode {
/***
* Make an item stat pulling compute node.
*
* @param name: Name of this node.
* @oaram item_input_field: Input field (html element) to listen for item names from.
*/
constructor(name, item_input_field) {
super(name);
this.input_field.setAttribute("onInput", "calcSchedule('"+name+"');");
this.input_field = item_input_field;
}
}

View file

@ -256,7 +256,7 @@ function decodeBuild(url_tag) {
}
}
/* Stores the entire build in a string using B64 encryption and adds it to the URL.
/* Stores the entire build in a string using B64 encoding and adds it to the URL.
*/
function encodeBuild() {