Skip to content

feat(kit): add an opt-in live refresh to useCollection - #62

Merged
Bccorb merged 1 commit into
mainfrom
feat/use-collection-live-refresh
Aug 26, 2026
Merged

feat(kit): add an opt-in live refresh to useCollection#62
Bccorb merged 1 commit into
mainfrom
feat/use-collection-live-refresh

Conversation

@Bccorb

@Bccorb Bccorb commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Why

useCollection loaded once. Any screen more than one person looks at went stale the moment somebody else posted, and the only way back was a reload button the reader had to know to press.

A socket server is the wrong shape for what these templates generate: an Express API behind a load balancer, with the auth wiring off limits to generation. It would mean a second server, a second protocol and an auth handshake to go with it. A poll plus a refresh on focus is the same seam at a fraction of the cost, and for a group of a dozen people it is not tellable from realtime.

What

const { records, create } = useCollection<Message>("/messages", { live: 5000 });

live is off by default, so every existing call site behaves exactly as before. When it is set, the collection refetches on that interval and also on window focus and on a tab becoming visible again, so a phone taken out of a pocket is current on the first look rather than one interval later.

A refresh is deliberately quieter than a load:

  • it never sends the screen back to its loading skeleton, so records swap underneath the reader instead of flashing;
  • it never clears an error, because the reader may be part way through one, and a refresh that fails at all is silent and tried again on the next tick;
  • it carries across a record whose create is still in flight, so an optimistic row is not taken off the screen by a poll that answered before the post landed.

Internally load is split on a background flag, reload() keeps its loading state (which is what a retry button wants), and a requestId ref means only the newest request may write, so a poll started before the path changed cannot land on what replaced it. refresh() joins reload() on the returned object for screens that know about a change themselves.

UseCollection<T> only gained a member, so nothing that reads the hook needs touching.

Where the edit went

The kit is edited in shared/react-app and synced, so this lands in both React starters. A test written under a template's components/kit would be deleted by the next npm run sync:shared, since the sync removes files with no shared source.

Tests

Five cases next to the hook: default behaviour with no options, a poll picking up a new record while state stays ready, the visibility refresh, a provisional record surviving a poll that omits it, and the interval and listeners gone after unmount.

They were mutation checked rather than only run green. Removing the provisional carry-across fails the create case, making setState("loading") unconditional fails the poll case, and dropping the effect cleanup fails the unmount case.

Checks

npm run check is green in both templates (react-vite 18 tests, react-oauth 22 tests, typecheck, lint and format included), and npm run validate passes at the root.

Not in scope

The wake path in useCollection, the ECS cold start problem in the same file, is left alone.

useCollection loaded once, so any screen more than one person looks at went
stale as soon as somebody else posted, and the only way back was a reload
button the reader had to know to press. A socket server is the wrong shape for
what these templates generate, an Express API behind a load balancer with the
auth wiring off limits, so it would mean a second server and a handshake to go
with it. A poll plus a refresh on focus is the same seam at a fraction of the
cost.

`useCollection(path, { live: 5000 })` refetches on that interval and also on
window focus and on a tab becoming visible again, so a phone taken out of a
pocket is current on the first look rather than one interval later. `live` is
off by default and every existing call site is unchanged.

A refresh is quieter than a load: it does not send the screen back to its
loading skeleton, it does not clear an error the reader may be part way
through, and it carries across a record whose create is still in flight so an
optimistic row is not taken off screen by a poll that answered before the post
landed. `refresh()` joins `reload()` on the returned object for screens that
know about a change themselves; `reload()` keeps its loading state, which is
what a retry button wants.
@Bccorb
Bccorb merged commit 21caf81 into main Aug 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant