(globalThis.webpackChunkavo=globalThis.webpackChunkavo||[]).push([[219],{7034:(t,e,n)=>{"use strict";n.r(e),n.d(e,{Connection:()=>m,ConnectionMonitor:()=>l,Consumer:()=>v,INTERNAL:()=>h,Subscription:()=>b,Subscriptions:()=>f,adapters:()=>i,createConsumer:()=>y,createWebSocketURL:()=>S,getConfig:()=>A,logger:()=>o});const i={logger:self.console,WebSocket:self.WebSocket},o={log(...t){this.enabled&&(t.push(Date.now()),i.logger.log("[ActionCable]",...t))}},s=()=>(new Date).getTime(),r=t=>(s()-t)/1e3;class c{constructor(t){this.visibilityDidChange=this.visibilityDidChange.bind(this),this.connection=t,this.reconnectAttempts=0}start(){this.isRunning()||(this.startedAt=s(),delete this.stoppedAt,this.startPolling(),addEventListener("visibilitychange",this.visibilityDidChange),o.log(`ConnectionMonitor started. pollInterval = ${this.getPollInterval()} ms`))}stop(){this.isRunning()&&(this.stoppedAt=s(),this.stopPolling(),removeEventListener("visibilitychange",this.visibilityDidChange),o.log("ConnectionMonitor stopped"))}isRunning(){return this.startedAt&&!this.stoppedAt}recordPing(){this.pingedAt=s()}recordConnect(){this.reconnectAttempts=0,this.recordPing(),delete this.disconnectedAt,o.log("ConnectionMonitor recorded connect")}recordDisconnect(){this.disconnectedAt=s(),o.log("ConnectionMonitor recorded disconnect")}startPolling(){this.stopPolling(),this.poll()}stopPolling(){clearTimeout(this.pollTimeout)}poll(){this.pollTimeout=setTimeout((()=>{this.reconnectIfStale(),this.poll()}),this.getPollInterval())}getPollInterval(){const{min:t,max:e,multiplier:n}=this.constructor.pollInterval,i=n*Math.log(this.reconnectAttempts+1);return Math.round(1e3*((t,e,n)=>Math.max(e,Math.min(n,t)))(i,t,e))}reconnectIfStale(){this.connectionIsStale()&&(o.log(`ConnectionMonitor detected stale connection. reconnectAttempts = ${this.reconnectAttempts}, pollInterval = ${this.getPollInterval()} ms, time disconnected = ${r(this.disconnectedAt)} s, stale threshold = ${this.constructor.staleThreshold} s`),this.reconnectAttempts++,this.disconnectedRecently()?o.log("ConnectionMonitor skipping reopening recent disconnect"):(o.log("ConnectionMonitor reopening"),this.connection.reopen()))}connectionIsStale(){return r(this.pingedAt?this.pingedAt:this.startedAt)>this.constructor.staleThreshold}disconnectedRecently(){return this.disconnectedAt&&r(this.disconnectedAt){!this.connectionIsStale()&&this.connection.isOpen()||(o.log(`ConnectionMonitor reopening stale connection on visibilitychange. visibilityState = ${document.visibilityState}`),this.connection.reopen())}),200)}}c.pollInterval={min:3,max:30,multiplier:5},c.staleThreshold=6;const l=c,h={message_types:{welcome:"welcome",disconnect:"disconnect",ping:"ping",confirmation:"confirm_subscription",rejection:"reject_subscription"},disconnect_reasons:{unauthorized:"unauthorized",invalid_request:"invalid_request",server_restart:"server_restart"},default_mount_path:"/cable",protocols:["actioncable-v1-json","actioncable-unsupported"]},{message_types:a,protocols:u}=h,d=u.slice(0,u.length-1),p=[].indexOf;class g{constructor(t){this.open=this.open.bind(this),this.consumer=t,this.subscriptions=this.consumer.subscriptions,this.monitor=new l(this),this.disconnected=!0}send(t){return!!this.isOpen()&&(this.webSocket.send(JSON.stringify(t)),!0)}open(){return this.isActive()?(o.log(`Attempted to open WebSocket, but existing socket is ${this.getState()}`),!1):(o.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${u}`),this.webSocket&&this.uninstallEventHandlers(),this.webSocket=new i.WebSocket(this.consumer.url,u),this.installEventHandlers(),this.monitor.start(),!0)}close({allowReconnect:t}={allowReconnect:!0}){if(t||this.monitor.stop(),this.isActive())return this.webSocket.close()}reopen(){if(o.log(`Reopening WebSocket, current state is ${this.getState()}`),!this.isActive())return this.open();try{return this.close()}catch(t){o.log("Failed to reopen WebSocket",t)}finally{o.log(`Reopening WebSocket in ${this.constructor.reopenDelay}ms`),setTimeout(this.open,this.constructor.reopenDelay)}}getProtocol(){if(this.webSocket)return this.webSocket.protocol}isOpen(){return this.isState("open")}isActive(){return this.isState("open","connecting")}isProtocolSupported(){return p.call(d,this.getProtocol())>=0}isState(...t){return p.call(t,this.getState())>=0}getState(){if(this.webSocket)for(let t in i.WebSocket)if(i.WebSocket[t]===this.webSocket.readyState)return t.toLowerCase();return null}installEventHandlers(){for(let t in this.events){const e=this.events[t].bind(this);this.webSocket[`on${t}`]=e}}uninstallEventHandlers(){for(let t in this.events)this.webSocket[`on${t}`]=function(){}}}g.reopenDelay=500,g.prototype.events={message(t){if(!this.isProtocolSupported())return;const{identifier:e,message:n,reason:i,reconnect:s,type:r}=JSON.parse(t.data);switch(r){case a.welcome:return this.monitor.recordConnect(),this.subscriptions.reload();case a.disconnect:return o.log(`Disconnecting. Reason: ${i}`),this.close({allowReconnect:s});case a.ping:return this.monitor.recordPing();case a.confirmation:return this.subscriptions.notify(e,"connected");case a.rejection:return this.subscriptions.reject(e);default:return this.subscriptions.notify(e,"received",n)}},open(){if(o.log(`WebSocket onopen event, using '${this.getProtocol()}' subprotocol`),this.disconnected=!1,!this.isProtocolSupported())return o.log("Protocol is unsupported. Stopping monitor and disconnecting."),this.close({allowReconnect:!1})},close(t){if(o.log("WebSocket onclose event"),!this.disconnected)return this.disconnected=!0,this.monitor.recordDisconnect(),this.subscriptions.notifyAll("disconnected",{willAttemptReconnect:this.monitor.isRunning()})},error(){o.log("WebSocket onerror event")}};const m=g;class b{constructor(t,e={},n){this.consumer=t,this.identifier=JSON.stringify(e),function(t,e){if(null!=e)for(let n in e){const i=e[n];t[n]=i}}(this,n)}perform(t,e={}){return e.action=t,this.send(e)}send(t){return this.consumer.send({command:"message",identifier:this.identifier,data:JSON.stringify(t)})}unsubscribe(){return this.consumer.subscriptions.remove(this)}}class f{constructor(t){this.consumer=t,this.subscriptions=[]}create(t,e){const n="object"==typeof t?t:{channel:t},i=new b(this.consumer,n,e);return this.add(i)}add(t){return this.subscriptions.push(t),this.consumer.ensureActiveConnection(),this.notify(t,"initialized"),this.sendCommand(t,"subscribe"),t}remove(t){return this.forget(t),this.findAll(t.identifier).length||this.sendCommand(t,"unsubscribe"),t}reject(t){return this.findAll(t).map((t=>(this.forget(t),this.notify(t,"rejected"),t)))}forget(t){return this.subscriptions=this.subscriptions.filter((e=>e!==t)),t}findAll(t){return this.subscriptions.filter((e=>e.identifier===t))}reload(){return this.subscriptions.map((t=>this.sendCommand(t,"subscribe")))}notifyAll(t,...e){return this.subscriptions.map((n=>this.notify(n,t,...e)))}notify(t,e,...n){let i;return i="string"==typeof t?this.findAll(t):[t],i.map((t=>"function"==typeof t[e]?t[e](...n):void 0))}sendCommand(t,e){const{identifier:n}=t;return this.consumer.send({command:e,identifier:n})}}class v{constructor(t){this._url=t,this.subscriptions=new f(this),this.connection=new m(this)}get url(){return S(this._url)}send(t){return this.connection.send(t)}connect(){return this.connection.open()}disconnect(){return this.connection.close({allowReconnect:!1})}ensureActiveConnection(){if(!this.connection.isActive())return this.connection.open()}}function S(t){if("function"==typeof t&&(t=t()),t&&!/^wss?:/i.test(t)){const e=document.createElement("a");return e.href=t,e.href=e.href,e.protocol=e.protocol.replace("http","ws"),e.href}return t}function y(t=A("url")||h.default_mount_path){return new v(t)}function A(t){const e=document.head.querySelector(`meta[name='action-cable-${t}']`);if(e)return e.getAttribute("content")}}}]); //# sourceMappingURL=219-9aa2b689f44613118203.chunk.js.map