UNPKG

@1771technologies/lytenyte-pro

Version:

Blazingly fast headless React data grid with 100s of features.

30 lines (29 loc) 1.03 kB
import { useEvent } from "@1771technologies/lytenyte-core/internal"; import {} from "../use-server-data-source.js"; export function useOnRowsUpdated(source, onRowDataChange, optimistic, globalSignal) { const onRowsUpdated = useEvent((updates) => { if (!onRowDataChange) return; if (!optimistic) { onRowDataChange?.({ rows: updates }); return; } const rollbackMap = new Map(); updates.forEach((d, row) => { const node = source.tree.rowIdToNode.get(row.id); if (node) { rollbackMap.set(row.id, { data: node.row.data, asOf: node.asOf }); } source.updateRow(row.id, d); }); source.flatten(); onRowDataChange({ rows: updates }).catch(() => { rollbackMap.forEach((d, id) => { source.updateRow(id, d.data, d.asOf); }); source.flatten(); }); globalSignal(Date.now()); }); return onRowsUpdated; }