-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.min.js
2 lines (2 loc) · 2.08 KB
/
wire.min.js
1
2
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Wire=void 0;var _createClass=function(){function n(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(e,t,i){return t&&n(e.prototype,t),i&&n(e,i),e}}(),_util=require("./util");function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var Wire=exports.Wire=function(){function n(e){var i=this;_classCallCheck(this,n),this.id=(0,_util.randomString)(),this.socket=e,this.timeout=7500,this.pending={},this.listeners={},this.socket.on("disconnect",this.destroy),this.socket.on("request",function(t){i.listeners[t.signal]&&i.listeners[t.signal](t.data,function(e){i.socket.emit("response",{tid:t.tid,data:e})},function(e){i.socket.emit("response",{tid:t.tid,error:e})})}),this.socket.on("response",function(e){i.pending[e.tid]&&(clearTimeout(i.pending[e.tid].timeout),e.error?i.pending[e.tid].reject(e.error):i.pending[e.tid].resolve(e.data),delete i.pending[e.tid])}),n.register[this.id]=this}return _createClass(n,[{key:"on",value:function(e,t){this.listeners[e]=t}},{key:"send",value:function(e,t){var n=this,r={tid:(0,_util.randomString)(),timestamp:Date.now(),data:t,signal:e};return new Promise(function(e,t){n.socket.emit("request",r);var i=setTimeout(function(){delete n.pending[r.tid]},n.timeout);n.pending[r.tid]={request:r,resolve:e,reject:t,timeout:i}})}},{key:"broadcast",value:function(e,t){return n.broadcast(e,t)}},{key:"destroy",value:function(){this.socket&&"function"==typeof this.socket.close&&this.socket.close(),delete n.register[this.id]}}]),n}();Wire.register={},Wire.broadcast=function(e,t){for(var i in Wire.register){Wire.register[i].send(e,t)}};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.randomString=randomString;var defaultLength=16,defaultCharset="123567890abcdefghijklmnopqrstuvwxyz";function randomString(t,e){t=t||defaultLength;for(var r=(e=e||defaultCharset).length,n="";n.length<t;)n+=e[Math.random()*r<<0];return n}