{"version":3,"file":"pagination-state.mjs","names":["initialPagination","key","epoch","skip","failPagination","state","reconcilePagination","changed","intervals","value","results","first","mode","interval","order","next","child","item","split","children","paginationView","rows","latest","loading","splitting","nextPagination","lastKey","last","endCursor"],"sources":["../../src/react/pagination-state.ts"],"sourcesContent":["/** Pure interval controller. Keys include the epoch so late subscription results cannot enter a reset. */\nexport type ListPage = {\n\tpage: unknown[]\n\tcontinueCursor: string\n\tisDone: boolean\n\tcompleteness?: string\n\tsplitCursor?: string | null\n\tpageStatus?: 'SplitRecommended' | 'SplitRequired' | null\n\tpaginationMode?: 'native-search'\n}\ntype Interval<Page extends ListPage> = {\n\tcursor: string | null\n\tendCursor?: string\n\tvalue?: Page\n\tchildren?: [string, string]\n}\nexport type PaginationState<Page extends ListPage> = {\n\tepoch: number\n\tidentity: string\n\trestarted: boolean\n\tnext: number\n\torder: string[]\n\tintervals: Record<string, Interval<Page>>\n\tmode?: 'browse' | 'native-search'\n\terror?: Error\n}\nexport function initialPagination<Page extends ListPage>(\n\tidentity: string,\n\tepoch: number,\n\tskip: boolean,\n\trestarted = false,\n): PaginationState<Page> {\n\tconst key = `${epoch}:0`\n\treturn {\n\t\tidentity,\n\t\tepoch,\n\t\trestarted,\n\t\tnext: 1,\n\t\torder: skip ? [] : [key],\n\t\tintervals: skip ? {} : { [key]: { cursor: null } },\n\t}\n}\nexport function failPagination<Page extends ListPage>(\n\tstate: PaginationState<Page>,\n\terror: Error,\n): PaginationState<Page> {\n\treturn { ...state, order: [], intervals: {}, error }\n}\nexport function reconcilePagination<Page extends ListPage>(\n\tstate: PaginationState<Page>,\n\tresults: Record<string, Page | Error | undefined>,\n): PaginationState<Page> {\n\tif (state.error) return state\n\tlet changed = false\n\tconst intervals = { ...state.intervals }\n\tfor (const key of Object.keys(intervals)) {\n\t\tconst value = results[key]\n\t\tif (value instanceof Error) return failPagination(state, value)\n\t\tif (value !== undefined && value !== intervals[key].value) {\n\t\t\tintervals[key] = { ...intervals[key], value }\n\t\t\tchanged = true\n\t\t}\n\t}\n\t// The first result chooses the protocol for this epoch, including on reactive reruns.\n\tconst first = intervals[state.order[0]]?.value\n\tconst mode = state.mode ?? (first ? (first.paginationMode ?? 'browse') : undefined)\n\tfor (const interval of Object.values(intervals)) {\n\t\tconst value = interval.value\n\t\tif (!value) continue\n\t\tif (\n\t\t\t(value.paginationMode !== undefined && value.paginationMode !== 'native-search') ||\n\t\t\t(mode !== undefined && (value.paginationMode ?? 'browse') !== mode) ||\n\t\t\tvalue.completeness === 'searchSplitRequired' ||\n\t\t\t(mode === 'native-search' && (value.pageStatus != null || value.splitCursor != null))\n\t\t)\n\t\t\treturn failPagination(state, new Error('Invalid pagination protocol'))\n\t}\n\tlet order = state.order,\n\t\tnext = state.next\n\tfor (const key of state.order) {\n\t\tconst interval = intervals[key],\n\t\t\tvalue = interval.value\n\t\tif (!value) continue\n\t\tif (interval.children) {\n\t\t\tif (interval.children.every((child) => intervals[child].value !== undefined)) {\n\t\t\t\torder = order.flatMap((item) => (item === key ? interval.children! : [item]))\n\t\t\t\tdelete intervals[key]\n\t\t\t\tchanged = true\n\t\t\t}\n\t\t} else if (value.pageStatus === 'SplitRecommended' || value.pageStatus === 'SplitRequired') {\n\t\t\tconst split = value.splitCursor\n\t\t\tif (\n\t\t\t\t!split ||\n\t\t\t\tsplit === interval.cursor ||\n\t\t\t\tsplit === value.continueCursor ||\n\t\t\t\tsplit === interval.endCursor ||\n\t\t\t\tvalue.continueCursor === interval.cursor\n\t\t\t)\n\t\t\t\treturn failPagination(state, new Error('Non-progressing pagination split'))\n\t\t\tconst children: [string, string] = [`${state.epoch}:${next++}`, `${state.epoch}:${next++}`]\n\t\t\tintervals[key] = { ...interval, children }\n\t\t\tintervals[children[0]] = { cursor: interval.cursor, endCursor: split }\n\t\t\tintervals[children[1]] = {\n\t\t\t\tcursor: split,\n\t\t\t\tendCursor: interval.endCursor ?? value.continueCursor,\n\t\t\t}\n\t\t\tchanged = true\n\t\t} else if (\n\t\t\t!value.isDone &&\n\t\t\t(!value.continueCursor || value.continueCursor === interval.cursor)\n\t\t) {\n\t\t\treturn failPagination(state, new Error('Non-progressing pagination cursor'))\n\t\t}\n\t}\n\treturn changed ? { ...state, mode, next, order, intervals } : state\n}\nexport function paginationView<Page extends ListPage>(state: PaginationState<Page>) {\n\tconst rows: Page['page'][number][] = []\n\tlet latest: Page | undefined\n\tlet loading = false\n\tfor (const key of state.order) {\n\t\tconst interval = state.intervals[key]\n\t\tif (!interval.value) {\n\t\t\tloading = true\n\t\t\tbreak\n\t\t}\n\t\trows.push(...interval.value.page)\n\t\tlatest = interval.value\n\t}\n\tconst splitting = Object.values(state.intervals).some(\n\t\t(interval) => interval.children !== undefined,\n\t)\n\treturn {\n\t\trows,\n\t\tcompleteness: latest?.completeness,\n\t\tisLoading: !state.error && loading && latest === undefined,\n\t\tisFetchingNextPage: !state.error && ((loading && latest !== undefined) || splitting),\n\t\thasNextPage: !state.error && !loading && !splitting && latest !== undefined && !latest.isDone,\n\t}\n}\nexport function nextPagination<Page extends ListPage>(\n\tstate: PaginationState<Page>,\n): PaginationState<Page> {\n\tif (!paginationView(state).hasNextPage) return state\n\tconst lastKey = state.order[state.order.length - 1]\n\tconst last = state.intervals[lastKey]\n\tconst endCursor = last.endCursor ?? last.value!.continueCursor\n\tconst key = `${state.epoch}:${state.next}`\n\treturn {\n\t\t...state,\n\t\tnext: state.next + 1,\n\t\torder: [...state.order, key],\n\t\tintervals: {\n\t\t\t...state.intervals,\n\t\t\t// Fixed adjacent ranges keep reactive inserts/deletes from moving rows across a loaded boundary.\n\t\t\t// Native search windows are maintained by SDK journals; their original inputs stay immutable.\n\t\t\t[lastKey]: state.mode === 'native-search' ? last : { ...last, endCursor },\n\t\t\t[key]: { cursor: endCursor },\n\t\t},\n\t}\n}\n"],"mappings":"AA0BA,SAAgBA,EACf,EACA,EACA,EACA,EAAY,GACY,CACxB,IAAMC,EAAM,GAAGC,EAAM,IACrB,MAAO,CACN,WACA,QACA,YACA,KA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