refactor(web): shared SSE connection, reconnect with backoff, app-level notifications #7
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/web-shared-stream"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes three Important findings from the repo-wide review in one coherent refactor:
One shared EventSource instead of three. New
src/api/stream.tsmodule owns a single connection, parses each frame once with the right zod schema (demuxed by thetypediscriminator), and fans out to per-type subscribers. Reference-counted: opens on first subscriber, tears down (including pending retry timers) on last. Previously the Notifications page ran 3 concurrent connections each parsing the full scanner payload every ~8s.Recovery from a permanently closed stream. When
onerrorfires withreadyState === CLOSED(e.g. proxy returns non-200 during a server restart — browser EventSource never retries from there), the connection is recreated with 1s→30s capped doubling backoff, reset on successful open. Transient errors keep the existing behavior (conn "stale", browser retries). Connection-status semantics in the Layout are unchanged.Desktop notifications fire on every page.
new Notification(...)moved out of the page-mounted alert hooks intouseDesktopNotifications()mounted once in App — previously notifications only worked while the user was already looking at the Notifications page. Permission flow unchanged.API changes:
useScannerStream's unusedinitialparam andlastTsreturn removed (no callers). The two alert hooks keep their public APIs and were NOT merged into one generic hook — post-refactor each is 5 lines and the generic form needed type gymnastics for zero dedup gain.All pre-existing tests pass untouched (including the view tests); 8 new tests cover connection sharing, fan-out, refcount teardown, backoff (fake timers), and notification firing from a non-Notifications view. 58 files / 319 passing, typecheck clean.
037b29caa3toc7576e1d82