5 lines
12 KiB
JavaScript
5 lines
12 KiB
JavaScript
// actioncable@5.2.8 downloaded from https://ga.jspm.io/npm:actioncable@5.2.8-1/lib/assets/compiled/action_cable.js
|
|
|
|
var t="undefined"!==typeof globalThis?globalThis:"undefined"!==typeof self?self:global;var n={};(function(){var o=this||t;(function(){(function(){var n=[].slice;(this||t).ActionCable={INTERNAL:{message_types:{welcome:"welcome",ping:"ping",confirmation:"confirm_subscription",rejection:"reject_subscription"},default_mount_path:"/cable",protocols:["actioncable-v1-json","actioncable-unsupported"]},WebSocket:window.WebSocket,logger:window.console,createConsumer:function(n){var o;null==n&&(n=null!=(o=this.getConfig("url"))?o:(this||t).INTERNAL.default_mount_path);return new e.Consumer(this.createWebSocketURL(n))},getConfig:function(t){var n;n=document.head.querySelector("meta[name='action-cable-"+t+"']");return null!=n?n.getAttribute("content"):void 0},createWebSocketURL:function(t){var n;if(t&&!/^wss?:/i.test(t)){n=document.createElement("a");n.href=t;n.href=n.href;n.protocol=n.protocol.replace("http","ws");return n.href}return t},startDebugging:function(){return(this||t).debugging=true},stopDebugging:function(){return(this||t).debugging=null},log:function(){var o,e;o=1<=arguments.length?n.call(arguments,0):[];if((this||t).debugging){o.push(Date.now());return(e=(this||t).logger).log.apply(e,["[ActionCable]"].concat(n.call(o)))}}}}).call(this||t)}).call(o);var e=o.ActionCable;(function(){(function(){var bind=function(t,n){return function(){return t.apply(n,arguments)}};e.ConnectionMonitor=function(){var n,o,i;ConnectionMonitor.pollInterval={min:3,max:30};ConnectionMonitor.staleThreshold=6;function ConnectionMonitor(n){(this||t).connection=n;(this||t).visibilityDidChange=bind((this||t).visibilityDidChange,this||t);(this||t).reconnectAttempts=0}ConnectionMonitor.prototype.start=function(){if(!this.isRunning()){(this||t).startedAt=o();delete(this||t).stoppedAt;this.startPolling();document.addEventListener("visibilitychange",(this||t).visibilityDidChange);return e.log("ConnectionMonitor started. pollInterval = "+this.getPollInterval()+" ms")}};ConnectionMonitor.prototype.stop=function(){if(this.isRunning()){(this||t).stoppedAt=o();this.stopPolling();document.removeEventListener("visibilitychange",(this||t).visibilityDidChange);return e.log("ConnectionMonitor stopped")}};ConnectionMonitor.prototype.isRunning=function(){return null!=(this||t).startedAt&&null==(this||t).stoppedAt};ConnectionMonitor.prototype.recordPing=function(){return(this||t).pingedAt=o()};ConnectionMonitor.prototype.recordConnect=function(){(this||t).reconnectAttempts=0;this.recordPing();delete(this||t).disconnectedAt;return e.log("ConnectionMonitor recorded connect")};ConnectionMonitor.prototype.recordDisconnect=function(){(this||t).disconnectedAt=o();return e.log("ConnectionMonitor recorded disconnect")};ConnectionMonitor.prototype.startPolling=function(){this.stopPolling();return this.poll()};ConnectionMonitor.prototype.stopPolling=function(){return clearTimeout((this||t).pollTimeout)};ConnectionMonitor.prototype.poll=function(){return(this||t).pollTimeout=setTimeout(function(t){return function(){t.reconnectIfStale();return t.poll()}}(this||t),this.getPollInterval())};ConnectionMonitor.prototype.getPollInterval=function(){var o,e,i,r;r=(this||t).constructor.pollInterval,i=r.min,e=r.max;o=5*Math.log((this||t).reconnectAttempts+1);return Math.round(1e3*n(o,i,e))};ConnectionMonitor.prototype.reconnectIfStale=function(){if(this.connectionIsStale()){e.log("ConnectionMonitor detected stale connection. reconnectAttempts = "+(this||t).reconnectAttempts+", pollInterval = "+this.getPollInterval()+" ms, time disconnected = "+i((this||t).disconnectedAt)+" s, stale threshold = "+(this||t).constructor.staleThreshold+" s");(this||t).reconnectAttempts++;if(this.disconnectedRecently())return e.log("ConnectionMonitor skipping reopening recent disconnect");e.log("ConnectionMonitor reopening");return(this||t).connection.reopen()}};ConnectionMonitor.prototype.connectionIsStale=function(){var n;return i(null!=(n=(this||t).pingedAt)?n:(this||t).startedAt)>(this||t).constructor.staleThreshold};ConnectionMonitor.prototype.disconnectedRecently=function(){return(this||t).disconnectedAt&&i((this||t).disconnectedAt)<(this||t).constructor.staleThreshold};ConnectionMonitor.prototype.visibilityDidChange=function(){if("visible"===document.visibilityState)return setTimeout(function(t){return function(){if(t.connectionIsStale()||!t.connection.isOpen()){e.log("ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = "+document.visibilityState);return t.connection.reopen()}}}(this||t),200)};o=function(){return(new Date).getTime()};i=function(t){return(o()-t)/1e3};n=function(t,n,o){return Math.max(n,Math.min(o,t))};return ConnectionMonitor}()}).call(this||t);(function(){var n,o,i,r,s,c=[].slice,bind=function(t,n){return function(){return t.apply(n,arguments)}},u=[].indexOf||function(n){for(var o=0,e=(this||t).length;o<e;o++)if(o in(this||t)&&(this||t)[o]===n)return o;return-1};r=e.INTERNAL,o=r.message_types,i=r.protocols;s=2<=i.length?c.call(i,0,n=i.length-1):(n=0,[]),i[n++];e.Connection=function(){Connection.reopenDelay=500;function Connection(n){(this||t).consumer=n;(this||t).open=bind((this||t).open,this||t);(this||t).subscriptions=(this||t).consumer.subscriptions;(this||t).monitor=new e.ConnectionMonitor(this||t);(this||t).disconnected=true}Connection.prototype.send=function(n){if(this.isOpen()){(this||t).webSocket.send(JSON.stringify(n));return true}return false};Connection.prototype.open=function(){if(this.isActive()){e.log("Attempted to open WebSocket, but existing socket is "+this.getState());return false}e.log("Opening WebSocket, current state is "+this.getState()+", subprotocols: "+i);null!=(this||t).webSocket&&this.uninstallEventHandlers();(this||t).webSocket=new e.WebSocket((this||t).consumer.url,i);this.installEventHandlers();(this||t).monitor.start();return true};Connection.prototype.close=function(n){var o,e;o=(null!=n?n:{allowReconnect:true}).allowReconnect;o||(this||t).monitor.stop();if(this.isActive())return null!=(e=(this||t).webSocket)?e.close():void 0};Connection.prototype.reopen=function(){var n;e.log("Reopening WebSocket, current state is "+this.getState());if(!this.isActive())return this.open();try{return this.close()}catch(t){n=t;return e.log("Failed to reopen WebSocket",n)}finally{e.log("Reopening WebSocket in "+(this||t).constructor.reopenDelay+"ms");setTimeout((this||t).open,(this||t).constructor.reopenDelay)}};Connection.prototype.getProtocol=function(){var n;return null!=(n=(this||t).webSocket)?n.protocol:void 0};Connection.prototype.isOpen=function(){return this.isState("open")};Connection.prototype.isActive=function(){return this.isState("open","connecting")};Connection.prototype.isProtocolSupported=function(){var t;return t=this.getProtocol(),u.call(s,t)>=0};Connection.prototype.isState=function(){var t,n;n=1<=arguments.length?c.call(arguments,0):[];return t=this.getState(),u.call(n,t)>=0};Connection.prototype.getState=function(){var n,o,e;for(o in WebSocket){e=WebSocket[o];if(e===(null!=(n=(this||t).webSocket)?n.readyState:void 0))return o.toLowerCase()}return null};Connection.prototype.installEventHandlers=function(){var n,o;for(n in(this||t).events){o=(this||t).events[n].bind(this||t);(this||t).webSocket["on"+n]=o}};Connection.prototype.uninstallEventHandlers=function(){var n;for(n in(this||t).events)(this||t).webSocket["on"+n]=function(){}};Connection.prototype.events={message:function(n){var e,i,r,s;if(this.isProtocolSupported()){r=JSON.parse(n.data),e=r.identifier,i=r.message,s=r.type;switch(s){case o.welcome:(this||t).monitor.recordConnect();return(this||t).subscriptions.reload();case o.ping:return(this||t).monitor.recordPing();case o.confirmation:return(this||t).subscriptions.notify(e,"connected");case o.rejection:return(this||t).subscriptions.reject(e);default:return(this||t).subscriptions.notify(e,"received",i)}}},open:function(){e.log("WebSocket onopen event, using '"+this.getProtocol()+"' subprotocol");(this||t).disconnected=false;if(!this.isProtocolSupported()){e.log("Protocol is unsupported. Stopping monitor and disconnecting.");return this.close({allowReconnect:false})}},close:function(n){e.log("WebSocket onclose event");if(!(this||t).disconnected){(this||t).disconnected=true;(this||t).monitor.recordDisconnect();return(this||t).subscriptions.notifyAll("disconnected",{willAttemptReconnect:(this||t).monitor.isRunning()})}},error:function(){return e.log("WebSocket onerror event")}};return Connection}()}).call(this||t);(function(){var n=[].slice;e.Subscriptions=function(){function Subscriptions(n){(this||t).consumer=n;(this||t).subscriptions=[]}Subscriptions.prototype.create=function(n,o){var i,r,s;i=n;r="object"===typeof i?i:{channel:i};s=new e.Subscription((this||t).consumer,r,o);return this.add(s)};Subscriptions.prototype.add=function(n){(this||t).subscriptions.push(n);(this||t).consumer.ensureActiveConnection();this.notify(n,"initialized");this.sendCommand(n,"subscribe");return n};Subscriptions.prototype.remove=function(t){this.forget(t);this.findAll(t.identifier).length||this.sendCommand(t,"unsubscribe");return t};Subscriptions.prototype.reject=function(t){var n,o,e,i,r;e=this.findAll(t);i=[];for(n=0,o=e.length;n<o;n++){r=e[n];this.forget(r);this.notify(r,"rejected");i.push(r)}return i};Subscriptions.prototype.forget=function(n){var o;(this||t).subscriptions=function(){var e,i,r,s;r=(this||t).subscriptions;s=[];for(e=0,i=r.length;e<i;e++){o=r[e];o!==n&&s.push(o)}return s}.call(this||t);return n};Subscriptions.prototype.findAll=function(n){var o,e,i,r,s;i=(this||t).subscriptions;r=[];for(o=0,e=i.length;o<e;o++){s=i[o];s.identifier===n&&r.push(s)}return r};Subscriptions.prototype.reload=function(){var n,o,e,i,r;e=(this||t).subscriptions;i=[];for(n=0,o=e.length;n<o;n++){r=e[n];i.push(this.sendCommand(r,"subscribe"))}return i};Subscriptions.prototype.notifyAll=function(){var o,e,i,r,s,c,u;e=arguments[0],o=2<=arguments.length?n.call(arguments,1):[];s=(this||t).subscriptions;c=[];for(i=0,r=s.length;i<r;i++){u=s[i];c.push((this||t).notify.apply(this||t,[u,e].concat(n.call(o))))}return c};Subscriptions.prototype.notify=function(){var t,o,e,i,r,s,c;s=arguments[0],o=arguments[1],t=3<=arguments.length?n.call(arguments,2):[];c="string"===typeof s?this.findAll(s):[s];r=[];for(e=0,i=c.length;e<i;e++){s=c[e];r.push("function"===typeof s[o]?s[o].apply(s,t):void 0)}return r};Subscriptions.prototype.sendCommand=function(n,o){var e;e=n.identifier;return(this||t).consumer.send({command:o,identifier:e})};return Subscriptions}()}).call(this||t);(function(){e.Subscription=function(){var n;function Subscription(o,e,i){(this||t).consumer=o;null==e&&(e={});(this||t).identifier=JSON.stringify(e);n(this||t,i)}Subscription.prototype.perform=function(t,n){null==n&&(n={});n.action=t;return this.send(n)};Subscription.prototype.send=function(n){return(this||t).consumer.send({command:"message",identifier:(this||t).identifier,data:JSON.stringify(n)})};Subscription.prototype.unsubscribe=function(){return(this||t).consumer.subscriptions.remove(this||t)};n=function(t,n){var o,e;if(null!=n)for(o in n){e=n[o];t[o]=e}return t};return Subscription}()}).call(this||t);(function(){e.Consumer=function(){function Consumer(n){(this||t).url=n;(this||t).subscriptions=new e.Subscriptions(this||t);(this||t).connection=new e.Connection(this||t)}Consumer.prototype.send=function(n){return(this||t).connection.send(n)};Consumer.prototype.connect=function(){return(this||t).connection.open()};Consumer.prototype.disconnect=function(){return(this||t).connection.close({allowReconnect:false})};Consumer.prototype.ensureActiveConnection=function(){if(!(this||t).connection.isActive())return(this||t).connection.open()};return Consumer}()}).call(this||t)}).call(this||t);n&&(n=e)}).call(n);var o=n;export{o as default};
|
|
|