/
nmsg-rpc.min.js
2 lines (2 loc) · 5.21 KB
/
nmsg-rpc.min.js
1
2
!function(t){function e(r){if(i[r])return i[r].exports;var n=i[r]={exports:{},id:r,loaded:!1};return t[r].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){var r,n,s,o=this;"undefined"!=typeof window&&(o=window),"undefined"!=typeof self&&(o=self),function(i,o){n=[],r=o,s="function"==typeof r?r.apply(e,n):r,!(void 0!==s&&(t.exports=s)),i.nmsg=i.nmsg||{},i.nmsg.rpc=o()}(o,function(){return i(1)})},function(t,e,i){"use strict";var r=this&&this.__extends||function(t,e){function i(){this.constructor=t}for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)},n=i(2),s=function(){},o=function(){function t(){this.data=null,this.id=0,this.event="",this.args=[],this.callbacks=[],this.rid=0,this.func=0,this.timeout=t.timeout}return t.getNextId=function(){return t.id=t.id%1e9+1},t.prototype.hasCallbacks=function(){for(var t=0,e=this.args;t<e.length;t++){var i=e[t];if("function"==typeof i)return!0}return!1},t.prototype.isResponse=function(){return!!this.rid},t.id=0,t.timeout=15e3,t}();e.Frame=o;var a=function(t){function e(e,i){void 0===e&&(e=[]),void 0===i&&(i=""),t.call(this),this.id=o.getNextId(),this.event=i,this.args=e}return r(e,t),e.createResponse=function(t,i,r){var n=new e(r);return n.rid=t.id,n.func=i,n},e.prototype.processResponse=function(t){var e=t.func,i=this.args[e];"function"==typeof i&&(this.args[e]=null,i.apply(null,t.args))},e.prototype.serialize=function(){var t={i:this.id};if(this.event&&(t.e=this.event),this.args.length){t.a=[];for(var e=[],i=0;i<this.args.length;i++){var r=this.args[i];"function"==typeof r?(e.push(i),this.callbacks.push(r)):(t.a.push(r),o.timeout!=this.timeout&&(t.t=this.timeout/1e3))}e.length&&(t.c=e)}return this.rid&&(t.r=this.rid,t.f=this.func),this.data=t,this.data},e}(o);e.FrameOutgoing=a;var h=function(t){function e(){t.apply(this,arguments)}return r(e,t),e.prototype.unserialize=function(t,e){if("number"!=typeof t.i)throw Error("Error parsing id");this.id=t.i,this.timeout="number"==typeof t.t?t.t:o.timeout;var i=t.a&&t.a instanceof Array?t.a:[];this.args=[];for(var r=t.c&&t.c instanceof Array?t.c:[],n=0,s=0,a=0;a<i.length+r.length;a++)if(a===r[n]){var h=r[n++];if("number"!=typeof h)throw Error("Invalid callback list");this.args.push(e(this,h))}else this.args.push(i[s++]);if(t.e){if("string"!=typeof t.e)throw Error("Error parsing event");this.event=t.e}else{if("number"!=typeof t.r)throw Error("Error parsing resposne id");if(this.rid=t.r,"number"!=typeof t.f)throw Error("Error parsing response position");this.func=t.f}},e}(o);e.FrameIncoming=h;var u=function(){function t(){this.latency=500,this.frame={},this.timer={},this.send=s,this.onerror=s,this.onframe=s,this.api=null,this.subs={}}return t.prototype.genCallack=function(t,e){var i=this,r=!1;return function(){for(var n=[],s=0;s<arguments.length;s++)n[s-0]=arguments[s];if(r)throw Error('Already called: .on("'+t.event+'") '+e+"th arg");r=!0,i.dispatch(a.createResponse(t,e,n))}},t.prototype.pub=function(t){var e=t.event,i=t.args;if(e){var r=!1;if(this.onevent&&(r=!!this.onevent(e,i)),!r){var n;if(this.api&&(n=this.api.get(e)),n)n.apply(this,i);else{var s=this.subs[e];s&&s.apply(null,i),s=this.subs["*"],s&&s.apply(null,[e].concat(i))}}}},t.prototype.sendData=function(t){this.send(t)},t.prototype.dispatch=function(t){var e=this;t.hasCallbacks()&&(this.frame[t.id]=t,this.timer[t.id]=setTimeout(function(){delete e.frame[t.id],delete e.timer[t.id]},t.timeout+this.latency));var i=t.serialize();this.sendData(i)},t.prototype.processResponse=function(t){var e=this.frame[t.rid];if(e&&(e.processResponse(t),!e.hasCallbacks())){var i=e.id;delete this.frame[i];var r=this.timer[i];r&&clearTimeout(r),delete this.timer[i]}},t.prototype.procMsg=function(t){var e=new h;try{e.unserialize(t,this.genCallack.bind(this))}catch(t){return void this.onerror(t)}this.onframe(e),e.isResponse()?this.processResponse(e):this.pub(e)},t.prototype.setApi=function(t){return this.api=t,this},t.prototype.onmessage=function(t){if("object"==typeof t)if(t instanceof Array)for(var e=0,i=t;e<i.length;e++){var r=i[e];this.procMsg(r)}else this.procMsg(t)},t.prototype.on=function(t,e){return this.subs[t]=e,this},t.prototype.off=function(t){return delete this.subs[t],this},t.prototype.emit=function(t){for(var e=[],i=1;i<arguments.length;i++)e[i-1]=arguments[i];var r=new a(e,t);return this.dispatch(r),this},t}();e.Router=u;var f=function(t){function e(){t.apply(this,arguments),this.cycle=10,this.cycleTimer=0,this.buffer=[]}return r(e,t),e.prototype.flush=function(){this.send(this.buffer),this.buffer=[]},e.prototype.sendData=function(t){this.buffer.push(t),this.startTimer()},e.prototype.startTimer=function(){var t=this;this.cycleTimer||(this.cycleTimer=setTimeout(function(){t.cycleTimer=0,t.flush()},this.cycle))},e}(u);e.RouterBuffered=f;var p=function(){function t(){this.methods={}}return t.prototype.add=function(t){return this.methods=n.extend(this.methods,t),this},t.prototype.get=function(t){return this.methods[t]},t}();e.Api=p},function(t,e){"use strict";function i(t,e){for(var r=[],n=2;n<arguments.length;n++)r[n-2]=arguments[n];if("object"==typeof e)for(var s in e)t[s]=e[s];return r.length?i.apply(null,[t].concat(r)):t}e.extend=i}]);
//# sourceMappingURL=dist/nmsg-rpc.min.js.map