Open History to see the last commit
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
27}
28β
29export function setConsole(c) {
30 Console = c
31}
32β
33export function useGetLatest(obj) {
34 const ref = React.useRef()
35 ref.current = obj
36β
37 return React.useCallback(() => ref.current, [])
38}
39β
40export function functionalUpdate(updater, old) {
41 return typeof updater === 'function' ? updater(old) : updater
42}
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
55}
56β
57export function stableStringify(obj) {
58 return JSON.stringify(obj, stableStringifyReplacer)
59}
60β
61export function isObject(a) {
62 return a && typeof a === 'object' && !Array.isArray(a)
63}
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
79}
80β
81export function isDocumentVisible() {
82 return (
83 typeof document === 'undefined' ||
84 document.visibilityState === undefined ||
85 document.visibilityState === 'visible' ||
86 document.visibilityState === 'prerender'
87 )
88}
89β
90export function isOnline() {
91 return navigator.onLine === undefined || navigator.onLine
92}
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]
115}
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 )
127}
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 }
145}
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
184}