diff options
-rw-r--r-- | examples/nword.js | 50 | ||||
-rw-r--r-- | tools/queue.js | 7 |
2 files changed, 55 insertions, 2 deletions
diff --git a/examples/nword.js b/examples/nword.js new file mode 100644 index 0000000..fe09a0b --- /dev/null +++ b/examples/nword.js @@ -0,0 +1,50 @@ +// Replaces the n-word with "n-word" +const Socket = require('../socket'); +const Space = require('../space'); +const Search = require('../tools/search'); +const Queue = require('../tools/queue'); +const maketiles = require('../utils/maketiles'); +const vec = require('../utils/vec'); +const wwrap = require('../utils/writewrap'); + +var main = new Socket(); +wwrap.call(main); +var search = new Search(); +var ids = []; +var idcall = []; +var wq = new Queue(1000, 200, (w,max) =>{ + let globid = main.write(w); + let id; + while (id = ids.shift()) { + if (id > max) break; + main.on(globid, idcall.shift()); + } +}); + +main.on('open',()=>{wq.enable(); console.log('here');}); + +var expire = {}; +main.on('tileUpdate', (send, source, tiles)=>{ + let tiledata = maketiles(tiles); + for (loc of tiledata.locs) { + let spc = tiledata.tilespaces[loc]; + if (search.has(loc)){ + clearTimeout(expire[loc]); + search.del(loc); + } + search.add(loc, spc); + expire[loc] = setTimeout(() => {search.del(loc); delete expire[loc];}, + 30000); + } +}); + +search.match(new Space().adhoc('nigger'), clean); +search.match(new Space().adhoc('Nigger'), clean); +search.match(new Space().adhoc('NIGGER'), clean); + +let response = new Space().adhoc(' I am a racist '); +function clean(coord, send){ + response.loc = vec.add(coord,[0,-6]); + ids.push(wq.enqueue(response.towrite())); + idcall.push(() => {search.update(response);}); +} diff --git a/tools/queue.js b/tools/queue.js index d8853e4..f45ec80 100644 --- a/tools/queue.js +++ b/tools/queue.js @@ -3,15 +3,18 @@ module.exports = function(delayms, maxExport, call){ let queue = []; - let open = true; // The meaning of this variable is complicated, so watch closely + let id = 0; + let open = true; // Has call() run in the past delayms ms? let getObjs = (maxExport == 1) ? // Should call be given one object or an array? () => queue.shift() : () => queue.splice(0, maxExport); this.enqueue = function(arr){ + id += arr.length; for (item of arr) queue.push(item); if (open) dequeue(); + return id; } disabled = true; // Used to disable pushes when non-sensical like if a socket is down @@ -31,7 +34,7 @@ module.exports = function(delayms, maxExport, call){ open = true; else { open = false; - call(getObjs()); + call(getObjs(), id-queue.length); setTimeout(dequeue, delayms); } } |