From 52d5b44da77f381c7ecf2137f86ae7d523142733 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Sat, 18 Jan 2020 21:23:33 -0500 Subject: jarvis.js reorg --- examples/jarvis.js | 24 ++++++++---------------- utils/maketiles.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 utils/maketiles.js diff --git a/examples/jarvis.js b/examples/jarvis.js index b82584f..fb3a999 100644 --- a/examples/jarvis.js +++ b/examples/jarvis.js @@ -2,7 +2,6 @@ const Space = require('../space'); const Socket = require('../socket'); -const tilekeys = require('../utils/tilekeys'); const ms = require('../utils/measurespace'); const vec = require('../utils/vec'); const comb = require('../utils/comb'); @@ -10,18 +9,18 @@ const Search = require('../tools/search'); const sched = require('../tools/schedule'); const ri = require('../utils/rectintersect'); const id = require('../utils/ident'); +const maketiles = require('../utils/maketiles'); -//// Basic handling on open and close of the socket +//// The queue of all writes to be sent to the server @ 200chars/1000ms +var writes = new sched.Queue(1000, 200, (elems) => main.write(elems)); var main = new Socket(); +// See ident.js for further documentation, but this basically sets up init functions id(main, initOnce, init, deinit); -//// tileUpdates require knowledge of the prog's identity (sender) var sender; - -//// Configurable helpers for utilities function init(send){ - sender = send; + sender = send; // tileUpdates require knowledge of the sender setTimeout(()=>{writes.enable()},1000); // Would fail if a write were added because it would be within a second of cursor send } @@ -34,27 +33,20 @@ function initOnce(){ main.on('tileUpdate', tileHandler); // Should only be active after the "control space" of the notification has been established } -var funcs = [protectArea, detectPrompt]; - //// Management utilities // When a tile changes, processes data into usable form and runs every function in `funcs` w/ that data. // Note that functions can edit inputs function tileHandler(send, source, tiles){ if (send == sender) return; - let locs = tilekeys(tiles); - let tilespaces = {}; - for (loc of locs){ - tilespaces[loc] = new Space(); - tilespaces[loc].fromfetch(tiles, [loc, loc], conform=false); - } + let tiledata = maketiles(tiles); funcs.forEach(func => { - func(send, tilespaces, locs); + func(send, tiledata.tilespaces, tiledata.locs); }); } //// "userspace" functions for responses, like to tileUpdates -var writes = new sched.Queue(1000, 200, (elems) => main.write(elems)); +var funcs = [protectArea, detectPrompt]; var command = 'jarvis' var sig = 'feynmansfedora' diff --git a/utils/maketiles.js b/utils/maketiles.js new file mode 100644 index 0000000..29b0c27 --- /dev/null +++ b/utils/maketiles.js @@ -0,0 +1,14 @@ +// A converter from the unusable tileUpdate format (the tiles object) to a `locs` list of tile coordinates and `tilespaces`---an object linking `locs` to small spaces which contain the tile data. + +const tilekeys = require('../utils/tilekeys'); +const Space = require('../space'); + +module.exports = (tiles) =>{ + let locs = tilekeys(tiles); + let tilespaces = {}; + for (loc of locs){ + tilespaces[loc] = new Space(); + tilespaces[loc].fromfetch(tiles, [loc, loc], conform=false); + } + return {'tilespaces':tilespaces, 'locs':locs}; +} -- cgit