refactor(inventory): reuse ordered row pagination (#16441)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-03 19:01:44 +00:00
1 parent 917646c723
commit 986e5ad05d
1 file changed
+25 -29
@@ -32,7 +32,7 @@ import {
type InventorySet,
} from "../artifactInventoryEvidence.js";
import type { ArtifactInspectionResult } from "../artifactInspection.js";
import { filterSet } from "./selectViewRows.js";
import { filterSet, selectViewRows } from "./selectViewRows.js";
import type { AnalysisViewParent } from "./analysisViewParent.js";
import {
assertOwnedView,
@@ -510,10 +510,10 @@ export const projectArtifactInventoryView = (
});
}
const inventoryPage = <Items extends readonly unknown[]>({
items,
rows: items,
total,
}: {
items: Items;
rows: Items;
total: number;
}) =>
ok({
@@ -538,7 +538,7 @@ export const projectArtifactInventoryView = (
"format_record_headers pages are read from the inventoried root file; this source cannot reopen it.",
),
);
return inventoryPage({ items: [...page.items], total: page.total });
return inventoryPage({ rows: [...page.items], total: page.total });
}
if (view.collection === "occurrences")
return inventoryPage(occurrencePage(source, view));
@@ -548,11 +548,23 @@ export const projectArtifactInventoryView = (
? inventory.edges
: inventory.integrityContradictions;
return inventoryPage({
items: rows.slice(view.offset, view.offset + view.limit),
rows: rows.slice(view.offset, view.offset + view.limit),
total: rows.length,
});
};
const selectInventoryRows = <Source, Row>(
rows: readonly Source[],
indices: readonly number[] | undefined,
view: { readonly offset: number; readonly limit: number },
project: (row: Source) => Row,
) =>
indices === undefined
? selectViewRows(rows, view, undefined, project)
: selectViewRows(indices, view, undefined, (index) =>
project(rows[index]!),
);
const occurrencePage = (source: InventorySource, view: OccurrenceView) => {
const rows = source.inventory.occurrences;
const selected = occurrenceIndices(source, view);
@@ -560,15 +572,9 @@ const occurrencePage = (source: InventorySource, view: OccurrenceView) => {
view.order_by === undefined
? selected
: sizeOrder(rows, selected, view.order_by);
const total = indices?.length ?? rows.length;
const items = [];
for (
let at = view.offset;
at < Math.min(total, view.offset + view.limit);
at++
)
items.push(selectOccurrenceFields(rows[indices?.[at] ?? at]!, view.fields));
return { items, total };
return selectInventoryRows(rows, indices, view, (row) =>
selectOccurrenceFields(row, view.fields),
);
};
/** Stable size order; unknown sizes follow known ones in either direction. */
@@ -597,21 +603,11 @@ const nodePage = (
) => {
const indices = nodeIndices(source, view.artifact_ids);
const rows = source.inventory.nodes;
const total = indices?.length ?? rows.length;
const items = [];
for (
let at = view.offset;
at < Math.min(total, view.offset + view.limit);
at++
) {
const row = rows[indices?.[at] ?? at]!;
items.push(
view.fields === undefined
? row
: { ...selectFields(row, view.fields), artifact_id: row.artifact_id },
);
}
return { items, total };
return selectInventoryRows(rows, indices, view, (row) =>
view.fields === undefined
? row
: { ...selectFields(row, view.fields), artifact_id: row.artifact_id },
);
};
const inventoryCounts = (source: InventorySource): InventoryCounts => {