gigafibre-fsm/services/targo-hub/lib/sse.js
louispaulb 320655b0a0 refactor: major cleanup — remove dead dispatch app, commit all backend code, extract client composables
- Remove apps/dispatch/ (100% replaced by ops dispatch module, unmaintained)
- Commit services/targo-hub/lib/ (24 modules, 6290 lines — was never tracked)
- Commit services/docuseal + services/legacy-db docker-compose configs
- Extract client app composables: useOTP, useAddressSearch, catalog data, format utils
- Refactor CartPage.vue 630→175 lines, CatalogPage.vue 375→95 lines
- Clean hardcoded credentials from config.js fallback values
- Add client portal: catalog, cart, checkout, OTP verification, address search
- Add ops: NetworkPage, AgentFlowsPage, ConversationPanel, UnifiedCreateModal
- Add ops composables: useBestTech, useConversations, usePermissions, useScanner
- Add field app: scanner composable, docker/nginx configs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 17:38:38 -04:00

57 lines
1.5 KiB
JavaScript

'use strict'
const { log } = require('./helpers')
const subscribers = new Map()
let clientIdSeq = 0
function addClient (topics, res, email) {
const id = ++clientIdSeq
const client = { id, res, email, topics }
for (const t of topics) {
if (!subscribers.has(t)) subscribers.set(t, new Set())
subscribers.get(t).add(client)
}
res.on('close', () => {
for (const t of topics) {
const set = subscribers.get(t)
if (set) { set.delete(client); if (!set.size) subscribers.delete(t) }
}
log(`SSE client #${id} disconnected (${email})`)
})
log(`SSE client #${id} connected (${email}) topics=[${topics.join(',')}]`)
return id
}
function broadcast (topic, event, data) {
const set = subscribers.get(topic)
if (!set?.size) return 0
const payload = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`
let count = 0
for (const c of set) { try { c.res.write(payload); count++ } catch {} }
return count
}
function broadcastAll (event, data) {
const sent = new Set()
let count = 0
for (const [, set] of subscribers) {
for (const c of set) {
if (!sent.has(c.id)) {
sent.add(c.id)
try { c.res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`); count++ } catch {}
}
}
}
return count
}
function clientCount () {
const ids = new Set()
for (const [, set] of subscribers) for (const c of set) ids.add(c.id)
return ids.size
}
function topicCount () { return subscribers.size }
module.exports = { addClient, broadcast, broadcastAll, clientCount, topicCount }