heartbeat

This commit is contained in:
Chris Wanstrath 2026-02-27 15:14:22 -08:00
parent cfba207077
commit 55316027c0
2 changed files with 8 additions and 2 deletions

View File

@ -8,7 +8,11 @@ const router = Hype.router()
// 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)) const unsub = onEvent(event => send(event))
return unsub const heartbeat = setInterval(() => send('', 'ping'), 60_000)
return () => {
clearInterval(heartbeat)
unsub()
}
}) })
export default router export default router

View File

@ -35,8 +35,10 @@ function ensureConnection() {
buf = lines.pop()! buf = lines.pop()!
for (const line of lines) { for (const line of lines) {
if (!line.startsWith('data: ')) continue if (!line.startsWith('data: ')) continue
const payload = line.slice(6)
if (!payload) continue
try { try {
const event: ToesEvent = JSON.parse(line.slice(6)) const event: ToesEvent = JSON.parse(payload)
_listeners.forEach(l => { _listeners.forEach(l => {
if (l.types.includes(event.type)) l.callback(event) if (l.types.includes(event.type)) l.callback(event)
}) })