From 8b014c3ce98670abd895789e9e206a828cc95a33 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 24 Jan 2020 15:31:02 -0500 Subject: Identity function built for .call use In an effort to move jarvis to using one class, MetaSocket, for all of its interactions with the main socket, ident is now only able to be used as id.call(socket); this should simplify the behavior and means that it can use EventEmitter events. --- utils/ident.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'utils') diff --git a/utils/ident.js b/utils/ident.js index 187132f..b4fa8ff 100644 --- a/utils/ident.js +++ b/utils/ident.js @@ -2,32 +2,32 @@ const vec = require('../utils/vec'); -module.exports = function identify(sock, initOnce, init, deinit){ - // `sock` should be Socket instance. +module.exports = function identify(){ + // `this` should be Socket instance. // initOnce and init should be functions that act like they describe; initOnce runs once per program execution and init whenever the socket turns back on - sock.on('open', ()=>{ + let self = this; + + self.on('open', ()=>{ // "Pings" the server with a cursor location which is detected and saved by identity() let coords = [Math.floor(Math.random()*100000+16), Math.floor(Math.random()*1000000+16)]; - sock.cursor(coords); - sock.on('cursor', detect); + self.cursor(coords); + self.on('cursor', detect); function detect(pos, send){ if (vec.equals(pos[0],coords)){ - sock.off('cursor', detect); + self.off('cursor', detect); identity(send); } } }); - sock.on('close', deinit); - // Calls initOnce and init on successful identification. let initialized = false; function identity(send){ if (! initialized){ initialized = true; - initOnce(); + self.emit('initOnce'); } - init(send); + self.emit('init', send); } } -- cgit From 8cb88f2ccf8d4487af74ba5f6c9e81a7dc306c54 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 24 Jan 2020 15:34:10 -0500 Subject: wwrap can be used for socket extension --- utils/writewrap.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'utils') diff --git a/utils/writewrap.js b/utils/writewrap.js index 3ca7ede..2e2ae07 100644 --- a/utils/writewrap.js +++ b/utils/writewrap.js @@ -1,12 +1,11 @@ // An EventEmitter wrapper for socket.js writes const EventEmitter = require('events'); -class wwrap extends EventEmitter{ - constructor(socket){ - super(); - socket.on('write', (acc) => { // Assumed that none are rejected because I've never seen it - for (let w of acc) this.emit(w); - }); - } +function wwrap(){ // should be called with wwrap.call(socket) + let self = this; + self.on('write', (acc) => { + for (let w of acc) this.emit(w); + }); } + module.exports = wwrap; -- cgit