// @bun
var{defineProperty:H,getOwnPropertyNames:X,getOwnPropertyDescriptor:Y}=Object,Z=Object.prototype.hasOwnProperty;function $(j){return this[j]}var _=(j)=>{var k=(L??=new WeakMap).get(j),q;if(k)return k;if(k=H({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var A of X(j))if(!Z.call(k,A))H(k,A,{get:$.bind(j,A),enumerable:!(q=Y(j,A))||q.enumerable})}return L.set(j,k),k},L;var x=(j)=>j;function C(j,k){this[j]=x.bind(null,k)}var S=(j,k)=>{for(var q in k)H(j,q,{get:k[q],enumerable:!0,configurable:!0,set:C.bind(k,q)})};var w=(j,k)=>()=>(j&&(k=j(j=0)),k);var f=import.meta.require;var u={};S(u,{toUsers:()=>y,toUser:()=>p,stopBroadcasting:()=>I,startBroadcasting:()=>b,loadChannels:()=>N,getBroadcastServer:()=>P,channel:()=>h,broadcastPrivate:()=>R,broadcastPresence:()=>B,broadcast:()=>Q,BroadcastServer:()=>m,Broadcast:()=>l});import{Broadcast as E,BroadcastServer as O}from"ts-broadcasting";import{channel as h}from"ts-broadcasting";import{Broadcast as l,BroadcastServer as m}from"ts-broadcasting";function P(){return z}async function b(j){if(z)return z;let k=j.driver??"bun",q=j.host??"0.0.0.0",A=j.port??6001,K=j.scheme??"ws",F={verbose:j.verbose??!1,driver:k,default:k,connections:{[k]:{driver:k,host:q,port:A,scheme:K}}};return z=new O(F),E.setServer(z),await z.start(),z}async function I(){for(let j of J)clearInterval(j);if(J.length=0,!z)return;await z.stop(),z=null}async function N(j){let k=await import(j),q=k.default??k;if(typeof q!=="function")throw Error(`routes/channels.ts must export a default function, got ${typeof q}`);let A=0;return q({channel(F,D){if(A++,D.authorize&&z)z.channels.channel(F,D.authorize);if(D.interval&&D.data){let T=D.event??`${F}.updated`,U=D.data,V=setInterval(async()=>{try{let W=await U();Q(F,T,W)}catch{}},D.interval);J.push(V)}}}),A}function G(){if(z)return!0;if(!M)M=!0,console.warn("[stx] broadcast() called before server started \u2014 messages will be sent once broadcasting is ready");return!1}function Q(j,k,q){if(!G())return;E.send(j,k,q)}function p(j,k,q){if(!G())return;E.toUser(j,k,q)}function y(j,k,q){if(!G())return;E.toUsers(j,k,q)}function R(j,k,q){if(!G())return;E.send(`private-${j}`,k,q)}function B(j,k,q){if(!G())return;E.presence(j,k,q)}var z=null,J,M=!1;var g=w(()=>{J=[]});g();export{y as toUsers,p as toUser,I as stopBroadcasting,b as startBroadcasting,N as loadChannels,P as getBroadcastServer,h as channel,R as broadcastPrivate,B as broadcastPresence,Q as broadcast,m as BroadcastServer,l as Broadcast};
