Files
simstudioai--sim/apps/sim/hooks/queries/utils/table-rows-pagination.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

53 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { TableRowsCursor } from '@/lib/table/types'
/**
* Infinite-rows page param: a keyset cursor on the default `(order_key, id)` order, or a numeric
* offset for sorted views / legacy rows without an order key. `0` doubles as the first page.
*/
export type TableRowsPageParam = number | TableRowsCursor
interface TableRowsPageLike {
rows: ReadonlyArray<{ id: string; orderKey?: string }>
totalCount: number | null
}
/** Rows loaded across all fetched pages. */
export function countLoadedTableRows(pages: readonly TableRowsPageLike[]): number {
return pages.reduce((sum, page) => sum + page.rows.length, 0)
}
/**
* Whether more rows may exist past the fetched pages. A page is terminal only when it is
* empty or when page 0's `COUNT(*)` is already covered — never when it is merely shorter
* than the requested page size, so a short server page can never be misread as end-of-table.
*
* `totalCount` is advisory (computed in a separate transaction from the page read). A
* stale-high count self-corrects via the empty-page rule at the cost of one extra request;
* a stale-low count (rows deleted after page 0's COUNT) stops the drain early — accepted,
* since the view is already stale and the run-stream/interval invalidations refetch it.
*/
export function hasMoreTableRows(pages: readonly TableRowsPageLike[]): boolean {
const lastPage = pages[pages.length - 1]
if (!lastPage || lastPage.rows.length === 0) return false
const totalCount = pages[0].totalCount
return totalCount == null || countLoadedTableRows(pages) < totalCount
}
/**
* Continuation for the next page: a keyset cursor from the last loaded row on the default
* order, else the absolute offset — the actual loaded-row count, not pages × pageSize, so
* short pages resume without gaps.
*/
export function getNextTableRowsPageParam(
pages: readonly TableRowsPageLike[],
sorted: boolean
): TableRowsPageParam | undefined {
if (!hasMoreTableRows(pages)) return undefined
const lastPage = pages[pages.length - 1]
if (!sorted) {
const last = lastPage.rows[lastPage.rows.length - 1]
if (last?.orderKey) return { orderKey: last.orderKey, id: last.id }
}
return countLoadedTableRows(pages)
}