Skip to content

Commit 2de8528

Browse files
committed
chore: Reorganize code to core + react
1 parent bcecb54 commit 2de8528

33 files changed

+311
-292
lines changed

rollup.config.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ const globals = {
1313
react: 'React',
1414
}
1515

16+
const inputSrc = 'src/react/index.js'
17+
1618
export default [
1719
{
18-
input: 'src/index.js',
20+
input: inputSrc,
1921
output: {
2022
file: 'dist/react-query.mjs',
2123
format: 'es',
@@ -25,7 +27,7 @@ export default [
2527
plugins: [resolve(), babel(), commonJS(), externalDeps()],
2628
},
2729
{
28-
input: 'src/index.js',
30+
input: inputSrc,
2931
output: {
3032
file: 'dist/react-query.min.mjs',
3133
format: 'es',
@@ -35,7 +37,7 @@ export default [
3537
plugins: [resolve(), babel(), commonJS(), externalDeps(), terser()],
3638
},
3739
{
38-
input: 'src/index.js',
40+
input: inputSrc,
3941
output: {
4042
name: 'ReactQuery',
4143
file: 'dist/react-query.development.js',
@@ -47,7 +49,7 @@ export default [
4749
plugins: [resolve(), babel(), commonJS(), externalDeps()],
4850
},
4951
{
50-
input: 'src/index.js',
52+
input: inputSrc,
5153
output: {
5254
name: 'ReactQuery',
5355
file: 'dist/react-query.production.min.js',
Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import React from 'react'
21
import { noop, stableStringify, identity, deepEqual } from './utils'
32

4-
export const configContext = React.createContext()
5-
6-
const DEFAULT_CONFIG = {
3+
export const DEFAULT_CONFIG = {
74
shared: {
85
suspense: false,
96
},
@@ -40,59 +37,6 @@ export const defaultConfigRef = {
4037
current: DEFAULT_CONFIG,
4138
}
4239

43-
export function useConfigContext() {
44-
return React.useContext(configContext) || defaultConfigRef.current
45-
}
46-
47-
export function ReactQueryConfigProvider({ config, children }) {
48-
let configContextValue = useConfigContext()
49-
50-
const newConfig = React.useMemo(() => {
51-
const { shared = {}, queries = {}, mutations = {} } = config
52-
const {
53-
shared: contextShared = {},
54-
queries: contextQueries = {},
55-
mutations: contextMutations = {},
56-
} = configContextValue
57-
58-
return {
59-
shared: {
60-
...contextShared,
61-
...shared,
62-
},
63-
queries: {
64-
...contextQueries,
65-
...queries,
66-
},
67-
mutations: {
68-
...contextMutations,
69-
...mutations,
70-
},
71-
}
72-
}, [config, configContextValue])
73-
74-
React.useEffect(() => {
75-
// restore previous config on unmount
76-
return () => {
77-
defaultConfigRef.current = { ...(configContextValue || DEFAULTS) }
78-
}
79-
}, [configContextValue])
80-
81-
if (!configContextValue) {
82-
defaultConfigRef.current = newConfig
83-
}
84-
85-
return (
86-
<configContext.Provider value={newConfig}>
87-
{children}
88-
</configContext.Provider>
89-
)
90-
}
91-
92-
function invalidQueryKey() {
93-
throw new Error('A valid query key is required!')
94-
}
95-
9640
export function defaultQueryKeySerializerFn(queryKey) {
9741
if (!queryKey) {
9842
invalidQueryKey()
@@ -115,3 +59,7 @@ export function defaultQueryKeySerializerFn(queryKey) {
11559

11660
return [queryHash, queryKey]
11761
}
62+
63+
function invalidQueryKey() {
64+
throw new Error('A valid query key is required!')
65+
}

src/core/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export { queryCache, queryCaches, makeQueryCache } from './queryCache'
2+
export { setFocusHandler } from './setFocusHandler'
3+
export {
4+
statusIdle,
5+
statusLoading,
6+
statusSuccess,
7+
statusError,
8+
stableStringify,
9+
setConsole,
10+
deepIncludes,
11+
} from './utils'
Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from 'react'
21
import {
32
isServer,
43
functionalUpdate,
@@ -15,45 +14,13 @@ import {
1514
Console,
1615
isObject,
1716
} from './utils'
17+
1818
import { defaultConfigRef } from './config'
1919

2020
export const queryCache = makeQueryCache()
2121

22-
export const queryCacheContext = React.createContext(queryCache)
23-
2422
export const queryCaches = [queryCache]
2523

26-
export const useQueryCache = () => React.useContext(queryCacheContext)
27-
28-
export function ReactQueryCacheProvider({ queryCache, children }) {
29-
const resolvedQueryCache = React.useMemo(
30-
() => queryCache || makeQueryCache(),
31-
[queryCache]
32-
)
33-
34-
React.useEffect(() => {
35-
queryCaches.push(resolvedQueryCache)
36-
37-
return () => {
38-
// remove the cache from the active list
39-
const i = queryCaches.indexOf(resolvedQueryCache)
40-
if (i > -1) {
41-
queryCaches.splice(i, 1)
42-
}
43-
// if the resolvedQueryCache was created by us, we need to tear it down
44-
if (queryCache == null) {
45-
resolvedQueryCache.clear()
46-
}
47-
}
48-
}, [resolvedQueryCache, queryCache])
49-
50-
return (
51-
<queryCacheContext.Provider value={resolvedQueryCache}>
52-
{children}
53-
</queryCacheContext.Provider>
54-
)
55-
}
56-
5724
const actionInit = 'Init'
5825
const actionFailed = 'Failed'
5926
const actionMarkStale = 'MarkStale'
@@ -248,6 +215,7 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) {
248215
if (throwOnError) {
249216
throw err
250217
}
218+
Console.error(err)
251219
}
252220
}
253221

src/core/tests/utils.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { setConsole, queryCache, queryCaches } from '../'
2+
import { deepEqual } from '../utils'
3+
4+
describe('core/utils', () => {
5+
afterEach(() => {
6+
queryCaches.forEach(cache => cache.clear())
7+
})
8+
9+
it('setConsole should override Console object', async () => {
10+
const mockConsole = {
11+
error: jest.fn(),
12+
}
13+
14+
setConsole(mockConsole)
15+
16+
await queryCache.prefetchQuery(
17+
'key',
18+
async () => {
19+
throw new Error('Test')
20+
},
21+
{
22+
retry: 0,
23+
}
24+
)
25+
26+
expect(mockConsole.error).toHaveBeenCalled()
27+
28+
setConsole(console)
29+
})
30+
31+
it('deepequal should return `false` for different dates', () => {
32+
const date1 = new Date(2020, 3, 1)
33+
const date2 = new Date(2020, 3, 2)
34+
35+
expect(deepEqual(date1, date2)).toEqual(false)
36+
})
37+
38+
it('should return `true` for equal dates', () => {
39+
const date1 = new Date(2020, 3, 1)
40+
const date2 = new Date(2020, 3, 1)
41+
42+
expect(deepEqual(date1, date2)).toEqual(true)
43+
})
44+
})

src/utils.js renamed to src/core/utils.js

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import React from 'react'
2-
import { useConfigContext } from './config'
3-
4-
//
5-
61
export const statusIdle = 'idle'
72
export const statusLoading = 'loading'
83
export const statusError = 'error'
@@ -21,27 +16,10 @@ export function identity(d) {
2116
}
2217
export let Console = console || { error: noop, warn: noop, log: noop }
2318

24-
export function useUid() {
25-
const ref = React.useRef(null)
26-
27-
if (ref.current === null) {
28-
ref.current = uid()
29-
}
30-
31-
return ref.current
32-
}
33-
3419
export function setConsole(c) {
3520
Console = c
3621
}
3722

38-
export function useGetLatest(obj) {
39-
const ref = React.useRef()
40-
ref.current = obj
41-
42-
return React.useCallback(() => ref.current, [])
43-
}
44-
4523
export function functionalUpdate(updater, old) {
4624
return typeof updater === 'function' ? updater(old) : updater
4725
}
@@ -112,54 +90,6 @@ export function getQueryArgs(args) {
11290
return [queryKey, queryFn ? { ...config, queryFn } : config, ...rest]
11391
}
11492

115-
export function useQueryArgs(args) {
116-
const configContext = useConfigContext()
117-
118-
let [queryKey, config, ...rest] = getQueryArgs(args)
119-
120-
// Build the final config
121-
config = {
122-
...configContext.shared,
123-
...configContext.queries,
124-
...config,
125-
}
126-
127-
return [queryKey, config, ...rest]
128-
}
129-
130-
export function useMountedCallback(callback) {
131-
const mounted = React.useRef(false)
132-
133-
React[isServer ? 'useEffect' : 'useLayoutEffect'](() => {
134-
mounted.current = true
135-
return () => (mounted.current = false)
136-
}, [])
137-
138-
return React.useCallback(
139-
(...args) => (mounted.current ? callback(...args) : void 0),
140-
[callback]
141-
)
142-
}
143-
144-
export function handleSuspense(queryInfo) {
145-
if (
146-
queryInfo.query.config.suspense ||
147-
queryInfo.query.config.useErrorBoundary
148-
) {
149-
if (
150-
queryInfo.query.state.status === statusError &&
151-
queryInfo.query.state.throwInErrorBoundary
152-
) {
153-
throw queryInfo.error
154-
}
155-
156-
if (queryInfo.query.config.suspense && queryInfo.status !== statusSuccess) {
157-
queryInfo.query.wasSuspended = true
158-
throw queryInfo.query.fetch()
159-
}
160-
}
161-
}
162-
16393
// This deep-equal is directly based on https://github.com/epoberezkin/fast-deep-equal.
16494
// The parts for comparing any non-JSON-supported values has been removed
16595
export function deepEqual(a, b) {

src/index.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)