feat(kit): add an opt-in live refresh to useCollection - #62
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
useCollectionloaded 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
liveis 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:
Internally
loadis split on abackgroundflag,reload()keeps its loading state (which is what a retry button wants), and arequestIdref means only the newest request may write, so a poll started before the path changed cannot land on what replaced it.refresh()joinsreload()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-appand synced, so this lands in both React starters. A test written under a template'scomponents/kitwould be deleted by the nextnpm 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
statestaysready, 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 checkis green in both templates (react-vite18 tests,react-oauth22 tests, typecheck, lint and format included), andnpm run validatepasses at the root.Not in scope
The wake path in
useCollection, the ECS cold start problem in the same file, is left alone.