Paginate, defer, and subscribe
Chapter 5 of 6
A shared shelf has a title and hundreds of entries. Decide which fields belong in the initial read, which lists can load independently, and which views need live updates.
Load a page at a time
A connection view describes the items and pagination fields the reader needs. With a server-backed Shelf that exposes a books connection, the selection can look like this:
const BookConnectionView = {
args: { first: 10 },
items: { node: BookView },
pagination: { hasNext: true, nextCursor: true },
};
const ShelfView = view<Shelf>()({
title: true,
books: BookConnectionView,
});
function ShelfBooks({ shelf }: { shelf: ViewRef<'Shelf'> }) {
const { books } = useView(ShelfView, shelf);
const [items = [], loadNext] = useListView(BookConnectionView, books);
return (
<>
<ul>
{items.map(({ node }) => (
<BookRow key={node.id} book={node} />
))}
</ul>
{loadNext && <button onClick={loadNext}>Load more</button>}
</>
);
}
BookRow reads the selected fields from BookView. When the reader loads another page, Fate sends the cursor as after and retains the original page size. When loadNext is null, there are no more pages in that direction. If a mutation adds a book, pass the original list value as an explicit insertion target with insert.into. Updating a record and adding it to a particular filtered list are separate operations.
Let the shelf title render early
Wrap the connection selection in defer(BookConnectionView) and pass the deferred list to a child under its own Suspense boundary. The child starts loading the list when it reads it. Use this when the shelf title is useful before the books finish loading.
Subscribe where it matters
Use useLiveView with a view and reference when the screen needs server changes to appear while it stays open. Live views use a shared connection for the client. The transport and server need to support live updates. The native HTTP guide shows how to publish record changes through an event bus.
Try it
On paper, sketch a shelf with a title, a list of books, and a count of readers currently active. Give each a loading and refresh policy. In a server-backed app, throttle the connection, load a second page, and change a live field from a second tab. Check which part of the first tab changes in each case.