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

utils.js

184 lines4.4 KB

1import React from 'react'
2​
3//
4​
5export const statusIdle = 'idle'
6export const statusLoading = 'loading'
7export const statusError = 'error'
8export const statusSuccess = 'success'
9​
10let _uid = 0
11export const uid = () => _uid++
12export const cancelledError = {}
13export let globalStateListeners = []
14export const isServer = typeof window === 'undefined'
15export const noop = () => {}
16export const identity = d => d
17export let Console = console || { error: noop, warn: noop, log: noop }
18​
19export function useUid() {
20 const ref = React.useRef(null)
21​
22 if (ref.current === null) {
23 ref.current = uid()
24 }
25​
26 return ref.current
28​
29export function setConsole(c) {
30 Console = c
32​
33export function useGetLatest(obj) {
34 const ref = React.useRef()
35 ref.current = obj
36​
37 return React.useCallback(() => ref.current, [])
39​
40export function functionalUpdate(updater, old) {
41 return typeof updater === 'function' ? updater(old) : updater
43​
44export function stableStringifyReplacer(_, value) {
45 return isObject(value)
46 ? Object.assign(
47 {},
48 ...Object.keys(value)
49 .sort()
50 .map(key => ({
51 [key]: value[key],
52 }))
53 )
54 : value
56​
57export function stableStringify(obj) {
58 return JSON.stringify(obj, stableStringifyReplacer)
60​
61export function isObject(a) {
62 return a && typeof a === 'object' && !Array.isArray(a)
64​
65export function deepIncludes(a, b) {
66 if (a === b) {
67 return true
68 }
69​
70 if (typeof a !== typeof b) {
71 return false
72 }
73​
74 if (typeof a === 'object') {
75 return !Object.keys(b).some(key => !deepIncludes(a[key], b[key]))
76 }
77​
78 return false
80​
81export function isDocumentVisible() {
82 return (
83 typeof document === 'undefined' ||
84 document.visibilityState === undefined ||
85 document.visibilityState === 'visible' ||
86 document.visibilityState === 'prerender'
87 )
89​
90export function isOnline() {
91 return navigator.onLine === undefined || navigator.onLine
93​
94export function getQueryArgs(args) {
95 if (isObject(args[0])) {
96 if (
97 args[0].hasOwnProperty('queryKey') &&
98 args[0].hasOwnProperty('queryFn')
99 ) {
100 const { queryKey, variables = [], queryFn, config = {} } = args[0]
101 return [queryKey, variables, queryFn, config]
102 } else {
103 throw new Error('queryKey and queryFn keys are required.')
104 }
105 }
106​
107 if (typeof args[2] === 'function') {
108 const [queryKey, variables = [], queryFn, config = {}] = args
109 return [queryKey, variables, queryFn, config]
110 }
111​
112 const [queryKey, queryFn, config = {}] = args
113​
114 return [queryKey, [], queryFn, config]
116​
117export function useMountedCallback(callback) {
118 const mounted = React.useRef(false)
119 React[isServer ? 'useEffect' : 'useLayoutEffect'](() => {
120 mounted.current = true
121 return () => (mounted.current = false)
122 }, [])
123 return React.useCallback(
124 (...args) => (mounted.current ? callback(...args) : void 0),
125 [callback]
126 )
128​
129export function handleSuspense(queryInfo) {
130 if (queryInfo.config.suspense || queryInfo.config.useErrorBoundary) {
131 if (queryInfo.status === statusError) {
132 setTimeout(() => {
133 queryInfo.query.state.status = 'loading'
134 })
135 throw queryInfo.error
136 }
137 }
138​
139 if (queryInfo.config.suspense) {
140 if (queryInfo.status === statusLoading) {
141 queryInfo.query.wasSuspended = true
142 throw queryInfo.refetch()
143 }
144 }
146​
147// This deep-equal is directly based on https://github.com/epoberezkin/fast-deep-equal.
148// The parts for comparing any non-JSON-supported values has been removed
149export function deepEqual(a, b) {
150 if (a === b) return true
151​
152 if (a && b && typeof a == 'object' && typeof b == 'object') {
153 var length, i, keys
154 if (Array.isArray(a)) {
155 length = a.length
156 // eslint-disable-next-line eqeqeq
157 if (length != b.length) return false
158 for (i = length; i-- !== 0; ) if (!deepEqual(a[i], b[i])) return false
159 return true
160 }
161​
162 if (a.valueOf !== Object.prototype.valueOf)
163 return a.valueOf() === b.valueOf()
164​
165 keys = Object.keys(a)
166 length = keys.length
167 if (length !== Object.keys(b).length) return false
168​
169 for (i = length; i-- !== 0; )
170 if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false
171​
172 for (i = length; i-- !== 0; ) {
173 var key = keys[i]
174​
175 if (!deepEqual(a[key], b[key])) return false
176 }
177​
178 return true
179 }
180​
181 // true if both NaN, false otherwise
182 // eslint-disable-next-line no-self-compare
183 return a !== a && b !== b