import{WebSocketServer as f,WebSocket as s}from"ws";import{logger as r}from"../tools/notifiers/logger.js";import{shutdowner as a}from"../tools/shutdowner.js";function b(i){const n=new f({server:i});a.registerShutdownCallback(()=>new Promise(e=>{r.verbose("Shutting down websocket server"),n.clients.forEach(o=>{o.readyState===s.OPEN&&o.close(1e3,"Server shutting down")}),r.verbose("Websocket connections closed"),n.close(()=>{r.verbose("Websocket server shut down"),e()})}));let c=!1;return n.on("connection",function(o){o.send(JSON.stringify({type:"ready"})),r.verbose("Hot reload client connected")}),{broadcast:e=>{c||n.clients.forEach(function(t){t.readyState===s.OPEN&&t.send(JSON.stringify(e))})},onConnect:e=>{n.on("connection",function(t){e(t)})},freeze:()=>{c=!0},unfreeze:()=>{c=!1}}}export{b as attachWsServer};
