var t=Object.defineProperty,e=(e,s,n)=>((e,s,n)=>s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[s]=n)(e,"symbol"!=typeof s?s+"":s,n);import{k as s}from"./index-D8tPjwDN.js";const n={EMPTY:"空",TO_BE_PROCESSED:"待处理",PENDING:"已挂起",RESOURCE_IS_READY:"已就绪",PROCESSING:"处理中",PROCESS_COMPLETE:"完成",ERROR:"出错"},c={EMPTY:"",TO_BE_PROCESSED:"#56ccab",PENDING:"#a793ec",RESOURCE_IS_READY:"#17b60d",PROCESSING:"#e35958",PROCESS_COMPLETE:"#773f16",ERROR:"#e93527"};class i{constructor(t){e(this,"ws",null),e(this,"url"),e(this,"reconnectAttempts",-1),e(this,"maxReconnectAttempts",5),e(this,"reconnectInterval",3e3),e(this,"messageHandlers",new Map),this.url=t}subscribe(t,e){var s;this.messageHandlers.has(t)||this.messageHandlers.set(t,new Set),null==(s=this.messageHandlers.get(t))||s.add(e)}unsubscribe(t,e){var s;null==(s=this.messageHandlers.get(t))||s.delete(e)}handleMessage(t){const e=this.messageHandlers.get(t.dataType);e&&e.forEach((e=>{try{e(t.data)}catch(s){}}))}connect(){try{this.ws&&this.ws.readyState<=1||(this.ws=new WebSocket(this.url),this.bindEvents())}catch(t){this.reconnect()}}bindEvents(){this.ws&&(this.ws.onopen=()=>{-1!==this.reconnectAttempts&&(this.reconnectAttempts=-1,s.emit("socketClosed",-1)),this.reconnectAttempts=-1},this.ws.onmessage=t=>{try{const e=JSON.parse(t.data);this.handleMessage(e)}catch(e){}},this.ws.onclose=()=>{s.emit("socketClosed",0),this.reconnect()},this.ws.onerror=t=>{})}reconnect(){if(-1===this.reconnectAttempts&&(this.reconnectAttempts=0),this.reconnectAttempts>=this.maxReconnectAttempts)return s.emit("socketClosed",this.maxReconnectAttempts),void(this.reconnectAttempts=-1);setTimeout((()=>{this.reconnectAttempts++,this.connect()}),this.reconnectInterval)}disconnect(){this.ws&&(this.ws.close(),this.ws=null)}}const a=new Map,r=t=>{if(a.has(t))return a.get(t);{const e=new i(t);return a.set(t,e),e}};export{c as a,r as c,n as e};
