@wcstack/websocket 4.18.0 → 4.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auto.min.js +1 -1
- package/dist/index.esm.js +1 -1
- package/package.json +1 -1
package/dist/auto.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e={triggerAttribute:"data-wstarget",tagNames:{ws:"wcs-ws"}};var t="@wcstack/websocket",s="4.18.0";const n="invalid-argument",r="invalid-state",i="connection-error";class o extends EventTarget{static wcBindable={protocol:"wc-bindable",version:1,properties:[{name:"message",event:"wcs-ws:message",semantics:"event"},{name:"connected",event:"wcs-ws:connected-changed",semantics:"state"},{name:"loading",event:"wcs-ws:loading-changed",semantics:"state"},{name:"error",event:"wcs-ws:error",semantics:"state"},{name:"errorInfo",event:"wcs-ws:error-info-changed",semantics:"state"},{name:"readyState",event:"wcs-ws:readystate-changed",semantics:"state"}],commands:[{name:"connect"},{name:"send"},{name:"close"}]};_target;_ws=null;_message=null;_connected=!1;_loading=!1;_error=null;_errorInfo=null;_readyState=3;_autoReconnect=!1;_reconnectInterval=3e3;_maxReconnects=1/0;_reconnectCount=0;_reconnectTimer=null;_url="";_protocols=void 0;_binaryType="blob";_intentionalClose=!1;_staleAfter=0;_lastActivity=0;_staleTimer=null;_gen=0;_socketGen=0;_ready=Promise.resolve();_toldNotReady=!1;constructor(e){super(),this._target=e??this}get ready(){return this._ready}observe(){return this._ready}dispose(){this._end(!1)}suspend(){const e=null!==this._ws||null!==this._reconnectTimer;return this._end(!0),e}_end(e){this._gen++,this._stop(void 0,void 0,e),this._publishClosed()}_publishClosed(){this._setConnected(!1),this._setLoading(!1),this._setReadyState(3)}get message(){return this._message}get connected(){return this._connected}get loading(){return this._loading}get error(){return this._error}get errorInfo(){return this._errorInfo}get readyState(){return this._readyState}_setMessage(e){this._message=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:message",{detail:e,bubbles:!0}))}_setConnected(e){this._connected=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:connected-changed",{detail:e,bubbles:!0}))}_setLoading(e){this._loading=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:loading-changed",{detail:e,bubbles:!0}))}_setError(e,t){this._error!==e&&(this._error=e,this._commitErrorInfo(null===e?null:function(e,t){const s=e.message,o="string"==typeof s?s:"WebSocket connection error";return t===n?{code:n,phase:"start",recoverable:!1,message:o}:t===r?{code:r,phase:"execute",recoverable:!1,message:o}:{code:i,phase:"execute",recoverable:!0,message:o}}(e,t)),this._target.dispatchEvent(new CustomEvent("wcs-ws:error",{detail:e,bubbles:!0})))}_commitErrorInfo(e){this._errorInfo=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:error-info-changed",{detail:e,bubbles:!0}))}_setReadyState(e){this._readyState=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:readystate-changed",{detail:e,bubbles:!0}))}connect(e,t={}){e?(this._gen++,this._intentionalClose=!0,this._closeInternal(),this._url=e,this._protocols=t.protocols,this._autoReconnect=t.autoReconnect??!1,this._reconnectInterval=t.reconnectInterval??3e3,this._maxReconnects=t.maxReconnects??1/0,this._binaryType=t.binaryType??"blob",this._staleAfter=void 0!==t.staleAfter&&t.staleAfter>0?t.staleAfter:0,this._reconnectCount=0,this._intentionalClose=!1,this._doConnect()):this._setError({message:"url is required."},n)}send(e){if(!this._ws||1!==this._ws.readyState)return this._setError({message:"WebSocket is not connected."},r),void this._warnNotReady();this._ws.send(e)}_warnNotReady(){this._toldNotReady||(this._toldNotReady=!0,console.warn("[@wcstack/websocket] [wcs/command-not-ready] send() was refused: the WebSocket is not connected, so the message is lost. It goes to `error` too. Send once `connected` is true. (Said once until the WebSocket connects again.)",this._target))}close(e,t){this._stop(e,t,!1)&&this._publishClosed()}_stop(e,t,s){this._intentionalClose=!0,this._clearReconnectTimer(),this._clearStaleTimer();const n=this._ws;return null!==n&&(s||0!==n.readyState?(n.close(e,t),!1):(this._removeListeners(),this._ws=null,this._closeWhenOpen(n,e,t),!0))}_closeWhenOpen(e,t,s){const n=()=>{e.removeEventListener("open",r),e.removeEventListener("error",n),e.removeEventListener("close",n)},r=()=>{n();try{e.close(t,s)}catch{e.close()}};e.addEventListener("open",r),e.addEventListener("error",n),e.addEventListener("close",n)}_doConnect(){this._setLoading(!0),this._setError(null),this._socketGen=this._gen;try{this._ws=this._protocols?new WebSocket(this._url,this._protocols):new WebSocket(this._url)}catch(e){return this._setLoading(!1),void this._setError(e,i)}this._ws.binaryType=this._binaryType,this._setReadyState(0),this._ws.addEventListener("open",this._onOpen),this._ws.addEventListener("message",this._onMessage),this._ws.addEventListener("error",this._onError),this._ws.addEventListener("close",this._onClose)}_onOpen=()=>{this._socketGen===this._gen&&(this._reconnectCount=0,this._toldNotReady=!1,this._lastActivity=Date.now(),this._armStale(),this._setLoading(!1),this._setConnected(!0),this._setReadyState(1))};_onMessage=e=>{if(this._socketGen!==this._gen)return;this._lastActivity=Date.now();let t=e.data;if("string"==typeof t)try{t=JSON.parse(t)}catch{}this._setMessage(t)};_onError=e=>{this._socketGen===this._gen&&this._setError(e,i)};_onClose=e=>{this._removeListeners(),this._ws=null,this._socketGen===this._gen&&(this._setConnected(!1),this._setLoading(!1),this._setReadyState(3),!this._intentionalClose&&this._autoReconnect&&1e3!==e.code&&this._reconnectCount<this._maxReconnects&&this._scheduleReconnect())};_scheduleReconnect(){const e=this._gen;this._clearReconnectTimer(),this._reconnectTimer=setTimeout(()=>{this._reconnectTimer=null,e===this._gen&&(this._reconnectCount++,this._doConnect())},this._reconnectInterval)}_armStale(){0!==this._staleAfter&&null===this._staleTimer&&(this._staleTimer=setTimeout(this._checkStale,this._staleAfter))}_checkStale=()=>{this._staleTimer=null;const e=this._ws;if(null===e||1!==this._readyState)return;const t=Date.now()-this._lastActivity;if(t<this._staleAfter)this._staleTimer=setTimeout(this._checkStale,this._staleAfter-t);else{this._removeListeners(),this._ws=null;try{e.close(4e3,"stale")}catch{}this._setConnected(!1),this._setLoading(!1),this._setReadyState(3),this._setError({message:`No message for ${this._staleAfter} ms: the connection is treated as lost.`},i),!this._intentionalClose&&this._autoReconnect&&this._reconnectCount<this._maxReconnects&&this._scheduleReconnect()}};_clearStaleTimer(){null!==this._staleTimer&&(clearTimeout(this._staleTimer),this._staleTimer=null)}_clearReconnectTimer(){null!==this._reconnectTimer&&(clearTimeout(this._reconnectTimer),this._reconnectTimer=null)}_removeListeners(){this._ws&&(this._ws.removeEventListener("open",this._onOpen),this._ws.removeEventListener("message",this._onMessage),this._ws.removeEventListener("error",this._onError),this._ws.removeEventListener("close",this._onClose))}_closeInternal(){this._clearReconnectTimer(),this._clearStaleTimer();const e=this._ws;e&&(this._removeListeners(),this._ws=null,1===e.readyState?e.close():0===e.readyState&&this._closeWhenOpen(e)),this._connected&&this._setConnected(!1),3!==this._readyState&&this._setReadyState(3)}}let a=!1;function c(t){const s=t.target;if(!(s instanceof Element))return;const n=s.closest(`[${e.triggerAttribute}]`);if(!n)return;const r=n.getAttribute(e.triggerAttribute);if(!r)return;const i=customElements.get(e.tagNames.ws),o=document.getElementById(r);i&&o instanceof i&&(t.preventDefault(),o.connect())}function h(e,t){let s=Object.getPrototypeOf(e);for(;null!==s;){const e=Object.getOwnPropertyDescriptor(s,t);if(void 0!==e)return"function"==typeof e.get||"function"==typeof e.set;s=Object.getPrototypeOf(s)}return!1}const l=new WeakSet;function _(e,t,s){if(l.has(e))return;l.add(e);const n=e.localName??"element";console.warn(`[wcs/input-as-attribute] <${n}> has the attribute ${t}="${s}": an object was written as an attribute, so the element did not receive it. A framework does that when the property is set before the element's definition loads (React 19), or when told to (Vue's .attr). Load the definition before rendering, or assign the property through a ref (Vue: .prop). (Said once per element.)`,e)}const d=new WeakMap;function u(e,t){let s=d.get(e);return void 0===s&&(s=new Map,d.set(e,s)),s.has(t)||s.set(t,e.getAttribute(t)),s.get(t)}function g(e,t,s){null===s?e.removeAttribute(t):e.setAttribute(t,s)}function m(e,t,s){const n=u(e,t);g(e,t,void 0===s?n:null===s?null:String(s))}function w(e,t,s){const n=u(e,t);g(e,t,void 0===s?n:s?"":null)}class b extends HTMLElement{static hasConnectedCallbackPromise=!0;static wcBindable={...o.wcBindable,properties:[...o.wcBindable.properties,{name:"trigger",event:"wcs-ws:trigger-changed",semantics:"state"},{name:"send",event:"wcs-ws:send-changed",semantics:"state"}],inputs:[{name:"url",attribute:"url"},{name:"protocols",attribute:"protocols"},{name:"autoReconnect",attribute:"auto-reconnect"},{name:"reconnectInterval",attribute:"reconnect-interval"},{name:"maxReconnects",attribute:"max-reconnects"},{name:"binaryType",attribute:"binary-type"},{name:"staleAfter",attribute:"stale-after"},{name:"manual",attribute:"manual"},{name:"trigger"},{name:"send"}],commands:[{name:"connect"},{name:"sendMessage"},{name:"close"}]};static get observedAttributes(){return["url"]}_core;_trigger=!1;_connectedCallbackPromise=Promise.resolve();_internals=null;_attached=!1;_connectPending=!1;constructor(){super(),this._core=new o(this),this._internals=this._initInternals(),this._wireStates({"wcs-ws:connected-changed":e=>({connected:!0===e}),"wcs-ws:loading-changed":e=>({loading:!0===e}),"wcs-ws:error":e=>({error:null!=e})})}get connectedCallbackPromise(){return this._connectedCallbackPromise}get debugStates(){return this._internals?[...this._internals.states]:[]}_initInternals(){try{if("function"!=typeof this.attachInternals)return null;const e=this.attachInternals();return e.states.add("wcs-probe"),e.states.delete("wcs-probe"),e}catch{return null}}_wireStates(e){if(null===this._internals)return;const t=this._internals.states;for(const[s,n]of Object.entries(e))this.addEventListener(s,e=>{const s=this.hasAttribute("debug-states");for(const[r,i]of Object.entries(n(e.detail))){try{i?t.add(r):t.delete(r)}catch{}s&&this.toggleAttribute(`data-wcs-state-${r}`,i)}})}get url(){return this.getAttribute("url")||""}set url(e){m(this,"url",e)}get protocols(){return this.getAttribute("protocols")||""}set protocols(e){m(this,"protocols",e)}get autoReconnect(){return this.hasAttribute("auto-reconnect")}set autoReconnect(e){w(this,"auto-reconnect",e)}get reconnectInterval(){const e=this.getAttribute("reconnect-interval"),t=e?parseInt(e,10):3e3;return Number.isNaN(t)?3e3:t}set reconnectInterval(e){m(this,"reconnect-interval",e)}get maxReconnects(){const e=this.getAttribute("max-reconnects"),t=e?parseInt(e,10):1/0;return Number.isNaN(t)?1/0:t}set maxReconnects(e){m(this,"max-reconnects",e)}get binaryType(){return"arraybuffer"===this.getAttribute("binary-type")?"arraybuffer":"blob"}set binaryType(e){m(this,"binary-type",e)}get staleAfter(){const e=Number(this.getAttribute("stale-after"));return Number.isFinite(e)&&e>0?e:0}set staleAfter(e){m(this,"stale-after",e)}get manual(){return this.hasAttribute("manual")}set manual(e){w(this,"manual",e)}get message(){return this._core.message}get connected(){return this._core.connected}get loading(){return this._core.loading}get error(){return this._core.error}get errorInfo(){return this._core.errorInfo}get readyState(){return this._core.readyState}get trigger(){return this._trigger}set trigger(e){!!e&&(this._trigger=!0,this.connect(),this._trigger=!1,this.dispatchEvent(new CustomEvent("wcs-ws:trigger-changed",{detail:!1,bubbles:!0})))}get send(){return null}set send(e){if(null==e)return;const t="string"==typeof e?e:JSON.stringify(e);this._core.send(t),this.dispatchEvent(new CustomEvent("wcs-ws:send-changed",{detail:null,bubbles:!0}))}connect(){this._connectPending=!1;const e=this.protocols?this.protocols.split(",").map(e=>e.trim()).filter(Boolean):void 0;this._core.connect(this.url,{protocols:e&&1===e.length?e[0]:e,autoReconnect:this.autoReconnect,reconnectInterval:this.reconnectInterval,maxReconnects:this.maxReconnects,binaryType:this.binaryType,staleAfter:this.staleAfter})}sendMessage(e){this._core.send(e)}close(e,t){this._connectPending=!1,this._core.close(e,t)}attributeChangedCallback(e,t,s){"url"===e&&t!==s&&this._attached&&!this.manual&&s&&this._scheduleConnect()}_scheduleConnect(){this._connectPending||(this._connectPending=!0,queueMicrotask(()=>{this._connectPending&&(this._connectPending=!1,this._attached&&!this.manual&&this.url&&this.connect())}))}connectedCallback(){!function(e){const t=e.constructor?.wcBindable,s=t?.inputs;if(void 0===s)return;const n=e.getAttribute;for(const t of s){const s=t.name,r="function"==typeof n?n.call(e,s.toLowerCase()):null;if(null!==r&&r.startsWith("[object ")&&_(e,s,r),!Object.prototype.hasOwnProperty.call(e,s))continue;if(!h(e,s))continue;const i=e,o=i[s];delete i[s],i[s]=o}}(this),this.style.display="none",a||(a=!0,document.addEventListener("click",c)),this._connectedCallbackPromise=this._core.observe(),this._attached=!0,window.addEventListener("pagehide",this._onPageHide),window.addEventListener("pageshow",this._onPageShow),this._scheduleConnect()}_reopenOnShow=!1;_onPageHide=e=>{e.persisted&&this._core.suspend()&&(this._reopenOnShow=!0)};_onPageShow=e=>{e.persisted&&this._reopenOnShow&&(this._reopenOnShow=!1,this.url&&this.connect())};disconnectedCallback(){this._attached=!1,this._reopenOnShow=!1,window.removeEventListener("pagehide",this._onPageHide),window.removeEventListener("pageshow",this._onPageShow),this._core.dispose()}}const p=Symbol.for("wcstack.package"),v=new WeakSet;function f(e,t){Object.defineProperty(e,p,{value:t,configurable:!0})}function y(e,t,s,n,r){if(void 0!==n&&(s===n||s.prototype instanceof n))return;const i=s[p];if(i===r||v.has(e))return;v.add(e);const o=r.slice(0,r.lastIndexOf("@"));console.warn(`[${o}] [wcs/package-twice] <${t}> is already defined by ${i??`another copy of ${o} that does not name its version`}; this copy, ${r}, does nothing. Load each package once.`)}function E(n=customElements){!function(e,t,s){for(const[n,r]of s){const s=e.get(n);void 0===s?(f(r,t),e.define(n,r)):y(e,n,s,r,t)}}(n,`${t}@${s}`,[[e.tagNames.ws,b]])}var S;E(S);
|
|
1
|
+
const e={triggerAttribute:"data-wstarget",tagNames:{ws:"wcs-ws"}};var t="@wcstack/websocket",s="4.20.0";const n="invalid-argument",r="invalid-state",i="connection-error";class o extends EventTarget{static wcBindable={protocol:"wc-bindable",version:1,properties:[{name:"message",event:"wcs-ws:message",semantics:"event"},{name:"connected",event:"wcs-ws:connected-changed",semantics:"state"},{name:"loading",event:"wcs-ws:loading-changed",semantics:"state"},{name:"error",event:"wcs-ws:error",semantics:"state"},{name:"errorInfo",event:"wcs-ws:error-info-changed",semantics:"state"},{name:"readyState",event:"wcs-ws:readystate-changed",semantics:"state"}],commands:[{name:"connect"},{name:"send"},{name:"close"}]};_target;_ws=null;_message=null;_connected=!1;_loading=!1;_error=null;_errorInfo=null;_readyState=3;_autoReconnect=!1;_reconnectInterval=3e3;_maxReconnects=1/0;_reconnectCount=0;_reconnectTimer=null;_url="";_protocols=void 0;_binaryType="blob";_intentionalClose=!1;_staleAfter=0;_lastActivity=0;_staleTimer=null;_gen=0;_socketGen=0;_ready=Promise.resolve();_toldNotReady=!1;constructor(e){super(),this._target=e??this}get ready(){return this._ready}observe(){return this._ready}dispose(){this._end(!1)}suspend(){const e=null!==this._ws||null!==this._reconnectTimer;return this._end(!0),e}_end(e){this._gen++,this._stop(void 0,void 0,e),this._publishClosed()}_publishClosed(){this._setConnected(!1),this._setLoading(!1),this._setReadyState(3)}get message(){return this._message}get connected(){return this._connected}get loading(){return this._loading}get error(){return this._error}get errorInfo(){return this._errorInfo}get readyState(){return this._readyState}_setMessage(e){this._message=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:message",{detail:e,bubbles:!0}))}_setConnected(e){this._connected=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:connected-changed",{detail:e,bubbles:!0}))}_setLoading(e){this._loading=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:loading-changed",{detail:e,bubbles:!0}))}_setError(e,t){this._error!==e&&(this._error=e,this._commitErrorInfo(null===e?null:function(e,t){const s=e.message,o="string"==typeof s?s:"WebSocket connection error";return t===n?{code:n,phase:"start",recoverable:!1,message:o}:t===r?{code:r,phase:"execute",recoverable:!1,message:o}:{code:i,phase:"execute",recoverable:!0,message:o}}(e,t)),this._target.dispatchEvent(new CustomEvent("wcs-ws:error",{detail:e,bubbles:!0})))}_commitErrorInfo(e){this._errorInfo=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:error-info-changed",{detail:e,bubbles:!0}))}_setReadyState(e){this._readyState=e,this._target.dispatchEvent(new CustomEvent("wcs-ws:readystate-changed",{detail:e,bubbles:!0}))}connect(e,t={}){e?(this._gen++,this._intentionalClose=!0,this._closeInternal(),this._url=e,this._protocols=t.protocols,this._autoReconnect=t.autoReconnect??!1,this._reconnectInterval=t.reconnectInterval??3e3,this._maxReconnects=t.maxReconnects??1/0,this._binaryType=t.binaryType??"blob",this._staleAfter=void 0!==t.staleAfter&&t.staleAfter>0?t.staleAfter:0,this._reconnectCount=0,this._intentionalClose=!1,this._doConnect()):this._setError({message:"url is required."},n)}send(e){if(!this._ws||1!==this._ws.readyState)return this._setError({message:"WebSocket is not connected."},r),void this._warnNotReady();this._ws.send(e)}_warnNotReady(){this._toldNotReady||(this._toldNotReady=!0,console.warn("[@wcstack/websocket] [wcs/command-not-ready] send() was refused: the WebSocket is not connected, so the message is lost. It goes to `error` too. Send once `connected` is true. (Said once until the WebSocket connects again.)",this._target))}close(e,t){this._stop(e,t,!1)&&this._publishClosed()}_stop(e,t,s){this._intentionalClose=!0,this._clearReconnectTimer(),this._clearStaleTimer();const n=this._ws;return null!==n&&(s||0!==n.readyState?(n.close(e,t),!1):(this._removeListeners(),this._ws=null,this._closeWhenOpen(n,e,t),!0))}_closeWhenOpen(e,t,s){const n=()=>{e.removeEventListener("open",r),e.removeEventListener("error",n),e.removeEventListener("close",n)},r=()=>{n();try{e.close(t,s)}catch{e.close()}};e.addEventListener("open",r),e.addEventListener("error",n),e.addEventListener("close",n)}_doConnect(){this._setLoading(!0),this._setError(null),this._socketGen=this._gen;try{this._ws=this._protocols?new WebSocket(this._url,this._protocols):new WebSocket(this._url)}catch(e){return this._setLoading(!1),void this._setError(e,i)}this._ws.binaryType=this._binaryType,this._setReadyState(0),this._ws.addEventListener("open",this._onOpen),this._ws.addEventListener("message",this._onMessage),this._ws.addEventListener("error",this._onError),this._ws.addEventListener("close",this._onClose)}_onOpen=()=>{this._socketGen===this._gen&&(this._reconnectCount=0,this._toldNotReady=!1,this._lastActivity=Date.now(),this._armStale(),this._setLoading(!1),this._setConnected(!0),this._setReadyState(1))};_onMessage=e=>{if(this._socketGen!==this._gen)return;this._lastActivity=Date.now();let t=e.data;if("string"==typeof t)try{t=JSON.parse(t)}catch{}this._setMessage(t)};_onError=e=>{this._socketGen===this._gen&&this._setError(e,i)};_onClose=e=>{this._removeListeners(),this._ws=null,this._socketGen===this._gen&&(this._setConnected(!1),this._setLoading(!1),this._setReadyState(3),!this._intentionalClose&&this._autoReconnect&&1e3!==e.code&&this._reconnectCount<this._maxReconnects&&this._scheduleReconnect())};_scheduleReconnect(){const e=this._gen;this._clearReconnectTimer(),this._reconnectTimer=setTimeout(()=>{this._reconnectTimer=null,e===this._gen&&(this._reconnectCount++,this._doConnect())},this._reconnectInterval)}_armStale(){0!==this._staleAfter&&null===this._staleTimer&&(this._staleTimer=setTimeout(this._checkStale,this._staleAfter))}_checkStale=()=>{this._staleTimer=null;const e=this._ws;if(null===e||1!==this._readyState)return;const t=Date.now()-this._lastActivity;if(t<this._staleAfter)this._staleTimer=setTimeout(this._checkStale,this._staleAfter-t);else{this._removeListeners(),this._ws=null;try{e.close(4e3,"stale")}catch{}this._setConnected(!1),this._setLoading(!1),this._setReadyState(3),this._setError({message:`No message for ${this._staleAfter} ms: the connection is treated as lost.`},i),!this._intentionalClose&&this._autoReconnect&&this._reconnectCount<this._maxReconnects&&this._scheduleReconnect()}};_clearStaleTimer(){null!==this._staleTimer&&(clearTimeout(this._staleTimer),this._staleTimer=null)}_clearReconnectTimer(){null!==this._reconnectTimer&&(clearTimeout(this._reconnectTimer),this._reconnectTimer=null)}_removeListeners(){this._ws&&(this._ws.removeEventListener("open",this._onOpen),this._ws.removeEventListener("message",this._onMessage),this._ws.removeEventListener("error",this._onError),this._ws.removeEventListener("close",this._onClose))}_closeInternal(){this._clearReconnectTimer(),this._clearStaleTimer();const e=this._ws;e&&(this._removeListeners(),this._ws=null,1===e.readyState?e.close():0===e.readyState&&this._closeWhenOpen(e)),this._connected&&this._setConnected(!1),3!==this._readyState&&this._setReadyState(3)}}let a=!1;function c(t){const s=t.target;if(!(s instanceof Element))return;const n=s.closest(`[${e.triggerAttribute}]`);if(!n)return;const r=n.getAttribute(e.triggerAttribute);if(!r)return;const i=customElements.get(e.tagNames.ws),o=document.getElementById(r);i&&o instanceof i&&(t.preventDefault(),o.connect())}function h(e,t){let s=Object.getPrototypeOf(e);for(;null!==s;){const e=Object.getOwnPropertyDescriptor(s,t);if(void 0!==e)return"function"==typeof e.get||"function"==typeof e.set;s=Object.getPrototypeOf(s)}return!1}const l=new WeakSet;function _(e,t,s){if(l.has(e))return;l.add(e);const n=e.localName??"element";console.warn(`[wcs/input-as-attribute] <${n}> has the attribute ${t}="${s}": an object was written as an attribute, so the element did not receive it. A framework does that when the property is set before the element's definition loads (React 19), or when told to (Vue's .attr). Load the definition before rendering, or assign the property through a ref (Vue: .prop). (Said once per element.)`,e)}const d=new WeakMap;function u(e,t){let s=d.get(e);return void 0===s&&(s=new Map,d.set(e,s)),s.has(t)||s.set(t,e.getAttribute(t)),s.get(t)}function g(e,t,s){null===s?e.removeAttribute(t):e.setAttribute(t,s)}function m(e,t,s){const n=u(e,t);g(e,t,void 0===s?n:null===s?null:String(s))}function w(e,t,s){const n=u(e,t);g(e,t,void 0===s?n:s?"":null)}class b extends HTMLElement{static hasConnectedCallbackPromise=!0;static wcBindable={...o.wcBindable,properties:[...o.wcBindable.properties,{name:"trigger",event:"wcs-ws:trigger-changed",semantics:"state"},{name:"send",event:"wcs-ws:send-changed",semantics:"state"}],inputs:[{name:"url",attribute:"url"},{name:"protocols",attribute:"protocols"},{name:"autoReconnect",attribute:"auto-reconnect"},{name:"reconnectInterval",attribute:"reconnect-interval"},{name:"maxReconnects",attribute:"max-reconnects"},{name:"binaryType",attribute:"binary-type"},{name:"staleAfter",attribute:"stale-after"},{name:"manual",attribute:"manual"},{name:"trigger"},{name:"send"}],commands:[{name:"connect"},{name:"sendMessage"},{name:"close"}]};static get observedAttributes(){return["url"]}_core;_trigger=!1;_connectedCallbackPromise=Promise.resolve();_internals=null;_attached=!1;_connectPending=!1;constructor(){super(),this._core=new o(this),this._internals=this._initInternals(),this._wireStates({"wcs-ws:connected-changed":e=>({connected:!0===e}),"wcs-ws:loading-changed":e=>({loading:!0===e}),"wcs-ws:error":e=>({error:null!=e})})}get connectedCallbackPromise(){return this._connectedCallbackPromise}get debugStates(){return this._internals?[...this._internals.states]:[]}_initInternals(){try{if("function"!=typeof this.attachInternals)return null;const e=this.attachInternals();return e.states.add("wcs-probe"),e.states.delete("wcs-probe"),e}catch{return null}}_wireStates(e){if(null===this._internals)return;const t=this._internals.states;for(const[s,n]of Object.entries(e))this.addEventListener(s,e=>{const s=this.hasAttribute("debug-states");for(const[r,i]of Object.entries(n(e.detail))){try{i?t.add(r):t.delete(r)}catch{}s&&this.toggleAttribute(`data-wcs-state-${r}`,i)}})}get url(){return this.getAttribute("url")||""}set url(e){m(this,"url",e)}get protocols(){return this.getAttribute("protocols")||""}set protocols(e){m(this,"protocols",e)}get autoReconnect(){return this.hasAttribute("auto-reconnect")}set autoReconnect(e){w(this,"auto-reconnect",e)}get reconnectInterval(){const e=this.getAttribute("reconnect-interval"),t=e?parseInt(e,10):3e3;return Number.isNaN(t)?3e3:t}set reconnectInterval(e){m(this,"reconnect-interval",e)}get maxReconnects(){const e=this.getAttribute("max-reconnects"),t=e?parseInt(e,10):1/0;return Number.isNaN(t)?1/0:t}set maxReconnects(e){m(this,"max-reconnects",e)}get binaryType(){return"arraybuffer"===this.getAttribute("binary-type")?"arraybuffer":"blob"}set binaryType(e){m(this,"binary-type",e)}get staleAfter(){const e=Number(this.getAttribute("stale-after"));return Number.isFinite(e)&&e>0?e:0}set staleAfter(e){m(this,"stale-after",e)}get manual(){return this.hasAttribute("manual")}set manual(e){w(this,"manual",e)}get message(){return this._core.message}get connected(){return this._core.connected}get loading(){return this._core.loading}get error(){return this._core.error}get errorInfo(){return this._core.errorInfo}get readyState(){return this._core.readyState}get trigger(){return this._trigger}set trigger(e){!!e&&(this._trigger=!0,this.connect(),this._trigger=!1,this.dispatchEvent(new CustomEvent("wcs-ws:trigger-changed",{detail:!1,bubbles:!0})))}get send(){return null}set send(e){if(null==e)return;const t="string"==typeof e?e:JSON.stringify(e);this._core.send(t),this.dispatchEvent(new CustomEvent("wcs-ws:send-changed",{detail:null,bubbles:!0}))}connect(){this._connectPending=!1;const e=this.protocols?this.protocols.split(",").map(e=>e.trim()).filter(Boolean):void 0;this._core.connect(this.url,{protocols:e&&1===e.length?e[0]:e,autoReconnect:this.autoReconnect,reconnectInterval:this.reconnectInterval,maxReconnects:this.maxReconnects,binaryType:this.binaryType,staleAfter:this.staleAfter})}sendMessage(e){this._core.send(e)}close(e,t){this._connectPending=!1,this._core.close(e,t)}attributeChangedCallback(e,t,s){"url"===e&&t!==s&&this._attached&&!this.manual&&s&&this._scheduleConnect()}_scheduleConnect(){this._connectPending||(this._connectPending=!0,queueMicrotask(()=>{this._connectPending&&(this._connectPending=!1,this._attached&&!this.manual&&this.url&&this.connect())}))}connectedCallback(){!function(e){const t=e.constructor?.wcBindable,s=t?.inputs;if(void 0===s)return;const n=e.getAttribute;for(const t of s){const s=t.name,r="function"==typeof n?n.call(e,s.toLowerCase()):null;if(null!==r&&r.startsWith("[object ")&&_(e,s,r),!Object.prototype.hasOwnProperty.call(e,s))continue;if(!h(e,s))continue;const i=e,o=i[s];delete i[s],i[s]=o}}(this),this.style.display="none",a||(a=!0,document.addEventListener("click",c)),this._connectedCallbackPromise=this._core.observe(),this._attached=!0,window.addEventListener("pagehide",this._onPageHide),window.addEventListener("pageshow",this._onPageShow),this._scheduleConnect()}_reopenOnShow=!1;_onPageHide=e=>{e.persisted&&this._core.suspend()&&(this._reopenOnShow=!0)};_onPageShow=e=>{e.persisted&&this._reopenOnShow&&(this._reopenOnShow=!1,this.url&&this.connect())};disconnectedCallback(){this._attached=!1,this._reopenOnShow=!1,window.removeEventListener("pagehide",this._onPageHide),window.removeEventListener("pageshow",this._onPageShow),this._core.dispose()}}const p=Symbol.for("wcstack.package"),v=new WeakSet;function f(e,t){Object.defineProperty(e,p,{value:t,configurable:!0})}function y(e,t,s,n,r){if(void 0!==n&&(s===n||s.prototype instanceof n))return;const i=s[p];if(i===r||v.has(e))return;v.add(e);const o=r.slice(0,r.lastIndexOf("@"));console.warn(`[${o}] [wcs/package-twice] <${t}> is already defined by ${i??`another copy of ${o} that does not name its version`}; this copy, ${r}, does nothing. Load each package once.`)}function E(n=customElements){!function(e,t,s){for(const[n,r]of s){const s=e.get(n);void 0===s?(f(r,t),e.define(n,r)):y(e,n,s,r,t)}}(n,`${t}@${s}`,[[e.tagNames.ws,b]])}var S;E(S);
|
|
2
2
|
//# sourceMappingURL=auto.min.js.map
|
package/dist/index.esm.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wcstack/websocket",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.20.0",
|
|
4
4
|
"description": "Declarative WebSocket component for Web Components. Framework-agnostic real-time communication via wc-bindable-protocol.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.esm.js",
|