UNPKG

voidspace

Version:

Transfer variables between threads, programs, and devices.

1 lines 1.86 kB
var Voidspace=function(){this.datamap={},this.callback={},this.debug=!1};Voidspace.prototype.on=function(o,t){this.callback[o]=t},Voidspace.prototype.id=function(){return this.ws.id},Voidspace.prototype.reset=function(o){this.ws.send(JSON.stringify({req:"overwrite",data:{}})),o&&o()},Voidspace.prototype.connect=function(o){var t=this.callback;o||(o="ws://127.0.0.1:3552"),this.ws=new WebSocket(o),this.ws.onopen=function(){this.debug&&console.log("[VS][INFO] Websocket connected. "),void 0!==t.connect&&t.connect({e:"connect",dest:o})},this.ws.onmessage=function(o,e){this.debug&&console.log("[VS][INBOUND] "+o);var i=JSON.parse(o);"set"===i.req?this.datamap[i.tag]=i.data:"create"===i.req?t.create(i.data):"init"===i.req?this.datamap=i.data:"uuid"===i.req&&(this.id=i.data.uuid,t.uuid(i.data))}},Voidspace.prototype.disconnect=function(){this.ws.close()},Voidspace.prototype.get=function(o){return this.datamap[o]},Voidspace.prototype.set=function(o,t){o?(this.debug&&(console.log("[VS][INFO] Setting '"+o+"' to '"+t+"'. "),console.log("[VS][OUTBOUND] "+JSON.stringify({req:"set",key:o,pool:this.pool,data:t}))),this.datamap[o]=t,this.ws.send(JSON.stringify({req:"set",pool:this.pool,data:t,key:o}))):console.log("[VS][WARN] No key provided! Not setting value. ")},Voidspace.prototype.create=function(o,t){this.debug&&(console.log("[VS][INFO] Creating namespace '"+o+"'. "),console.log("[VS][OUTBOUND] "+JSON.stringify({req:"create",pool:o,uuid:this.ws.id}))),this.pool=o,this.ws.send(JSON.stringify({req:"create",pool:o})),this.callback.create=t},Voidspace.prototype.join=function(o){this.debug&&(console.log("[VS][INFO] Joining namespace '"+o+"'. "),console.log("[VS][OUTBOUND] "+JSON.stringify({req:"join",pool:o}))),this.pool=o,this.ws.send(JSON.stringify({req:"join",pool:o}))},Voidspace.prototype.save=function(o){},Voidspace.prototype.load=function(o){};