Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

model.ts

36 lines1.2 KB

1export type Row = { id: number; rank: number; group: number }
2export type Scope = { group: number | undefined; descending: boolean }
3export type Window = { offset: number; limit: number | undefined }
4​
5/**
6 * # What is an opaque cursor window?
7 *
8 * An exact ordered request is a slice of the whole filtered relation. Numeric
9 * rank then ID is the declared total order in this bounded domain. Backend
10 * pages, cursor tokens, Query caches, promises, retries, and publication do not
11 * enter the value model. Their separate drivers must refine this answer.
12 */
13export function expectedRows(
14 source: ReadonlyArray<Row>,
15 scope: Scope,
16 window: Window,
17): Array<Row> {
18 const direction = scope.descending ? -1 : 1
19 return source
20 .filter((row) => scope.group === undefined || row.group === scope.group)
21 .sort((a, b) => direction * (a.rank - b.rank || a.id - b.id))
22 .slice(
23 window.offset,
24 window.limit === undefined ? undefined : window.offset + window.limit,
25 )
26 .map((row) => ({ ...row }))
28​
29export function expectedWindow(
30 source: ReadonlyArray<Row>,
31 scope: Scope,
32 n: number,
33) {
34 const all = expectedRows(source, scope, { offset: 0, limit: undefined })
35 return { rows: all.slice(0, n), hasNextPage: all.length > n }