@tanstack/react-db 0.1.31 → 0.1.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/useLiveInfiniteQuery.cjs +124 -0
- package/dist/cjs/useLiveInfiniteQuery.cjs.map +1 -0
- package/dist/cjs/useLiveInfiniteQuery.d.cts +82 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/useLiveInfiniteQuery.d.ts +82 -0
- package/dist/esm/useLiveInfiniteQuery.js +124 -0
- package/dist/esm/useLiveInfiniteQuery.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/useLiveInfiniteQuery.ts +303 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const useLiveQuery = require("./useLiveQuery.cjs");
|
|
4
|
+
const useLiveInfiniteQuery = require("./useLiveInfiniteQuery.cjs");
|
|
4
5
|
const db = require("@tanstack/db");
|
|
5
6
|
exports.useLiveQuery = useLiveQuery.useLiveQuery;
|
|
7
|
+
exports.useLiveInfiniteQuery = useLiveInfiniteQuery.useLiveInfiniteQuery;
|
|
6
8
|
Object.defineProperty(exports, "createTransaction", {
|
|
7
9
|
enumerable: true,
|
|
8
10
|
get: () => db.createTransaction
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const react = require("react");
|
|
4
|
+
const db = require("@tanstack/db");
|
|
5
|
+
const useLiveQuery = require("./useLiveQuery.cjs");
|
|
6
|
+
function isLiveQueryCollectionUtils(utils) {
|
|
7
|
+
return typeof utils.setWindow === `function`;
|
|
8
|
+
}
|
|
9
|
+
function useLiveInfiniteQuery(queryFnOrCollection, config, deps = []) {
|
|
10
|
+
const pageSize = config.pageSize || 20;
|
|
11
|
+
const initialPageParam = config.initialPageParam ?? 0;
|
|
12
|
+
const isCollection = queryFnOrCollection instanceof db.CollectionImpl;
|
|
13
|
+
if (!isCollection && typeof queryFnOrCollection !== `function`) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`useLiveInfiniteQuery: First argument must be either a pre-created live query collection (CollectionImpl) or a query function. Received: ${typeof queryFnOrCollection}`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
const [loadedPageCount, setLoadedPageCount] = react.useState(1);
|
|
19
|
+
const [isFetchingNextPage, setIsFetchingNextPage] = react.useState(false);
|
|
20
|
+
const collectionRef = react.useRef(isCollection ? queryFnOrCollection : null);
|
|
21
|
+
const hasValidatedCollectionRef = react.useRef(false);
|
|
22
|
+
const depsKey = JSON.stringify(deps);
|
|
23
|
+
const prevDepsKeyRef = react.useRef(depsKey);
|
|
24
|
+
react.useEffect(() => {
|
|
25
|
+
let shouldReset = false;
|
|
26
|
+
if (isCollection) {
|
|
27
|
+
if (collectionRef.current !== queryFnOrCollection) {
|
|
28
|
+
collectionRef.current = queryFnOrCollection;
|
|
29
|
+
hasValidatedCollectionRef.current = false;
|
|
30
|
+
shouldReset = true;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
if (prevDepsKeyRef.current !== depsKey) {
|
|
34
|
+
prevDepsKeyRef.current = depsKey;
|
|
35
|
+
shouldReset = true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (shouldReset) {
|
|
39
|
+
setLoadedPageCount(1);
|
|
40
|
+
}
|
|
41
|
+
}, [isCollection, queryFnOrCollection, depsKey]);
|
|
42
|
+
const queryResult = isCollection ? useLiveQuery.useLiveQuery(queryFnOrCollection) : useLiveQuery.useLiveQuery(
|
|
43
|
+
(q) => queryFnOrCollection(q).limit(pageSize).offset(0),
|
|
44
|
+
deps
|
|
45
|
+
);
|
|
46
|
+
react.useEffect(() => {
|
|
47
|
+
const utils = queryResult.collection.utils;
|
|
48
|
+
const expectedOffset = 0;
|
|
49
|
+
const expectedLimit = loadedPageCount * pageSize + 1;
|
|
50
|
+
if (!isLiveQueryCollectionUtils(utils)) {
|
|
51
|
+
if (isCollection) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. Please add .orderBy() to your createLiveQueryCollection query.`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (isCollection && !hasValidatedCollectionRef.current) {
|
|
59
|
+
const currentWindow = utils.getWindow();
|
|
60
|
+
if (currentWindow && (currentWindow.offset !== expectedOffset || currentWindow.limit !== expectedLimit)) {
|
|
61
|
+
console.warn(
|
|
62
|
+
`useLiveInfiniteQuery: Pre-created collection has window {offset: ${currentWindow.offset}, limit: ${currentWindow.limit}} but hook expects {offset: ${expectedOffset}, limit: ${expectedLimit}}. Adjusting window now.`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
hasValidatedCollectionRef.current = true;
|
|
66
|
+
}
|
|
67
|
+
if (!isCollection && !queryResult.isReady) return;
|
|
68
|
+
const result = utils.setWindow({
|
|
69
|
+
offset: expectedOffset,
|
|
70
|
+
limit: expectedLimit
|
|
71
|
+
});
|
|
72
|
+
if (result !== true) {
|
|
73
|
+
setIsFetchingNextPage(true);
|
|
74
|
+
result.then(() => {
|
|
75
|
+
setIsFetchingNextPage(false);
|
|
76
|
+
});
|
|
77
|
+
} else {
|
|
78
|
+
setIsFetchingNextPage(false);
|
|
79
|
+
}
|
|
80
|
+
}, [
|
|
81
|
+
isCollection,
|
|
82
|
+
queryResult.collection,
|
|
83
|
+
queryResult.isReady,
|
|
84
|
+
loadedPageCount,
|
|
85
|
+
pageSize
|
|
86
|
+
]);
|
|
87
|
+
const { pages, pageParams, hasNextPage, flatData } = react.useMemo(() => {
|
|
88
|
+
const dataArray = Array.isArray(queryResult.data) ? queryResult.data : [];
|
|
89
|
+
const totalItemsRequested = loadedPageCount * pageSize;
|
|
90
|
+
const hasMore = dataArray.length > totalItemsRequested;
|
|
91
|
+
const pagesResult = [];
|
|
92
|
+
const pageParamsResult = [];
|
|
93
|
+
for (let i = 0; i < loadedPageCount; i++) {
|
|
94
|
+
const pageData = dataArray.slice(i * pageSize, (i + 1) * pageSize);
|
|
95
|
+
pagesResult.push(pageData);
|
|
96
|
+
pageParamsResult.push(initialPageParam + i);
|
|
97
|
+
}
|
|
98
|
+
const flatDataResult = dataArray.slice(
|
|
99
|
+
0,
|
|
100
|
+
totalItemsRequested
|
|
101
|
+
);
|
|
102
|
+
return {
|
|
103
|
+
pages: pagesResult,
|
|
104
|
+
pageParams: pageParamsResult,
|
|
105
|
+
hasNextPage: hasMore,
|
|
106
|
+
flatData: flatDataResult
|
|
107
|
+
};
|
|
108
|
+
}, [queryResult.data, loadedPageCount, pageSize, initialPageParam]);
|
|
109
|
+
const fetchNextPage = react.useCallback(() => {
|
|
110
|
+
if (!hasNextPage || isFetchingNextPage) return;
|
|
111
|
+
setLoadedPageCount((prev) => prev + 1);
|
|
112
|
+
}, [hasNextPage, isFetchingNextPage]);
|
|
113
|
+
return {
|
|
114
|
+
...queryResult,
|
|
115
|
+
data: flatData,
|
|
116
|
+
pages,
|
|
117
|
+
pageParams,
|
|
118
|
+
fetchNextPage,
|
|
119
|
+
hasNextPage,
|
|
120
|
+
isFetchingNextPage
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
exports.useLiveInfiniteQuery = useLiveInfiniteQuery;
|
|
124
|
+
//# sourceMappingURL=useLiveInfiniteQuery.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLiveInfiniteQuery.cjs","sources":["../../src/useLiveInfiniteQuery.ts"],"sourcesContent":["import { useCallback, useEffect, useMemo, useRef, useState } from \"react\"\nimport { CollectionImpl } from \"@tanstack/db\"\nimport { useLiveQuery } from \"./useLiveQuery\"\nimport type {\n Collection,\n Context,\n InferResultType,\n InitialQueryBuilder,\n LiveQueryCollectionUtils,\n NonSingleResult,\n QueryBuilder,\n} from \"@tanstack/db\"\n\n/**\n * Type guard to check if utils object has setWindow method (LiveQueryCollectionUtils)\n */\nfunction isLiveQueryCollectionUtils(\n utils: unknown\n): utils is LiveQueryCollectionUtils {\n return typeof (utils as any).setWindow === `function`\n}\n\nexport type UseLiveInfiniteQueryConfig<TContext extends Context> = {\n pageSize?: number\n initialPageParam?: number\n getNextPageParam: (\n lastPage: Array<InferResultType<TContext>[number]>,\n allPages: Array<Array<InferResultType<TContext>[number]>>,\n lastPageParam: number,\n allPageParams: Array<number>\n ) => number | undefined\n}\n\nexport type UseLiveInfiniteQueryReturn<TContext extends Context> = Omit<\n ReturnType<typeof useLiveQuery<TContext>>,\n `data`\n> & {\n data: InferResultType<TContext>\n pages: Array<Array<InferResultType<TContext>[number]>>\n pageParams: Array<number>\n fetchNextPage: () => void\n hasNextPage: boolean\n isFetchingNextPage: boolean\n}\n\n/**\n * Create an infinite query using a query function with live updates\n *\n * Uses `utils.setWindow()` to dynamically adjust the limit/offset window\n * without recreating the live query collection on each page change.\n *\n * @param queryFn - Query function that defines what data to fetch. Must include `.orderBy()` for setWindow to work.\n * @param config - Configuration including pageSize and getNextPageParam\n * @param deps - Array of dependencies that trigger query re-execution when changed\n * @returns Object with pages, data, and pagination controls\n *\n * @example\n * // Basic infinite query\n * const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(\n * (q) => q\n * .from({ posts: postsCollection })\n * .orderBy(({ posts }) => posts.createdAt, 'desc')\n * .select(({ posts }) => ({\n * id: posts.id,\n * title: posts.title\n * })),\n * {\n * pageSize: 20,\n * getNextPageParam: (lastPage, allPages) =>\n * lastPage.length === 20 ? allPages.length : undefined\n * }\n * )\n *\n * @example\n * // With dependencies\n * const { pages, fetchNextPage } = useLiveInfiniteQuery(\n * (q) => q\n * .from({ posts: postsCollection })\n * .where(({ posts }) => eq(posts.category, category))\n * .orderBy(({ posts }) => posts.createdAt, 'desc'),\n * {\n * pageSize: 10,\n * getNextPageParam: (lastPage) =>\n * lastPage.length === 10 ? lastPage.length : undefined\n * },\n * [category]\n * )\n *\n * @example\n * // Router loader pattern with pre-created collection\n * // In loader:\n * const postsQuery = createLiveQueryCollection({\n * query: (q) => q\n * .from({ posts: postsCollection })\n * .orderBy(({ posts }) => posts.createdAt, 'desc')\n * .limit(20)\n * })\n * await postsQuery.preload()\n * return { postsQuery }\n *\n * // In component:\n * const { postsQuery } = useLoaderData()\n * const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(\n * postsQuery,\n * {\n * pageSize: 20,\n * getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined\n * }\n * )\n */\n\n// Overload for pre-created collection (non-single result)\nexport function useLiveInfiniteQuery<\n TResult extends object,\n TKey extends string | number,\n TUtils extends Record<string, any>,\n>(\n liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult,\n config: UseLiveInfiniteQueryConfig<any>\n): UseLiveInfiniteQueryReturn<any>\n\n// Overload for query function\nexport function useLiveInfiniteQuery<TContext extends Context>(\n queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,\n config: UseLiveInfiniteQueryConfig<TContext>,\n deps?: Array<unknown>\n): UseLiveInfiniteQueryReturn<TContext>\n\n// Implementation\nexport function useLiveInfiniteQuery<TContext extends Context>(\n queryFnOrCollection: any,\n config: UseLiveInfiniteQueryConfig<TContext>,\n deps: Array<unknown> = []\n): UseLiveInfiniteQueryReturn<TContext> {\n const pageSize = config.pageSize || 20\n const initialPageParam = config.initialPageParam ?? 0\n\n // Detect if input is a collection or query function\n const isCollection = queryFnOrCollection instanceof CollectionImpl\n\n // Validate input type\n if (!isCollection && typeof queryFnOrCollection !== `function`) {\n throw new Error(\n `useLiveInfiniteQuery: First argument must be either a pre-created live query collection (CollectionImpl) ` +\n `or a query function. Received: ${typeof queryFnOrCollection}`\n )\n }\n\n // Track how many pages have been loaded\n const [loadedPageCount, setLoadedPageCount] = useState(1)\n const [isFetchingNextPage, setIsFetchingNextPage] = useState(false)\n\n // Track collection instance and whether we've validated it (only for pre-created collections)\n const collectionRef = useRef(isCollection ? queryFnOrCollection : null)\n const hasValidatedCollectionRef = useRef(false)\n\n // Track deps for query functions (stringify for comparison)\n const depsKey = JSON.stringify(deps)\n const prevDepsKeyRef = useRef(depsKey)\n\n // Reset pagination when inputs change\n useEffect(() => {\n let shouldReset = false\n\n if (isCollection) {\n // Reset if collection instance changed\n if (collectionRef.current !== queryFnOrCollection) {\n collectionRef.current = queryFnOrCollection\n hasValidatedCollectionRef.current = false\n shouldReset = true\n }\n } else {\n // Reset if deps changed (for query functions)\n if (prevDepsKeyRef.current !== depsKey) {\n prevDepsKeyRef.current = depsKey\n shouldReset = true\n }\n }\n\n if (shouldReset) {\n setLoadedPageCount(1)\n }\n }, [isCollection, queryFnOrCollection, depsKey])\n\n // Create a live query with initial limit and offset\n // Either pass collection directly or wrap query function\n const queryResult = isCollection\n ? useLiveQuery(queryFnOrCollection)\n : useLiveQuery(\n (q) => queryFnOrCollection(q).limit(pageSize).offset(0),\n deps\n )\n\n // Adjust window when pagination changes\n useEffect(() => {\n const utils = queryResult.collection.utils\n const expectedOffset = 0\n const expectedLimit = loadedPageCount * pageSize + 1 // +1 for peek ahead\n\n // Check if collection has orderBy (required for setWindow)\n if (!isLiveQueryCollectionUtils(utils)) {\n // For pre-created collections, throw an error if no orderBy\n if (isCollection) {\n throw new Error(\n `useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. ` +\n `Please add .orderBy() to your createLiveQueryCollection query.`\n )\n }\n return\n }\n\n // For pre-created collections, validate window on first check\n if (isCollection && !hasValidatedCollectionRef.current) {\n const currentWindow = utils.getWindow()\n if (\n currentWindow &&\n (currentWindow.offset !== expectedOffset ||\n currentWindow.limit !== expectedLimit)\n ) {\n console.warn(\n `useLiveInfiniteQuery: Pre-created collection has window {offset: ${currentWindow.offset}, limit: ${currentWindow.limit}} ` +\n `but hook expects {offset: ${expectedOffset}, limit: ${expectedLimit}}. Adjusting window now.`\n )\n }\n hasValidatedCollectionRef.current = true\n }\n\n // For query functions, wait until collection is ready\n if (!isCollection && !queryResult.isReady) return\n\n // Adjust the window\n const result = utils.setWindow({\n offset: expectedOffset,\n limit: expectedLimit,\n })\n\n if (result !== true) {\n setIsFetchingNextPage(true)\n result.then(() => {\n setIsFetchingNextPage(false)\n })\n } else {\n setIsFetchingNextPage(false)\n }\n }, [\n isCollection,\n queryResult.collection,\n queryResult.isReady,\n loadedPageCount,\n pageSize,\n ])\n\n // Split the data array into pages and determine if there's a next page\n const { pages, pageParams, hasNextPage, flatData } = useMemo(() => {\n const dataArray = (\n Array.isArray(queryResult.data) ? queryResult.data : []\n ) as InferResultType<TContext>\n const totalItemsRequested = loadedPageCount * pageSize\n\n // Check if we have more data than requested (the peek ahead item)\n const hasMore = dataArray.length > totalItemsRequested\n\n // Build pages array (without the peek ahead item)\n const pagesResult: Array<Array<InferResultType<TContext>[number]>> = []\n const pageParamsResult: Array<number> = []\n\n for (let i = 0; i < loadedPageCount; i++) {\n const pageData = dataArray.slice(i * pageSize, (i + 1) * pageSize)\n pagesResult.push(pageData)\n pageParamsResult.push(initialPageParam + i)\n }\n\n // Flatten the pages for the data return (without peek ahead item)\n const flatDataResult = dataArray.slice(\n 0,\n totalItemsRequested\n ) as InferResultType<TContext>\n\n return {\n pages: pagesResult,\n pageParams: pageParamsResult,\n hasNextPage: hasMore,\n flatData: flatDataResult,\n }\n }, [queryResult.data, loadedPageCount, pageSize, initialPageParam])\n\n // Fetch next page\n const fetchNextPage = useCallback(() => {\n if (!hasNextPage || isFetchingNextPage) return\n\n setLoadedPageCount((prev) => prev + 1)\n }, [hasNextPage, isFetchingNextPage])\n\n return {\n ...queryResult,\n data: flatData,\n pages,\n pageParams,\n fetchNextPage,\n hasNextPage,\n isFetchingNextPage,\n } as UseLiveInfiniteQueryReturn<TContext>\n}\n"],"names":["CollectionImpl","useState","useRef","useEffect","useLiveQuery","useMemo","useCallback"],"mappings":"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"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useLiveQuery } from './useLiveQuery.cjs';
|
|
2
|
+
import { Collection, Context, InferResultType, InitialQueryBuilder, NonSingleResult, QueryBuilder } from '@tanstack/db';
|
|
3
|
+
export type UseLiveInfiniteQueryConfig<TContext extends Context> = {
|
|
4
|
+
pageSize?: number;
|
|
5
|
+
initialPageParam?: number;
|
|
6
|
+
getNextPageParam: (lastPage: Array<InferResultType<TContext>[number]>, allPages: Array<Array<InferResultType<TContext>[number]>>, lastPageParam: number, allPageParams: Array<number>) => number | undefined;
|
|
7
|
+
};
|
|
8
|
+
export type UseLiveInfiniteQueryReturn<TContext extends Context> = Omit<ReturnType<typeof useLiveQuery<TContext>>, `data`> & {
|
|
9
|
+
data: InferResultType<TContext>;
|
|
10
|
+
pages: Array<Array<InferResultType<TContext>[number]>>;
|
|
11
|
+
pageParams: Array<number>;
|
|
12
|
+
fetchNextPage: () => void;
|
|
13
|
+
hasNextPage: boolean;
|
|
14
|
+
isFetchingNextPage: boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Create an infinite query using a query function with live updates
|
|
18
|
+
*
|
|
19
|
+
* Uses `utils.setWindow()` to dynamically adjust the limit/offset window
|
|
20
|
+
* without recreating the live query collection on each page change.
|
|
21
|
+
*
|
|
22
|
+
* @param queryFn - Query function that defines what data to fetch. Must include `.orderBy()` for setWindow to work.
|
|
23
|
+
* @param config - Configuration including pageSize and getNextPageParam
|
|
24
|
+
* @param deps - Array of dependencies that trigger query re-execution when changed
|
|
25
|
+
* @returns Object with pages, data, and pagination controls
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Basic infinite query
|
|
29
|
+
* const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
|
|
30
|
+
* (q) => q
|
|
31
|
+
* .from({ posts: postsCollection })
|
|
32
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc')
|
|
33
|
+
* .select(({ posts }) => ({
|
|
34
|
+
* id: posts.id,
|
|
35
|
+
* title: posts.title
|
|
36
|
+
* })),
|
|
37
|
+
* {
|
|
38
|
+
* pageSize: 20,
|
|
39
|
+
* getNextPageParam: (lastPage, allPages) =>
|
|
40
|
+
* lastPage.length === 20 ? allPages.length : undefined
|
|
41
|
+
* }
|
|
42
|
+
* )
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // With dependencies
|
|
46
|
+
* const { pages, fetchNextPage } = useLiveInfiniteQuery(
|
|
47
|
+
* (q) => q
|
|
48
|
+
* .from({ posts: postsCollection })
|
|
49
|
+
* .where(({ posts }) => eq(posts.category, category))
|
|
50
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc'),
|
|
51
|
+
* {
|
|
52
|
+
* pageSize: 10,
|
|
53
|
+
* getNextPageParam: (lastPage) =>
|
|
54
|
+
* lastPage.length === 10 ? lastPage.length : undefined
|
|
55
|
+
* },
|
|
56
|
+
* [category]
|
|
57
|
+
* )
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* // Router loader pattern with pre-created collection
|
|
61
|
+
* // In loader:
|
|
62
|
+
* const postsQuery = createLiveQueryCollection({
|
|
63
|
+
* query: (q) => q
|
|
64
|
+
* .from({ posts: postsCollection })
|
|
65
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc')
|
|
66
|
+
* .limit(20)
|
|
67
|
+
* })
|
|
68
|
+
* await postsQuery.preload()
|
|
69
|
+
* return { postsQuery }
|
|
70
|
+
*
|
|
71
|
+
* // In component:
|
|
72
|
+
* const { postsQuery } = useLoaderData()
|
|
73
|
+
* const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
|
|
74
|
+
* postsQuery,
|
|
75
|
+
* {
|
|
76
|
+
* pageSize: 20,
|
|
77
|
+
* getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined
|
|
78
|
+
* }
|
|
79
|
+
* )
|
|
80
|
+
*/
|
|
81
|
+
export declare function useLiveInfiniteQuery<TResult extends object, TKey extends string | number, TUtils extends Record<string, any>>(liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult, config: UseLiveInfiniteQueryConfig<any>): UseLiveInfiniteQueryReturn<any>;
|
|
82
|
+
export declare function useLiveInfiniteQuery<TContext extends Context>(queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>, config: UseLiveInfiniteQueryConfig<TContext>, deps?: Array<unknown>): UseLiveInfiniteQueryReturn<TContext>;
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { useLiveQuery } from "./useLiveQuery.js";
|
|
2
|
+
import { useLiveInfiniteQuery } from "./useLiveInfiniteQuery.js";
|
|
2
3
|
export * from "@tanstack/db";
|
|
3
4
|
import { createTransaction } from "@tanstack/db";
|
|
4
5
|
export {
|
|
5
6
|
createTransaction,
|
|
7
|
+
useLiveInfiniteQuery,
|
|
6
8
|
useLiveQuery
|
|
7
9
|
};
|
|
8
10
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useLiveQuery } from './useLiveQuery.js';
|
|
2
|
+
import { Collection, Context, InferResultType, InitialQueryBuilder, NonSingleResult, QueryBuilder } from '@tanstack/db';
|
|
3
|
+
export type UseLiveInfiniteQueryConfig<TContext extends Context> = {
|
|
4
|
+
pageSize?: number;
|
|
5
|
+
initialPageParam?: number;
|
|
6
|
+
getNextPageParam: (lastPage: Array<InferResultType<TContext>[number]>, allPages: Array<Array<InferResultType<TContext>[number]>>, lastPageParam: number, allPageParams: Array<number>) => number | undefined;
|
|
7
|
+
};
|
|
8
|
+
export type UseLiveInfiniteQueryReturn<TContext extends Context> = Omit<ReturnType<typeof useLiveQuery<TContext>>, `data`> & {
|
|
9
|
+
data: InferResultType<TContext>;
|
|
10
|
+
pages: Array<Array<InferResultType<TContext>[number]>>;
|
|
11
|
+
pageParams: Array<number>;
|
|
12
|
+
fetchNextPage: () => void;
|
|
13
|
+
hasNextPage: boolean;
|
|
14
|
+
isFetchingNextPage: boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Create an infinite query using a query function with live updates
|
|
18
|
+
*
|
|
19
|
+
* Uses `utils.setWindow()` to dynamically adjust the limit/offset window
|
|
20
|
+
* without recreating the live query collection on each page change.
|
|
21
|
+
*
|
|
22
|
+
* @param queryFn - Query function that defines what data to fetch. Must include `.orderBy()` for setWindow to work.
|
|
23
|
+
* @param config - Configuration including pageSize and getNextPageParam
|
|
24
|
+
* @param deps - Array of dependencies that trigger query re-execution when changed
|
|
25
|
+
* @returns Object with pages, data, and pagination controls
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Basic infinite query
|
|
29
|
+
* const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
|
|
30
|
+
* (q) => q
|
|
31
|
+
* .from({ posts: postsCollection })
|
|
32
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc')
|
|
33
|
+
* .select(({ posts }) => ({
|
|
34
|
+
* id: posts.id,
|
|
35
|
+
* title: posts.title
|
|
36
|
+
* })),
|
|
37
|
+
* {
|
|
38
|
+
* pageSize: 20,
|
|
39
|
+
* getNextPageParam: (lastPage, allPages) =>
|
|
40
|
+
* lastPage.length === 20 ? allPages.length : undefined
|
|
41
|
+
* }
|
|
42
|
+
* )
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // With dependencies
|
|
46
|
+
* const { pages, fetchNextPage } = useLiveInfiniteQuery(
|
|
47
|
+
* (q) => q
|
|
48
|
+
* .from({ posts: postsCollection })
|
|
49
|
+
* .where(({ posts }) => eq(posts.category, category))
|
|
50
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc'),
|
|
51
|
+
* {
|
|
52
|
+
* pageSize: 10,
|
|
53
|
+
* getNextPageParam: (lastPage) =>
|
|
54
|
+
* lastPage.length === 10 ? lastPage.length : undefined
|
|
55
|
+
* },
|
|
56
|
+
* [category]
|
|
57
|
+
* )
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* // Router loader pattern with pre-created collection
|
|
61
|
+
* // In loader:
|
|
62
|
+
* const postsQuery = createLiveQueryCollection({
|
|
63
|
+
* query: (q) => q
|
|
64
|
+
* .from({ posts: postsCollection })
|
|
65
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc')
|
|
66
|
+
* .limit(20)
|
|
67
|
+
* })
|
|
68
|
+
* await postsQuery.preload()
|
|
69
|
+
* return { postsQuery }
|
|
70
|
+
*
|
|
71
|
+
* // In component:
|
|
72
|
+
* const { postsQuery } = useLoaderData()
|
|
73
|
+
* const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
|
|
74
|
+
* postsQuery,
|
|
75
|
+
* {
|
|
76
|
+
* pageSize: 20,
|
|
77
|
+
* getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined
|
|
78
|
+
* }
|
|
79
|
+
* )
|
|
80
|
+
*/
|
|
81
|
+
export declare function useLiveInfiniteQuery<TResult extends object, TKey extends string | number, TUtils extends Record<string, any>>(liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult, config: UseLiveInfiniteQueryConfig<any>): UseLiveInfiniteQueryReturn<any>;
|
|
82
|
+
export declare function useLiveInfiniteQuery<TContext extends Context>(queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>, config: UseLiveInfiniteQueryConfig<TContext>, deps?: Array<unknown>): UseLiveInfiniteQueryReturn<TContext>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { useState, useRef, useEffect, useMemo, useCallback } from "react";
|
|
2
|
+
import { CollectionImpl } from "@tanstack/db";
|
|
3
|
+
import { useLiveQuery } from "./useLiveQuery.js";
|
|
4
|
+
function isLiveQueryCollectionUtils(utils) {
|
|
5
|
+
return typeof utils.setWindow === `function`;
|
|
6
|
+
}
|
|
7
|
+
function useLiveInfiniteQuery(queryFnOrCollection, config, deps = []) {
|
|
8
|
+
const pageSize = config.pageSize || 20;
|
|
9
|
+
const initialPageParam = config.initialPageParam ?? 0;
|
|
10
|
+
const isCollection = queryFnOrCollection instanceof CollectionImpl;
|
|
11
|
+
if (!isCollection && typeof queryFnOrCollection !== `function`) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`useLiveInfiniteQuery: First argument must be either a pre-created live query collection (CollectionImpl) or a query function. Received: ${typeof queryFnOrCollection}`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
const [loadedPageCount, setLoadedPageCount] = useState(1);
|
|
17
|
+
const [isFetchingNextPage, setIsFetchingNextPage] = useState(false);
|
|
18
|
+
const collectionRef = useRef(isCollection ? queryFnOrCollection : null);
|
|
19
|
+
const hasValidatedCollectionRef = useRef(false);
|
|
20
|
+
const depsKey = JSON.stringify(deps);
|
|
21
|
+
const prevDepsKeyRef = useRef(depsKey);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
let shouldReset = false;
|
|
24
|
+
if (isCollection) {
|
|
25
|
+
if (collectionRef.current !== queryFnOrCollection) {
|
|
26
|
+
collectionRef.current = queryFnOrCollection;
|
|
27
|
+
hasValidatedCollectionRef.current = false;
|
|
28
|
+
shouldReset = true;
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
if (prevDepsKeyRef.current !== depsKey) {
|
|
32
|
+
prevDepsKeyRef.current = depsKey;
|
|
33
|
+
shouldReset = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (shouldReset) {
|
|
37
|
+
setLoadedPageCount(1);
|
|
38
|
+
}
|
|
39
|
+
}, [isCollection, queryFnOrCollection, depsKey]);
|
|
40
|
+
const queryResult = isCollection ? useLiveQuery(queryFnOrCollection) : useLiveQuery(
|
|
41
|
+
(q) => queryFnOrCollection(q).limit(pageSize).offset(0),
|
|
42
|
+
deps
|
|
43
|
+
);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const utils = queryResult.collection.utils;
|
|
46
|
+
const expectedOffset = 0;
|
|
47
|
+
const expectedLimit = loadedPageCount * pageSize + 1;
|
|
48
|
+
if (!isLiveQueryCollectionUtils(utils)) {
|
|
49
|
+
if (isCollection) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. Please add .orderBy() to your createLiveQueryCollection query.`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (isCollection && !hasValidatedCollectionRef.current) {
|
|
57
|
+
const currentWindow = utils.getWindow();
|
|
58
|
+
if (currentWindow && (currentWindow.offset !== expectedOffset || currentWindow.limit !== expectedLimit)) {
|
|
59
|
+
console.warn(
|
|
60
|
+
`useLiveInfiniteQuery: Pre-created collection has window {offset: ${currentWindow.offset}, limit: ${currentWindow.limit}} but hook expects {offset: ${expectedOffset}, limit: ${expectedLimit}}. Adjusting window now.`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
hasValidatedCollectionRef.current = true;
|
|
64
|
+
}
|
|
65
|
+
if (!isCollection && !queryResult.isReady) return;
|
|
66
|
+
const result = utils.setWindow({
|
|
67
|
+
offset: expectedOffset,
|
|
68
|
+
limit: expectedLimit
|
|
69
|
+
});
|
|
70
|
+
if (result !== true) {
|
|
71
|
+
setIsFetchingNextPage(true);
|
|
72
|
+
result.then(() => {
|
|
73
|
+
setIsFetchingNextPage(false);
|
|
74
|
+
});
|
|
75
|
+
} else {
|
|
76
|
+
setIsFetchingNextPage(false);
|
|
77
|
+
}
|
|
78
|
+
}, [
|
|
79
|
+
isCollection,
|
|
80
|
+
queryResult.collection,
|
|
81
|
+
queryResult.isReady,
|
|
82
|
+
loadedPageCount,
|
|
83
|
+
pageSize
|
|
84
|
+
]);
|
|
85
|
+
const { pages, pageParams, hasNextPage, flatData } = useMemo(() => {
|
|
86
|
+
const dataArray = Array.isArray(queryResult.data) ? queryResult.data : [];
|
|
87
|
+
const totalItemsRequested = loadedPageCount * pageSize;
|
|
88
|
+
const hasMore = dataArray.length > totalItemsRequested;
|
|
89
|
+
const pagesResult = [];
|
|
90
|
+
const pageParamsResult = [];
|
|
91
|
+
for (let i = 0; i < loadedPageCount; i++) {
|
|
92
|
+
const pageData = dataArray.slice(i * pageSize, (i + 1) * pageSize);
|
|
93
|
+
pagesResult.push(pageData);
|
|
94
|
+
pageParamsResult.push(initialPageParam + i);
|
|
95
|
+
}
|
|
96
|
+
const flatDataResult = dataArray.slice(
|
|
97
|
+
0,
|
|
98
|
+
totalItemsRequested
|
|
99
|
+
);
|
|
100
|
+
return {
|
|
101
|
+
pages: pagesResult,
|
|
102
|
+
pageParams: pageParamsResult,
|
|
103
|
+
hasNextPage: hasMore,
|
|
104
|
+
flatData: flatDataResult
|
|
105
|
+
};
|
|
106
|
+
}, [queryResult.data, loadedPageCount, pageSize, initialPageParam]);
|
|
107
|
+
const fetchNextPage = useCallback(() => {
|
|
108
|
+
if (!hasNextPage || isFetchingNextPage) return;
|
|
109
|
+
setLoadedPageCount((prev) => prev + 1);
|
|
110
|
+
}, [hasNextPage, isFetchingNextPage]);
|
|
111
|
+
return {
|
|
112
|
+
...queryResult,
|
|
113
|
+
data: flatData,
|
|
114
|
+
pages,
|
|
115
|
+
pageParams,
|
|
116
|
+
fetchNextPage,
|
|
117
|
+
hasNextPage,
|
|
118
|
+
isFetchingNextPage
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export {
|
|
122
|
+
useLiveInfiniteQuery
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=useLiveInfiniteQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLiveInfiniteQuery.js","sources":["../../src/useLiveInfiniteQuery.ts"],"sourcesContent":["import { useCallback, useEffect, useMemo, useRef, useState } from \"react\"\nimport { CollectionImpl } from \"@tanstack/db\"\nimport { useLiveQuery } from \"./useLiveQuery\"\nimport type {\n Collection,\n Context,\n InferResultType,\n InitialQueryBuilder,\n LiveQueryCollectionUtils,\n NonSingleResult,\n QueryBuilder,\n} from \"@tanstack/db\"\n\n/**\n * Type guard to check if utils object has setWindow method (LiveQueryCollectionUtils)\n */\nfunction isLiveQueryCollectionUtils(\n utils: unknown\n): utils is LiveQueryCollectionUtils {\n return typeof (utils as any).setWindow === `function`\n}\n\nexport type UseLiveInfiniteQueryConfig<TContext extends Context> = {\n pageSize?: number\n initialPageParam?: number\n getNextPageParam: (\n lastPage: Array<InferResultType<TContext>[number]>,\n allPages: Array<Array<InferResultType<TContext>[number]>>,\n lastPageParam: number,\n allPageParams: Array<number>\n ) => number | undefined\n}\n\nexport type UseLiveInfiniteQueryReturn<TContext extends Context> = Omit<\n ReturnType<typeof useLiveQuery<TContext>>,\n `data`\n> & {\n data: InferResultType<TContext>\n pages: Array<Array<InferResultType<TContext>[number]>>\n pageParams: Array<number>\n fetchNextPage: () => void\n hasNextPage: boolean\n isFetchingNextPage: boolean\n}\n\n/**\n * Create an infinite query using a query function with live updates\n *\n * Uses `utils.setWindow()` to dynamically adjust the limit/offset window\n * without recreating the live query collection on each page change.\n *\n * @param queryFn - Query function that defines what data to fetch. Must include `.orderBy()` for setWindow to work.\n * @param config - Configuration including pageSize and getNextPageParam\n * @param deps - Array of dependencies that trigger query re-execution when changed\n * @returns Object with pages, data, and pagination controls\n *\n * @example\n * // Basic infinite query\n * const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(\n * (q) => q\n * .from({ posts: postsCollection })\n * .orderBy(({ posts }) => posts.createdAt, 'desc')\n * .select(({ posts }) => ({\n * id: posts.id,\n * title: posts.title\n * })),\n * {\n * pageSize: 20,\n * getNextPageParam: (lastPage, allPages) =>\n * lastPage.length === 20 ? allPages.length : undefined\n * }\n * )\n *\n * @example\n * // With dependencies\n * const { pages, fetchNextPage } = useLiveInfiniteQuery(\n * (q) => q\n * .from({ posts: postsCollection })\n * .where(({ posts }) => eq(posts.category, category))\n * .orderBy(({ posts }) => posts.createdAt, 'desc'),\n * {\n * pageSize: 10,\n * getNextPageParam: (lastPage) =>\n * lastPage.length === 10 ? lastPage.length : undefined\n * },\n * [category]\n * )\n *\n * @example\n * // Router loader pattern with pre-created collection\n * // In loader:\n * const postsQuery = createLiveQueryCollection({\n * query: (q) => q\n * .from({ posts: postsCollection })\n * .orderBy(({ posts }) => posts.createdAt, 'desc')\n * .limit(20)\n * })\n * await postsQuery.preload()\n * return { postsQuery }\n *\n * // In component:\n * const { postsQuery } = useLoaderData()\n * const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(\n * postsQuery,\n * {\n * pageSize: 20,\n * getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined\n * }\n * )\n */\n\n// Overload for pre-created collection (non-single result)\nexport function useLiveInfiniteQuery<\n TResult extends object,\n TKey extends string | number,\n TUtils extends Record<string, any>,\n>(\n liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult,\n config: UseLiveInfiniteQueryConfig<any>\n): UseLiveInfiniteQueryReturn<any>\n\n// Overload for query function\nexport function useLiveInfiniteQuery<TContext extends Context>(\n queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,\n config: UseLiveInfiniteQueryConfig<TContext>,\n deps?: Array<unknown>\n): UseLiveInfiniteQueryReturn<TContext>\n\n// Implementation\nexport function useLiveInfiniteQuery<TContext extends Context>(\n queryFnOrCollection: any,\n config: UseLiveInfiniteQueryConfig<TContext>,\n deps: Array<unknown> = []\n): UseLiveInfiniteQueryReturn<TContext> {\n const pageSize = config.pageSize || 20\n const initialPageParam = config.initialPageParam ?? 0\n\n // Detect if input is a collection or query function\n const isCollection = queryFnOrCollection instanceof CollectionImpl\n\n // Validate input type\n if (!isCollection && typeof queryFnOrCollection !== `function`) {\n throw new Error(\n `useLiveInfiniteQuery: First argument must be either a pre-created live query collection (CollectionImpl) ` +\n `or a query function. Received: ${typeof queryFnOrCollection}`\n )\n }\n\n // Track how many pages have been loaded\n const [loadedPageCount, setLoadedPageCount] = useState(1)\n const [isFetchingNextPage, setIsFetchingNextPage] = useState(false)\n\n // Track collection instance and whether we've validated it (only for pre-created collections)\n const collectionRef = useRef(isCollection ? queryFnOrCollection : null)\n const hasValidatedCollectionRef = useRef(false)\n\n // Track deps for query functions (stringify for comparison)\n const depsKey = JSON.stringify(deps)\n const prevDepsKeyRef = useRef(depsKey)\n\n // Reset pagination when inputs change\n useEffect(() => {\n let shouldReset = false\n\n if (isCollection) {\n // Reset if collection instance changed\n if (collectionRef.current !== queryFnOrCollection) {\n collectionRef.current = queryFnOrCollection\n hasValidatedCollectionRef.current = false\n shouldReset = true\n }\n } else {\n // Reset if deps changed (for query functions)\n if (prevDepsKeyRef.current !== depsKey) {\n prevDepsKeyRef.current = depsKey\n shouldReset = true\n }\n }\n\n if (shouldReset) {\n setLoadedPageCount(1)\n }\n }, [isCollection, queryFnOrCollection, depsKey])\n\n // Create a live query with initial limit and offset\n // Either pass collection directly or wrap query function\n const queryResult = isCollection\n ? useLiveQuery(queryFnOrCollection)\n : useLiveQuery(\n (q) => queryFnOrCollection(q).limit(pageSize).offset(0),\n deps\n )\n\n // Adjust window when pagination changes\n useEffect(() => {\n const utils = queryResult.collection.utils\n const expectedOffset = 0\n const expectedLimit = loadedPageCount * pageSize + 1 // +1 for peek ahead\n\n // Check if collection has orderBy (required for setWindow)\n if (!isLiveQueryCollectionUtils(utils)) {\n // For pre-created collections, throw an error if no orderBy\n if (isCollection) {\n throw new Error(\n `useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. ` +\n `Please add .orderBy() to your createLiveQueryCollection query.`\n )\n }\n return\n }\n\n // For pre-created collections, validate window on first check\n if (isCollection && !hasValidatedCollectionRef.current) {\n const currentWindow = utils.getWindow()\n if (\n currentWindow &&\n (currentWindow.offset !== expectedOffset ||\n currentWindow.limit !== expectedLimit)\n ) {\n console.warn(\n `useLiveInfiniteQuery: Pre-created collection has window {offset: ${currentWindow.offset}, limit: ${currentWindow.limit}} ` +\n `but hook expects {offset: ${expectedOffset}, limit: ${expectedLimit}}. Adjusting window now.`\n )\n }\n hasValidatedCollectionRef.current = true\n }\n\n // For query functions, wait until collection is ready\n if (!isCollection && !queryResult.isReady) return\n\n // Adjust the window\n const result = utils.setWindow({\n offset: expectedOffset,\n limit: expectedLimit,\n })\n\n if (result !== true) {\n setIsFetchingNextPage(true)\n result.then(() => {\n setIsFetchingNextPage(false)\n })\n } else {\n setIsFetchingNextPage(false)\n }\n }, [\n isCollection,\n queryResult.collection,\n queryResult.isReady,\n loadedPageCount,\n pageSize,\n ])\n\n // Split the data array into pages and determine if there's a next page\n const { pages, pageParams, hasNextPage, flatData } = useMemo(() => {\n const dataArray = (\n Array.isArray(queryResult.data) ? queryResult.data : []\n ) as InferResultType<TContext>\n const totalItemsRequested = loadedPageCount * pageSize\n\n // Check if we have more data than requested (the peek ahead item)\n const hasMore = dataArray.length > totalItemsRequested\n\n // Build pages array (without the peek ahead item)\n const pagesResult: Array<Array<InferResultType<TContext>[number]>> = []\n const pageParamsResult: Array<number> = []\n\n for (let i = 0; i < loadedPageCount; i++) {\n const pageData = dataArray.slice(i * pageSize, (i + 1) * pageSize)\n pagesResult.push(pageData)\n pageParamsResult.push(initialPageParam + i)\n }\n\n // Flatten the pages for the data return (without peek ahead item)\n const flatDataResult = dataArray.slice(\n 0,\n totalItemsRequested\n ) as InferResultType<TContext>\n\n return {\n pages: pagesResult,\n pageParams: pageParamsResult,\n hasNextPage: hasMore,\n flatData: flatDataResult,\n }\n }, [queryResult.data, loadedPageCount, pageSize, initialPageParam])\n\n // Fetch next page\n const fetchNextPage = useCallback(() => {\n if (!hasNextPage || isFetchingNextPage) return\n\n setLoadedPageCount((prev) => prev + 1)\n }, [hasNextPage, isFetchingNextPage])\n\n return {\n ...queryResult,\n data: flatData,\n pages,\n pageParams,\n fetchNextPage,\n hasNextPage,\n isFetchingNextPage,\n } as UseLiveInfiniteQueryReturn<TContext>\n}\n"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-db",
|
|
3
3
|
"description": "React integration for @tanstack/db",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.33",
|
|
5
5
|
"author": "Kyle Mathews",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"use-sync-external-store": "^1.6.0",
|
|
20
|
-
"@tanstack/db": "0.4.
|
|
20
|
+
"@tanstack/db": "0.4.11"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@electric-sql/client": "1.0.14",
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
|
|
2
|
+
import { CollectionImpl } from "@tanstack/db"
|
|
3
|
+
import { useLiveQuery } from "./useLiveQuery"
|
|
4
|
+
import type {
|
|
5
|
+
Collection,
|
|
6
|
+
Context,
|
|
7
|
+
InferResultType,
|
|
8
|
+
InitialQueryBuilder,
|
|
9
|
+
LiveQueryCollectionUtils,
|
|
10
|
+
NonSingleResult,
|
|
11
|
+
QueryBuilder,
|
|
12
|
+
} from "@tanstack/db"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type guard to check if utils object has setWindow method (LiveQueryCollectionUtils)
|
|
16
|
+
*/
|
|
17
|
+
function isLiveQueryCollectionUtils(
|
|
18
|
+
utils: unknown
|
|
19
|
+
): utils is LiveQueryCollectionUtils {
|
|
20
|
+
return typeof (utils as any).setWindow === `function`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type UseLiveInfiniteQueryConfig<TContext extends Context> = {
|
|
24
|
+
pageSize?: number
|
|
25
|
+
initialPageParam?: number
|
|
26
|
+
getNextPageParam: (
|
|
27
|
+
lastPage: Array<InferResultType<TContext>[number]>,
|
|
28
|
+
allPages: Array<Array<InferResultType<TContext>[number]>>,
|
|
29
|
+
lastPageParam: number,
|
|
30
|
+
allPageParams: Array<number>
|
|
31
|
+
) => number | undefined
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type UseLiveInfiniteQueryReturn<TContext extends Context> = Omit<
|
|
35
|
+
ReturnType<typeof useLiveQuery<TContext>>,
|
|
36
|
+
`data`
|
|
37
|
+
> & {
|
|
38
|
+
data: InferResultType<TContext>
|
|
39
|
+
pages: Array<Array<InferResultType<TContext>[number]>>
|
|
40
|
+
pageParams: Array<number>
|
|
41
|
+
fetchNextPage: () => void
|
|
42
|
+
hasNextPage: boolean
|
|
43
|
+
isFetchingNextPage: boolean
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create an infinite query using a query function with live updates
|
|
48
|
+
*
|
|
49
|
+
* Uses `utils.setWindow()` to dynamically adjust the limit/offset window
|
|
50
|
+
* without recreating the live query collection on each page change.
|
|
51
|
+
*
|
|
52
|
+
* @param queryFn - Query function that defines what data to fetch. Must include `.orderBy()` for setWindow to work.
|
|
53
|
+
* @param config - Configuration including pageSize and getNextPageParam
|
|
54
|
+
* @param deps - Array of dependencies that trigger query re-execution when changed
|
|
55
|
+
* @returns Object with pages, data, and pagination controls
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* // Basic infinite query
|
|
59
|
+
* const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
|
|
60
|
+
* (q) => q
|
|
61
|
+
* .from({ posts: postsCollection })
|
|
62
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc')
|
|
63
|
+
* .select(({ posts }) => ({
|
|
64
|
+
* id: posts.id,
|
|
65
|
+
* title: posts.title
|
|
66
|
+
* })),
|
|
67
|
+
* {
|
|
68
|
+
* pageSize: 20,
|
|
69
|
+
* getNextPageParam: (lastPage, allPages) =>
|
|
70
|
+
* lastPage.length === 20 ? allPages.length : undefined
|
|
71
|
+
* }
|
|
72
|
+
* )
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* // With dependencies
|
|
76
|
+
* const { pages, fetchNextPage } = useLiveInfiniteQuery(
|
|
77
|
+
* (q) => q
|
|
78
|
+
* .from({ posts: postsCollection })
|
|
79
|
+
* .where(({ posts }) => eq(posts.category, category))
|
|
80
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc'),
|
|
81
|
+
* {
|
|
82
|
+
* pageSize: 10,
|
|
83
|
+
* getNextPageParam: (lastPage) =>
|
|
84
|
+
* lastPage.length === 10 ? lastPage.length : undefined
|
|
85
|
+
* },
|
|
86
|
+
* [category]
|
|
87
|
+
* )
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* // Router loader pattern with pre-created collection
|
|
91
|
+
* // In loader:
|
|
92
|
+
* const postsQuery = createLiveQueryCollection({
|
|
93
|
+
* query: (q) => q
|
|
94
|
+
* .from({ posts: postsCollection })
|
|
95
|
+
* .orderBy(({ posts }) => posts.createdAt, 'desc')
|
|
96
|
+
* .limit(20)
|
|
97
|
+
* })
|
|
98
|
+
* await postsQuery.preload()
|
|
99
|
+
* return { postsQuery }
|
|
100
|
+
*
|
|
101
|
+
* // In component:
|
|
102
|
+
* const { postsQuery } = useLoaderData()
|
|
103
|
+
* const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
|
|
104
|
+
* postsQuery,
|
|
105
|
+
* {
|
|
106
|
+
* pageSize: 20,
|
|
107
|
+
* getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined
|
|
108
|
+
* }
|
|
109
|
+
* )
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
// Overload for pre-created collection (non-single result)
|
|
113
|
+
export function useLiveInfiniteQuery<
|
|
114
|
+
TResult extends object,
|
|
115
|
+
TKey extends string | number,
|
|
116
|
+
TUtils extends Record<string, any>,
|
|
117
|
+
>(
|
|
118
|
+
liveQueryCollection: Collection<TResult, TKey, TUtils> & NonSingleResult,
|
|
119
|
+
config: UseLiveInfiniteQueryConfig<any>
|
|
120
|
+
): UseLiveInfiniteQueryReturn<any>
|
|
121
|
+
|
|
122
|
+
// Overload for query function
|
|
123
|
+
export function useLiveInfiniteQuery<TContext extends Context>(
|
|
124
|
+
queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,
|
|
125
|
+
config: UseLiveInfiniteQueryConfig<TContext>,
|
|
126
|
+
deps?: Array<unknown>
|
|
127
|
+
): UseLiveInfiniteQueryReturn<TContext>
|
|
128
|
+
|
|
129
|
+
// Implementation
|
|
130
|
+
export function useLiveInfiniteQuery<TContext extends Context>(
|
|
131
|
+
queryFnOrCollection: any,
|
|
132
|
+
config: UseLiveInfiniteQueryConfig<TContext>,
|
|
133
|
+
deps: Array<unknown> = []
|
|
134
|
+
): UseLiveInfiniteQueryReturn<TContext> {
|
|
135
|
+
const pageSize = config.pageSize || 20
|
|
136
|
+
const initialPageParam = config.initialPageParam ?? 0
|
|
137
|
+
|
|
138
|
+
// Detect if input is a collection or query function
|
|
139
|
+
const isCollection = queryFnOrCollection instanceof CollectionImpl
|
|
140
|
+
|
|
141
|
+
// Validate input type
|
|
142
|
+
if (!isCollection && typeof queryFnOrCollection !== `function`) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`useLiveInfiniteQuery: First argument must be either a pre-created live query collection (CollectionImpl) ` +
|
|
145
|
+
`or a query function. Received: ${typeof queryFnOrCollection}`
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Track how many pages have been loaded
|
|
150
|
+
const [loadedPageCount, setLoadedPageCount] = useState(1)
|
|
151
|
+
const [isFetchingNextPage, setIsFetchingNextPage] = useState(false)
|
|
152
|
+
|
|
153
|
+
// Track collection instance and whether we've validated it (only for pre-created collections)
|
|
154
|
+
const collectionRef = useRef(isCollection ? queryFnOrCollection : null)
|
|
155
|
+
const hasValidatedCollectionRef = useRef(false)
|
|
156
|
+
|
|
157
|
+
// Track deps for query functions (stringify for comparison)
|
|
158
|
+
const depsKey = JSON.stringify(deps)
|
|
159
|
+
const prevDepsKeyRef = useRef(depsKey)
|
|
160
|
+
|
|
161
|
+
// Reset pagination when inputs change
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
let shouldReset = false
|
|
164
|
+
|
|
165
|
+
if (isCollection) {
|
|
166
|
+
// Reset if collection instance changed
|
|
167
|
+
if (collectionRef.current !== queryFnOrCollection) {
|
|
168
|
+
collectionRef.current = queryFnOrCollection
|
|
169
|
+
hasValidatedCollectionRef.current = false
|
|
170
|
+
shouldReset = true
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
// Reset if deps changed (for query functions)
|
|
174
|
+
if (prevDepsKeyRef.current !== depsKey) {
|
|
175
|
+
prevDepsKeyRef.current = depsKey
|
|
176
|
+
shouldReset = true
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (shouldReset) {
|
|
181
|
+
setLoadedPageCount(1)
|
|
182
|
+
}
|
|
183
|
+
}, [isCollection, queryFnOrCollection, depsKey])
|
|
184
|
+
|
|
185
|
+
// Create a live query with initial limit and offset
|
|
186
|
+
// Either pass collection directly or wrap query function
|
|
187
|
+
const queryResult = isCollection
|
|
188
|
+
? useLiveQuery(queryFnOrCollection)
|
|
189
|
+
: useLiveQuery(
|
|
190
|
+
(q) => queryFnOrCollection(q).limit(pageSize).offset(0),
|
|
191
|
+
deps
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
// Adjust window when pagination changes
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
const utils = queryResult.collection.utils
|
|
197
|
+
const expectedOffset = 0
|
|
198
|
+
const expectedLimit = loadedPageCount * pageSize + 1 // +1 for peek ahead
|
|
199
|
+
|
|
200
|
+
// Check if collection has orderBy (required for setWindow)
|
|
201
|
+
if (!isLiveQueryCollectionUtils(utils)) {
|
|
202
|
+
// For pre-created collections, throw an error if no orderBy
|
|
203
|
+
if (isCollection) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
`useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. ` +
|
|
206
|
+
`Please add .orderBy() to your createLiveQueryCollection query.`
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// For pre-created collections, validate window on first check
|
|
213
|
+
if (isCollection && !hasValidatedCollectionRef.current) {
|
|
214
|
+
const currentWindow = utils.getWindow()
|
|
215
|
+
if (
|
|
216
|
+
currentWindow &&
|
|
217
|
+
(currentWindow.offset !== expectedOffset ||
|
|
218
|
+
currentWindow.limit !== expectedLimit)
|
|
219
|
+
) {
|
|
220
|
+
console.warn(
|
|
221
|
+
`useLiveInfiniteQuery: Pre-created collection has window {offset: ${currentWindow.offset}, limit: ${currentWindow.limit}} ` +
|
|
222
|
+
`but hook expects {offset: ${expectedOffset}, limit: ${expectedLimit}}. Adjusting window now.`
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
hasValidatedCollectionRef.current = true
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// For query functions, wait until collection is ready
|
|
229
|
+
if (!isCollection && !queryResult.isReady) return
|
|
230
|
+
|
|
231
|
+
// Adjust the window
|
|
232
|
+
const result = utils.setWindow({
|
|
233
|
+
offset: expectedOffset,
|
|
234
|
+
limit: expectedLimit,
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
if (result !== true) {
|
|
238
|
+
setIsFetchingNextPage(true)
|
|
239
|
+
result.then(() => {
|
|
240
|
+
setIsFetchingNextPage(false)
|
|
241
|
+
})
|
|
242
|
+
} else {
|
|
243
|
+
setIsFetchingNextPage(false)
|
|
244
|
+
}
|
|
245
|
+
}, [
|
|
246
|
+
isCollection,
|
|
247
|
+
queryResult.collection,
|
|
248
|
+
queryResult.isReady,
|
|
249
|
+
loadedPageCount,
|
|
250
|
+
pageSize,
|
|
251
|
+
])
|
|
252
|
+
|
|
253
|
+
// Split the data array into pages and determine if there's a next page
|
|
254
|
+
const { pages, pageParams, hasNextPage, flatData } = useMemo(() => {
|
|
255
|
+
const dataArray = (
|
|
256
|
+
Array.isArray(queryResult.data) ? queryResult.data : []
|
|
257
|
+
) as InferResultType<TContext>
|
|
258
|
+
const totalItemsRequested = loadedPageCount * pageSize
|
|
259
|
+
|
|
260
|
+
// Check if we have more data than requested (the peek ahead item)
|
|
261
|
+
const hasMore = dataArray.length > totalItemsRequested
|
|
262
|
+
|
|
263
|
+
// Build pages array (without the peek ahead item)
|
|
264
|
+
const pagesResult: Array<Array<InferResultType<TContext>[number]>> = []
|
|
265
|
+
const pageParamsResult: Array<number> = []
|
|
266
|
+
|
|
267
|
+
for (let i = 0; i < loadedPageCount; i++) {
|
|
268
|
+
const pageData = dataArray.slice(i * pageSize, (i + 1) * pageSize)
|
|
269
|
+
pagesResult.push(pageData)
|
|
270
|
+
pageParamsResult.push(initialPageParam + i)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Flatten the pages for the data return (without peek ahead item)
|
|
274
|
+
const flatDataResult = dataArray.slice(
|
|
275
|
+
0,
|
|
276
|
+
totalItemsRequested
|
|
277
|
+
) as InferResultType<TContext>
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
pages: pagesResult,
|
|
281
|
+
pageParams: pageParamsResult,
|
|
282
|
+
hasNextPage: hasMore,
|
|
283
|
+
flatData: flatDataResult,
|
|
284
|
+
}
|
|
285
|
+
}, [queryResult.data, loadedPageCount, pageSize, initialPageParam])
|
|
286
|
+
|
|
287
|
+
// Fetch next page
|
|
288
|
+
const fetchNextPage = useCallback(() => {
|
|
289
|
+
if (!hasNextPage || isFetchingNextPage) return
|
|
290
|
+
|
|
291
|
+
setLoadedPageCount((prev) => prev + 1)
|
|
292
|
+
}, [hasNextPage, isFetchingNextPage])
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
...queryResult,
|
|
296
|
+
data: flatData,
|
|
297
|
+
pages,
|
|
298
|
+
pageParams,
|
|
299
|
+
fetchNextPage,
|
|
300
|
+
hasNextPage,
|
|
301
|
+
isFetchingNextPage,
|
|
302
|
+
} as UseLiveInfiniteQueryReturn<TContext>
|
|
303
|
+
}
|