class WSBus {
    sessions = new Map();
    listeners = new Map();
    attach(sessionId, ws) {
        this.sessions.set(sessionId, ws);
    }
    detach(sessionId) {
        this.sessions.delete(sessionId);
    }
    send(sessionId, payload) {
        const ws = this.sessions.get(sessionId);
        if (!ws)
            return false;
        try {
            ws.send(JSON.stringify(payload));
            return true;
        }
        catch {
            return false;
        }
    }
    on(sessionId, fn) {
        let set = this.listeners.get(sessionId);
        if (!set) {
            set = new Set();
            this.listeners.set(sessionId, set);
        }
        set.add(fn);
        return () => {
            set?.delete(fn);
        };
    }
    emit(sessionId, msg) {
        const set = this.listeners.get(sessionId);
        if (!set)
            return;
        for (const fn of set)
            fn(msg);
    }
}
export const bus = new WSBus();
//# sourceMappingURL=ws-bus.js.map