From 80d427c30d46937b9a2491ace764a9fc01318841 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 17 Jul 2020 21:40:54 -0400 Subject: examples/clear is now more efficient with the fetch api --- examples/clear.js | 58 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/examples/clear.js b/examples/clear.js index e845c55..68557f5 100644 --- a/examples/clear.js +++ b/examples/clear.js @@ -1,28 +1,58 @@ // Demonstrates large-scale changes using tools/queue.js; pastes a character (once) over a large area // This demo puts a bunch of z's on yourworldoftext.com/zworld (scroll up because it's a big area) -const Socket = require('../socket.js'); -const Space = require('../space.js'); -const Queue = require('../tools/queue.js'); +const Socket = require('../socket'); +const Space = require('../space'); +const Queue = require('../tools/queue'); +const comb = require('../utils/comb'); +const vec = require('../utils/vec'); -let width = 801; -let height = 801; -let sends = width*height; +let fetchdim = [ [-30,-30], [30,30] ]; +let chr = ' '; -let copyspace = new Space(); -copyspace.loc = [-400,-400]; +let chrdim = [ vec.tileToChar(fetchdim[0]), + vec.sub(vec.tileToChar(vec.add(fetchdim[1],[1,1])),[1,1]) ]; + // vec.add(,[1,1]) because to the maximum chr in a block is [7,15] + // higher than the minimun chr in a block. +let size = vec.sub(chrdim[1], chrdim[0]); -let row = 'z'.repeat(width)+'\n'; -let tot = row.repeat(height); -copyspace.adhoc(tot); +console.log('deletion from', chrdim[0], 'to', chrdim[1], 'will occur'); -let main = new Socket('zworld'); +let reqs = []; +for (let i = fetchdim[0][0]; i <= fetchdim[1][0]; i++){ + reqs.push([[i, fetchdim[0][1]], [i, fetchdim[1][1]]]); +} +// This assumes a maximum width of <=1000 by splitting the fetch into +// many rows, each one block tall (this is required by the API). + +// Constructs copyspace, what will be written to the page (this can be +// anything, and the repeated chr can be built upon using Space.comb). +// Transparency is also possible but this is also not demonstrated here. +let row = chr.repeat(size[0])+'\n'; +let tot = row.repeat(size[1]); +let copyspace = new Space().adhoc(tot); +copyspace.loc = vec.dot(fetchdim[0],[8,16]); + +let main = new Socket(); let writect = 0; +let sends; let writes = new Queue(1000, 200, w => { main.write(w); writect += 200; + if (writect > sends){ console.log('done!'); process.exit();} console.log(writect + '/' + sends)}); -writes.enqueue(copyspace.towrite()); +main.on('open',()=>{ + writes.enable(); // disabling on socket close is not handled, but + // should be in a longer script + main.fetch(reqs); + console.log('enabled'); +}); -main.on('open',()=>{writes.enable(); console.log('enabled');}); +main.on('fetch',(tiles)=>{ + let fetchspace = new Space().fromfetch(tiles,fetchdim); + copyspace.comb(fetchspace,comb.sub); + let exit = copyspace.towrite(); + sends = exit.length; + writes.enqueue(exit); +}); -- cgit