Open History to see the last commit
1import React from 'react'
2β
3//
4β
5import { useQueryCache } from './queryCache'
6import { useConfigContext } from './config'
7import {
8 useUid,
9 isDocumentVisible,
10 Console,
11 useGetLatest,
12 useMountedCallback,
13} from './utils'
14β
15export function useBaseQuery(queryKey, queryVariables, queryFn, config = {}) {
16 const instanceId = useUid()
17β
18 config = {
19 ...useConfigContext(),
20 ...config,
21 }
22β
23 const queryCache = useQueryCache()
24β
25 const queryRef = React.useRef()
26β
27 const newQuery = queryCache._buildQuery(
28 queryKey,
29 queryVariables,
30 queryFn,
31 config
32 )
33β
34 const useCachedQuery =
35 queryRef.current &&
36 typeof queryRef.current.queryHash === 'undefined' &&
37 typeof newQuery.queryHash === 'undefined'
38β
39 // Do not use new query with undefined queryHash, if previous query also had undefined queryHash.
40 // Otherwise this will cause infinite loop.
41 if (!useCachedQuery) {
42 queryRef.current = newQuery
43 }
44β
45 const query = queryRef.current
46β
47 const [, unsafeRerender] = React.useState()
48β
49 const rerender = useMountedCallback(unsafeRerender)
50β
51 const getLatestConfig = useGetLatest(config)
52 const refetch = React.useCallback(
53 async ({ throwOnError, ...rest } = {}) => {
54 try {
55 return await query.fetch(rest)
56 } catch (err) {
57 if (throwOnError) {
58 throw err
59 }
60 }
61 },
62 [query]
63 )
64β
65 query.suspenseInstance = {
66 onSuccess: data => getLatestConfig().onSuccess(data),
67 onError: err => getLatestConfig().onError(err),
68 onSettled: (data, err) => getLatestConfig().onSettled(data, err),
69 }
70β
71 // After mount, subscribe to the query
72 React.useEffect(() => {
73 // Update the instance to the query again, but not as a placeholder
74 query.updateInstance({
75 id: instanceId,
76 onStateUpdate: () => rerender({}),
77 onSuccess: data => getLatestConfig().onSuccess(data),
78 onError: err => getLatestConfig().onError(err),
79 onSettled: (data, err) => getLatestConfig().onSettled(data, err),
80 })
81β
82 return query.subscribe(instanceId)
83 }, [getLatestConfig, instanceId, query, rerender])
84β
85 React.useEffect(() => {
86 // Perform the initial fetch for this query if necessary
87 if (
88 !getLatestConfig().manual && // Don't auto fetch if config is set to manual query
89 !query.wasPrefetched && // Don't double fetch for prefetched queries
90 !query.wasSuspended && // Don't double fetch for suspense
91 query.state.isStale && // Only refetch if stale
92 (getLatestConfig().refetchOnMount || query.instances.length === 1)
93 ) {
94 refetch().catch(Console.error)
95 }
96β
97 query.wasPrefetched = false
98 query.wasSuspended = false
99 }, [getLatestConfig, query, refetch])
100β
101 // Handle refetch interval
102 React.useEffect(() => {
103 const query = queryRef.current
104 if (
105 config.refetchInterval &&
106 (!query.currentRefetchInterval ||
107 // shorter interval should override previous one
108 config.refetchInterval < query.currentRefetchInterval)
109 ) {
110 query.currentRefetchInterval = config.refetchInterval
111 clearInterval(query.refetchIntervalId)
112 query.refetchIntervalId = setInterval(() => {
113 if (isDocumentVisible() || config.refetchIntervalInBackground) {
114 refetch().catch(Console.error)
115 }
116 }, config.refetchInterval)
117β
118 return () => {
119 clearInterval(query.refetchIntervalId)
120 delete query.refetchIntervalId
121 delete query.currentRefetchInterval
122 }
123 }
124 }, [config.refetchInterval, config.refetchIntervalInBackground, refetch])
125β
126 return {
127 ...query.state,
128 config,
129 query,
130 refetch,
131 }
132}