var e=class{constructor(e){this.url=e,this.eventSource=new EventSource(e),this.listening={},this.clients=[],this.listen(`open`),this.listen(`close`),this.listen(`logout`),this.listen(`notification-count`),this.listen(`stopwatches`),this.listen(`error`)}register(e){this.clients.includes(e)||(this.clients.push(e),e.postMessage({type:`status`,message:`registered to ${this.url}`}))}deregister(e){let t=this.clients.indexOf(e);return t<0||this.clients.splice(t,1),this.clients.length}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=null)}listen(e){this.listening[e]||(this.listening[e]=!0,this.eventSource?.addEventListener(e,t=>{this.notifyClients({type:e,data:t.data})}))}notifyClients(e){for(let t of this.clients)t.postMessage(e)}status(e){e.postMessage({type:`status`,message:`url: ${this.url} readyState: ${this.eventSource?.readyState}`})}},t=new Map,n=new Map;self.addEventListener(`connect`,r=>{for(let i of r.ports)i.addEventListener(`message`,r=>{if(!self.EventSource){i.postMessage({type:`no-event-source`});return}if(r.data.type===`start`){let a=r.data.url,o=t.get(a);if(o){o.register(i),n.set(i,o);return}if(o=n.get(i),o){if(o.eventSource&&o.url===a)return;o.deregister(i)===0&&(o.close(),t.set(o.url,null))}o=new e(a),o.register(i),t.set(a,o),n.set(i,o)}else if(r.data.type===`listen`)n.get(i).listen(r.data.eventType);else if(r.data.type===`close`){let e=n.get(i);if(!e)return;e.deregister(i)===0&&(e.close(),t.set(e.url,null),n.set(i,null))}else if(r.data.type===`status`){let e=n.get(i);if(!e){i.postMessage({type:`status`,message:`not connected`});return}e.status(i)}else i.postMessage({type:`error`,message:`received but don't know how to handle: ${r.data}`})}),i.start()});
//# sourceMappingURL=eventsource.sharedworker.Dug1twio.js.map