Open History to see the last commit
1import React from 'react'
2β
3//
4β
5import { useBaseQuery } from './useBaseQuery'
6import { getQueryArgs, useGetLatest, handleSuspense } from './utils'
7β
8export function useInfiniteQuery(...args) {
9 const queryInfoRef = React.useRef()
10 let [queryKey, queryVariables, queryFn, config = {}] = getQueryArgs(args)
11β
12 const { getFetchMore } = config
13 const getGetFetchMore = useGetLatest(getFetchMore)
14β
15 // The default queryFn will query all pages and map them together
16 const originalQueryFn = queryFn
17β
18 queryFn = async () => {
19 const data = []
20 const pageVariables = [...queryInfoRef.current.query.pageVariables]
21 const rebuiltPageVariables = []
22β
23 do {
24 const args = pageVariables.shift()
25β
26 if (!data.length) {
27 // the first page query doesn't need to be rebuilt
28 data.push(await originalQueryFn(...args))
29 rebuiltPageVariables.push(args)
30 } else {
31 // get an up-to-date cursor based on the previous data set
32 const nextCursor = getGetFetchMore()(data[data.length - 1], data)
33β
34 // break early if there's no next cursor
35 // otherwise we'll start from the beginning
36 // which will cause unwanted duplication
37 if (!nextCursor) {
38 break
39 }
40β
41 const pageArgs = [
42 // remove the last argument (the previously saved cursor)
43 ...args.slice(0, -1),
44 nextCursor,
45 ]
46β
47 data.push(await originalQueryFn(...pageArgs))
48 rebuiltPageVariables.push(pageArgs)
49 }
50 } while (pageVariables.length)
51β
52 queryInfoRef.current.query.canFetchMore = getGetFetchMore()(
53 data[data.length - 1],
54 data
55 )
56 queryInfoRef.current.query.pageVariables = rebuiltPageVariables
57β
58 return data
59 }
60β
61 const queryInfo = useBaseQuery(queryKey, queryVariables, queryFn, config)
62β
63 if (
64 typeof queryInfo.query.canFetchMore === 'undefined' &&
65 typeof queryInfo.data !== 'undefined'
66 ) {
67 queryInfo.query.canFetchMore = getGetFetchMore()(
68 queryInfo.data[queryInfo.data.length - 1],
69 queryInfo.data
70 )
71 }
72β
73 queryInfoRef.current = queryInfo
74β
75 let {
76 refetch,
77 data = [],
78 query: { canFetchMore },
79 } = queryInfo
80β
81 // Here we seed the pageVariabes for the query
82 if (!queryInfo.query.pageVariables) {
83 queryInfo.query.pageVariables = [
84 [...queryInfo.query.queryKey, ...queryInfo.query.queryVariables],
85 ]
86 }
87β
88 const fetchMore = React.useCallback(
89 (fetchMoreInfo = queryInfoRef.current.query.canFetchMore) =>
90 queryInfoRef.current.query.canFetchMore
91 ? refetch({
92 force: true,
93 __queryFn: async (...args) => {
94 try {
95 queryInfoRef.current.query.setState(old => ({
96 ...old,
97 isFetchingMore: true,
98 }))
99β
100 const newArgs = [...args, fetchMoreInfo]
101 queryInfoRef.current.query.pageVariables.push(newArgs)
102β
103 const data = [
104 ...queryInfoRef.current.data,
105 await originalQueryFn(...newArgs),
106 ]
107β
108 queryInfoRef.current.query.canFetchMore = getGetFetchMore()(
109 data[data.length - 1],
110 data
111 )
112β
113 return data
114 } finally {
115 queryInfoRef.current.query.setState(old => ({
116 ...old,
117 isFetchingMore: false,
118 }))
119 }
120 },
121 })
122 : void 0,
123 [getGetFetchMore, originalQueryFn, refetch]
124 )
125β
126 handleSuspense(queryInfo)
127β
128 return {
129 ...queryInfo,
130 data,
131 canFetchMore,
132 fetchMore,
133 }
134}