(function(){var u=Object.defineProperty,a=(r,t,e)=>t in r?u(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,c=(r,t,e)=>a(r,typeof t!="symbol"?t+"":t,e);class h{constructor(t){c(this,"url"),c(this,"eventSource"),c(this,"listening"),c(this,"clients"),this.url=t,this.eventSource=new EventSource(t),this.listening={},this.clients=[],this.listen("open"),this.listen("close"),this.listen("logout"),this.listen("notification-count"),this.listen("stopwatches"),this.listen("error")}register(t){this.clients.includes(t)||(this.clients.push(t),t.postMessage({type:"status",message:`registered to ${this.url}`}))}deregister(t){const e=this.clients.indexOf(t);return e<0?this.clients.length:(this.clients.splice(e,1),this.clients.length)}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=null)}listen(t){this.listening[t]||(this.listening[t]=!0,this.eventSource.addEventListener(t,e=>{this.notifyClients({type:t,data:e.data})}))}notifyClients(t){for(const e of this.clients)e.postMessage(t)}status(t){t.postMessage({type:"status",message:`url: ${this.url} readyState: ${this.eventSource.readyState}`})}}const o=new Map,n=new Map;self.addEventListener("connect",r=>{for(const t of r.ports)t.addEventListener("message",e=>{if(!self.EventSource){t.postMessage({type:"no-event-source"});return}if(e.data.type==="start"){const s=e.data.url;if(o.get(s)){const l=o.get(s);l.register(t),n.set(t,l);return}let i=n.get(t);if(i){if(i.eventSource&&i.url===s)return;i.deregister(t)===0&&(i.close(),o.set(i.url,null))}i=new h(s),i.register(t),o.set(s,i),n.set(t,i)}else if(e.data.type==="listen")n.get(t).listen(e.data.eventType);else if(e.data.type==="close"){const s=n.get(t);if(!s)return;s.deregister(t)===0&&(s.close(),o.set(s.url,null),n.set(t,null))}else if(e.data.type==="status"){const s=n.get(t);if(!s){t.postMessage({type:"status",message:"not connected"});return}s.status(t)}else t.postMessage({type:"error",message:`received but don't know how to handle: ${e.data}`})}),t.start()})})();
