From ce5532e1d9cfefa971890700aa8ee9305fc0c481 Mon Sep 17 00:00:00 2001 From: hppeng Date: Sat, 21 May 2022 15:51:09 -0700 Subject: [PATCH] Smol work on compute graph --- js/computation_graph.js | 57 +++++++++++++++++++++++++++++++++++++++-- js/sq2builder.js | 2 +- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/js/computation_graph.js b/js/computation_graph.js index 849e6e4..6e7f844 100644 --- a/js/computation_graph.js +++ b/js/computation_graph.js @@ -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; + } +} + diff --git a/js/sq2builder.js b/js/sq2builder.js index 7f4d2db..fc42f6d 100644 --- a/js/sq2builder.js +++ b/js/sq2builder.js @@ -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() {