fix race condition

This commit is contained in:
Chris Wanstrath 2026-03-01 09:40:35 -08:00
parent 2046af1407
commit fdc14a5021
2 changed files with 8 additions and 3 deletions

View File

@ -24,13 +24,14 @@ function convert(app: BackendApp): SharedApp {
// SSE: full app state snapshots for the dashboard UI (every state change) // SSE: full app state snapshots for the dashboard UI (every state change)
// For discrete lifecycle events consumed by app processes, see /api/events/stream // For discrete lifecycle events consumed by app processes, see /api/events/stream
router.sse('/stream', (send) => { router.sse('/stream', (send) => {
let queue = Promise.resolve()
const broadcast = () => { const broadcast = () => {
const apps: SharedApp[] = allApps().map(({ const apps: SharedApp[] = allApps().map(({
name, state, icon, error, port, started, logs, tool, tunnelEnabled, tunnelUrl name, state, icon, error, port, started, logs, tool, tunnelEnabled, tunnelUrl
}) => ({ }) => ({
name, state, icon, error, port, started, logs, tool, tunnelEnabled, tunnelUrl, name, state, icon, error, port, started, logs, tool, tunnelEnabled, tunnelUrl,
})) }))
send(apps) queue = queue.then(() => send(apps))
} }
broadcast() broadcast()

View File

@ -7,8 +7,12 @@ const router = Hype.router()
// Unlike /api/apps/stream (full state snapshots for the dashboard), this sends // Unlike /api/apps/stream (full state snapshots for the dashboard), this sends
// individual events so apps can react to specific lifecycle changes. // individual events so apps can react to specific lifecycle changes.
router.sse('/stream', (send) => { router.sse('/stream', (send) => {
const unsub = onEvent(event => send(event)) let queue = Promise.resolve()
const heartbeat = setInterval(() => send('', 'ping'), 60_000) const safeSend = (...args: Parameters<typeof send>) => {
queue = queue.then(() => send(...args))
}
const unsub = onEvent(event => safeSend(event))
const heartbeat = setInterval(() => safeSend('', 'ping'), 60_000)
return () => { return () => {
clearInterval(heartbeat) clearInterval(heartbeat)
unsub() unsub()