Streaming rows example
Push batches of cell updates into a live ticker four times a second and choose between an immediate and a deferred re-sort.
A live ticker receives new values with updateCells, four batches a second. The Auto-sort switch sets the reorder option of each batch. With "immediate", the grid re-sorts on every batch. With "defer", the rows keep their positions. If a batch changes a value that the sort reads, a Re-sort bar appears.
Click the Change % header to sort: with Auto-sort off, rows hold their position as new values stream in and a "Re-sort" bar appears once the order goes stale; with Auto-sort on, the grid re-sorts on every tick (4/s).
pnpm dlx shadcn@latest add @gridcn/data-grid-streaming-demo
What the demo shows
- A feed patches the
changeandvolumecolumns of every row every 250 ms. The Pause feed button stops the feed and the Start feed button resumes it. - Auto-sort starts off.
reorderdefaults to"defer". - Each batch reports a change with
source: "stream", which undo history skips andonSavenever receives. - Click the Change % header to sort. A batch that touches no sorted or filtered column never reorders the rows and never shows the Re-sort bar.
- With Auto-sort on, the rows shuffle as the Change % values cross.
- With Auto-sort off, the rows hold their positions. When a batch changes a Change % value, the Re-sort bar appears. Its button calls
reconcileView().
Wire the feed
Render both components inside the provider. The feed patches by row id and column id. The bar reads useDataGridViewStale:
import { useEffect } from "react";
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
defineColumns,
useDataGridActions,
useDataGridViewStale,
type CellPatch,
} from "@/components/data-grid/data-grid";
import { Button } from "@/components/ui/button";
type Ticker = { id: string; change: number };
const columns = defineColumns<Ticker>()([
{ id: "id", header: "Symbol", accessorKey: "id" },
{ id: "change", header: "Change %", accessorKey: "change", type: "number" },
]);
const rows: Ticker[] = [
{ id: "AAPL", change: 0 },
{ id: "MSFT", change: 0 },
];
function TickerFeed({ autoSort }: { autoSort: boolean }) {
const actions = useDataGridActions();
useEffect(() => {
const timer = setInterval(() => {
const patches: CellPatch[] = rows.map((row) => ({
rowId: row.id,
columnId: "change",
value: Number((Math.random() * 4 - 2).toFixed(2)),
}));
actions.updateCells(patches, {
source: "stream",
reorder: autoSort ? "immediate" : "defer",
});
}, 250);
return () => clearInterval(timer);
}, [actions, autoSort]);
return null;
}
function ReSortBar() {
const actions = useDataGridActions();
const viewStale = useDataGridViewStale();
if (!viewStale) return null;
return (
<Button size="sm" variant="outline" onClick={() => actions.reconcileView()}>
Re-sort
</Button>
);
}
function TickerGrid({ autoSort }: { autoSort: boolean }) {
return (
<DataGridProvider
defaultData={rows}
columns={columns}
getRowId={(row) => row.id}
headerClickBehavior="sort"
>
<TickerFeed autoSort={autoSort} />
<ReSortBar />
<DataGridRoot>
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
</DataGridProvider>
);
}Feed at a high rate
A feed of 1,000 updates a second must not make 1,000 grid calls a second. This demo holds the patches in a buffer and sends them in one updateCells call per animation frame. The grid has 100,000 rows. The feed touches the first 200.
- Frames per second
- —
- p95 frame time (ms)
- —
- Applied cells per second
- —
- Grid calls per second
- —
pnpm dlx shadcn@latest add @gridcn/data-grid-stream-rate-demo
- Pause stops the feed. The feed also stops while the tab is hidden or the demo is off screen.
- The rate select sets how many patches the feed makes each second. The grid calls per second stay at or below the frames per second.
- The reorder select sets the
reorderoption of each call. Click a column header to sort, then compare the three values. - The Flash switch sets
flashon each call. - The counters update once a second. Applied cells counts the writes that changed a value.
Flush the buffer from an animation frame:
import { useEffect } from "react";
import {
useDataGridActions,
type CellPatch,
} from "@/components/data-grid/data-grid";
function FrameFeed({ buffer }: { buffer: CellPatch[] }) {
const actions = useDataGridActions();
useEffect(() => {
let frame = 0;
const flush = () => {
if (buffer.length > 0) {
const patches = buffer.splice(0);
actions.updateCells(patches, { source: "stream", reorder: "frame" });
}
frame = requestAnimationFrame(flush);
};
frame = requestAnimationFrame(flush);
return () => cancelAnimationFrame(frame);
}, [actions, buffer]);
return null;
}Related
- Stream updates:
updateCells,updateRows, and the threereordervalues - Performance guidance: what keeps a 100k-row grid smooth while it streams