diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-01-29 23:49:17 -0500 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-01-29 23:49:17 -0500 |
commit | 0e2883105e948ff6f35f954f8184fdd232d17a97 (patch) | |
tree | 7764d8b8aa770ddb836cb8a2dc7c580441437969 /utils | |
parent | df56e8a4c6b05e9f2f5c98c14af3f95a7bc6926c (diff) | |
parent | 1cc310e20dda4a378aac76b49288e6c8e2361e5e (diff) |
Merge branch 'socket'
Made the socket object used by jarvis a singleton MetaSocket object to maintain
clean, modular code standards. This also means that future feature addition
will be easier because all references are to one object, of which the behavior
can be easily changed since most systems rely on EventEmitter or callback-style
structures
Diffstat (limited to 'utils')
-rw-r--r-- | utils/ident.js | 20 | ||||
-rw-r--r-- | utils/writewrap.js | 13 |
2 files changed, 16 insertions, 17 deletions
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); } } 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; |