UNPKG

ng-cable

Version:

Power your Angular application with ActionCable

2 lines (1 loc) 6.84 kB
(function(){this.Cable={PING_IDENTIFIER:"_ping",createConsumer:function(t){return new Cable.Consumer(t)}}}).call(this),function(){var t=[].slice,n=[].indexOf||function(t){for(var n=0,e=this.length;e>n;n++)if(n in this&&this[n]===t)return n;return-1};Cable.Connection=function(){function e(t){this.consumer=t,this.open()}return e.prototype.send=function(t){return this.isOpen()?(this.webSocket.send(JSON.stringify(t)),!0):!1},e.prototype.open=function(){return this.isState("open","connecting")?void 0:(this.webSocket=new WebSocket(this.consumer.url),this.installEventHandlers())},e.prototype.close=function(){var t;if(!this.isState("closed","closing"))return null!=(t=this.webSocket)?t.close():void 0},e.prototype.reopen=function(){return this.isOpen()?this.closeSilently(function(t){return function(){return t.open()}}(this)):this.open()},e.prototype.isOpen=function(){return this.isState("open")},e.prototype.isState=function(){var e,i;return i=1<=arguments.length?t.call(arguments,0):[],e=this.getState(),n.call(i,e)>=0},e.prototype.getState=function(){var t,n,e;for(n in WebSocket)if(e=WebSocket[n],e===(null!=(t=this.webSocket)?t.readyState:void 0))return n.toLowerCase();return null},e.prototype.closeSilently=function(t){null==t&&(t=function(){}),this.uninstallEventHandlers(),this.installEventHandler("close",t),this.installEventHandler("error",t);try{return this.webSocket.close()}finally{this.uninstallEventHandlers()}},e.prototype.installEventHandlers=function(){var t,n;n=[];for(t in this.events)n.push(this.installEventHandler(t));return n},e.prototype.installEventHandler=function(t,n){return null==n&&(n=this.events[t].bind(this)),this.webSocket.addEventListener(t,n)},e.prototype.uninstallEventHandlers=function(){var t,n;n=[];for(t in this.events)n.push(this.webSocket.removeEventListener(t));return n},e.prototype.events={message:function(t){var n,e,i;return i=JSON.parse(t.data),n=i.identifier,e=i.message,this.consumer.subscriptions.notify(n,"received",e)},open:function(){return this.consumer.subscriptions.reload()},close:function(){return this.consumer.subscriptions.notifyAll("disconnected")},error:function(){return this.consumer.subscriptions.notifyAll("disconnected"),this.closeSilently()}},e.prototype.toJSON=function(){return{state:this.getState()}},e}()}.call(this),function(){Cable.ConnectionMonitor=function(){function t(t){this.consumer=t,this.consumer.subscriptions.add(this),this.start()}var n,e,i;return t.prototype.identifier=Cable.PING_IDENTIFIER,t.prototype.pollInterval={min:2,max:30},t.prototype.staleThreshold={startedAt:4,pingedAt:8},t.prototype.connected=function(){return this.reset(),this.pingedAt=e()},t.prototype.received=function(){return this.pingedAt=e()},t.prototype.reset=function(){return this.reconnectAttempts=0},t.prototype.start=function(){return this.reset(),delete this.stoppedAt,this.startedAt=e(),this.poll()},t.prototype.stop=function(){return this.stoppedAt=e()},t.prototype.poll=function(){return setTimeout(function(t){return function(){return t.stoppedAt?void 0:(t.reconnectIfStale(),t.poll())}}(this),this.getInterval())},t.prototype.getInterval=function(){var t,e,i,o;return o=this.pollInterval,i=o.min,e=o.max,t=4*Math.log(this.reconnectAttempts+1),1e3*n(t,i,e)},t.prototype.reconnectIfStale=function(){return this.connectionIsStale()?(this.reconnectAttempts+=1,this.consumer.connection.reopen()):void 0},t.prototype.connectionIsStale=function(){return this.pingedAt?i(this.pingedAt)>this.staleThreshold.pingedAt:i(this.startedAt)>this.staleThreshold.startedAt},t.prototype.toJSON=function(){var t,n;return n=this.getInterval(),t=this.connectionIsStale(),{startedAt:this.startedAt,stoppedAt:this.stoppedAt,pingedAt:this.pingedAt,reconnectAttempts:this.reconnectAttempts,connectionIsStale:t,interval:n}},e=function(){return(new Date).getTime()},i=function(t){return(e()-t)/1e3},n=function(t,n,e){return Math.max(n,Math.min(e,t))},t}()}.call(this),function(){var t=[].slice;Cable.Subscriptions=function(){function n(t){this.consumer=t,this.subscriptions=[]}return n.prototype.create=function(t,n){var e,i;return e=t,i="object"==typeof e?e:{channel:e},new Cable.Subscription(this,i,n)},n.prototype.add=function(t){return this.subscriptions.push(t),this.notify(t,"initialized"),this.sendCommand(t,"subscribe")?this.notify(t,"connected"):void 0},n.prototype.reload=function(){var t,n,e,i,o;for(e=this.subscriptions,i=[],t=0,n=e.length;n>t;t++)o=e[t],i.push(this.sendCommand(o,"subscribe")?this.notify(o,"connected"):void 0);return i},n.prototype.remove=function(t){var n;return this.subscriptions=function(){var e,i,o,s;for(o=this.subscriptions,s=[],e=0,i=o.length;i>e;e++)n=o[e],n!==t&&s.push(n);return s}.call(this),this.findAll(t.identifier).length?void 0:this.sendCommand(t,"unsubscribe")},n.prototype.findAll=function(t){var n,e,i,o,s;for(i=this.subscriptions,o=[],n=0,e=i.length;e>n;n++)s=i[n],s.identifier===t&&o.push(s);return o},n.prototype.notifyAll=function(){var n,e,i,o,s,r,c;for(e=arguments[0],n=2<=arguments.length?t.call(arguments,1):[],s=this.subscriptions,r=[],i=0,o=s.length;o>i;i++)c=s[i],r.push(this.notify.apply(this,[c,e].concat(t.call(n))));return r},n.prototype.notify=function(){var n,e,i,o,s,r,c;for(r=arguments[0],e=arguments[1],n=3<=arguments.length?t.call(arguments,2):[],c="string"==typeof r?this.findAll(r):[r],s=[],i=0,o=c.length;o>i;i++)r=c[i],s.push("function"==typeof r[e]?r[e].apply(r,n):void 0);return s},n.prototype.sendCommand=function(t,n){var e;return e=t.identifier,e===Cable.PING_IDENTIFIER?this.consumer.connection.isOpen():this.consumer.send({command:n,identifier:e})},n.prototype.toJSON=function(){var t,n,e,i,o;for(e=this.subscriptions,i=[],t=0,n=e.length;n>t;t++)o=e[t],i.push(o.identifier);return i},n}()}.call(this),function(){Cable.Subscription=function(){function t(t,e,i){this.subscriptions=t,null==e&&(e={}),this.identifier=JSON.stringify(e),n(this,i),this.subscriptions.add(this),this.consumer=this.subscriptions.consumer}var n;return t.prototype.perform=function(t,n){return null==n&&(n={}),n.action=t,this.send(n)},t.prototype.send=function(t){return this.consumer.send({command:"message",identifier:this.identifier,data:JSON.stringify(t)})},t.prototype.unsubscribe=function(){return this.subscriptions.remove(this)},n=function(t,n){var e,i;if(null!=n)for(e in n)i=n[e],t[e]=i;return t},t}()}.call(this),function(){Cable.Consumer=function(){function t(t){this.url=t,this.subscriptions=new Cable.Subscriptions(this),this.connection=new Cable.Connection(this),this.connectionMonitor=new Cable.ConnectionMonitor(this)}return t.prototype.send=function(t){return this.connection.send(t)},t.prototype.inspect=function(){return JSON.stringify(this,null,2)},t.prototype.toJSON=function(){return{url:this.url,subscriptions:this.subscriptions,connection:this.connection,connectionMonitor:this.connectionMonitor}},t}()}.call(this);