Skip to content

Commit 3ef9c7c

Browse files
committed
2 parents de633a3 + 51375fd commit 3ef9c7c

20 files changed

+592
-106
lines changed

.babelrc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
],
1111
"@babel/react"
1212
],
13-
"plugins": ["babel-plugin-transform-async-to-promises"],
13+
"plugins": [
14+
[
15+
"babel-plugin-transform-async-to-promises",
16+
{
17+
externalHelpers: true
18+
}
19+
]
20+
],
1421
"env": {
1522
"test": {
1623
"presets": [
@@ -22,7 +29,8 @@
2229
"exclude": ["@babel/plugin-transform-regenerator"]
2330
}
2431
]
25-
]
32+
],
33+
"plugins": ["babel-plugin-transform-async-to-promises"]
2634
}
2735
}
2836
}

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ If you have been assigned to fix an issue or develop a new feature, please follo
2424
- To run examples, follow their individual directions. Usually this is just `$ yarn && yarn start`.
2525
- To run examples using your local build, link to the local `react-query` by running `$ yarn link react-query` from the example's directory
2626
- Document your changes in the appropriate doc page
27-
- Git stage your required chnages and commit (see below commit guidelines)
27+
- Git stage your required changes and commit (see below commit guidelines)
2828
- Submit PR for review
2929

3030
## Commit message conventions

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,17 @@ This library is being built and maintained by me, @tannerlinsley and I am always
250250
- [`queryCache.getQueryData`](#querycachegetquerydata)
251251
- [`queryCache.setQueryData`](#querycachesetquerydata)
252252
- [`queryCache.refetchQueries`](#querycacherefetchqueries)
253+
- [`queryCache.cancelQueries`](#querycachecancelqueries)
253254
- [`queryCache.removeQueries`](#querycacheremovequeries)
254255
- [`queryCache.getQuery`](#querycachegetquery)
255256
- [`queryCache.getQueries`](#querycachegetqueries)
256257
- [`queryCache.isFetching`](#querycacheisfetching)
257258
- [`queryCache.subscribe`](#querycachesubscribe)
258259
- [`queryCache.clear`](#querycacheclear)
260+
- [`useQueryCache`](#usequerycache)
259261
- [`useIsFetching`](#useisfetching)
260262
- [`ReactQueryConfigProvider`](#reactqueryconfigprovider)
263+
- [`ReactQueryCacheProvider`](#reactquerycacheprovider)
261264
- [`setConsole`](#setconsole)
262265
- [Contributors ✨](#contributors-)
263266

@@ -2236,6 +2239,7 @@ The `queryCache` instance is the backbone of React Query that manages all of the
22362239
- [`getQueryData`](#querycachegetquerydata)
22372240
- [`setQueryData`](#querycachesetquerydata)
22382241
- [`refetchQueries`](#querycacherefetchqueries)
2242+
- [`cancelQueries`](#querycachecancelqueries)
22392243
- [`removeQueries`](#querycacheremovequeries)
22402244
- [`getQueries`](#querycachegetqueries)
22412245
- [`getQuery`](#querycachegetquery)
@@ -2361,7 +2365,7 @@ const queries = queryCache.refetchQueries(inclusiveQueryKeyOrPredicateFn, {
23612365
- This predicate function will be called for every single query in the cache and be expected to return truthy for queries that are `found`.
23622366
- The `exact` option has no effect with using a function
23632367
- `exact: Boolean`
2364-
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed. Don't remember to destructure it out of the array!
2368+
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed. Remember to destructure it out of the array!
23652369
- `throwOnError: Boolean`
23662370
- When set to `true`, this function will throw if any of the query refetch tasks fail.
23672371
- `force: Boolean`
@@ -2394,7 +2398,7 @@ const queries = queryCache.cancelQueries(queryKeyOrPredicateFn, {
23942398
- This predicate function will be called for every single query in the cache and be expected to return truthy for queries that are `found`.
23952399
- The `exact` option has no effect with using a function
23962400
- `exact: Boolean`
2397-
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed. Don't remember to destructure it out of the array!
2401+
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed. Remember to destructure it out of the array!
23982402

23992403
### Returns
24002404

@@ -2421,7 +2425,7 @@ const queries = queryCache.removeQueries(queryKeyOrPredicateFn, {
24212425
- This predicate function will be called for every single query in the cache and be expected to return truthy for queries that are `found`.
24222426
- The `exact` option has no effect with using a function
24232427
- `exact: Boolean`
2424-
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed. Don't remember to destructure it out of the array!
2428+
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed. Remember to destructure it out of the array!
24252429

24262430
### Returns
24272431

@@ -2522,6 +2526,18 @@ queryCache.clear()
25222526
- `queries: Array<Query>`
25232527
- This will be an array containing the queries that were found.
25242528
2529+
## `useQueryCache`
2530+
2531+
The `useQueryCache` hook returns the current queryCache instance.
2532+
2533+
```js
2534+
import { useQueryCache } from 'react-query';
2535+
2536+
const queryCache = useQueryCache()
2537+
```
2538+
2539+
If you are using the `ReactQueryCacheProvider` to set a custom cache, you cannot simply import `{ queryCache }` any more. This hook will ensure you're getting the correct instance.
2540+
25252541
## `useIsFetching`
25262542
25272543
`useIsFetching` is an optional hook that returns the `number` of the queries that your application is loading or fetching in the background (useful for app-wide loading indicators).
@@ -2582,6 +2598,29 @@ function App() {
25822598
- Must be **stable** or **memoized**. Do not create an inline object!
25832599
- For non-global properties please see their usage in both the [`useQuery` hook](#usequery) and the [`useMutation` hook](#usemutation).
25842600
2601+
## `ReactQueryCacheProvider`
2602+
2603+
`ReactQueryCacheProvider` is an optional provider component for explicitly setting the query cache used by React Query. This is useful for creating component-level caches that are not completely global, as well as making truly isolated unit tests.
2604+
2605+
```js
2606+
import { ReactQueryCacheProvider, makeQueryCache } from 'react-query';
2607+
2608+
const queryCache = makeQueryCache()
2609+
2610+
function App() {
2611+
return (
2612+
<ReactQueryCacheProvider queryCache={queryCache}>
2613+
...
2614+
</ReactQueryCacheProvider>
2615+
)
2616+
}
2617+
```
2618+
2619+
### Options
2620+
- `queryCache: Object`
2621+
- In instance of queryCache, you can use the `makeQueryCache` factory to create this.
2622+
- If not provided, a new cache will be generated.
2623+
25852624
## `setConsole`
25862625
25872626
`setConsole` is an optional utility function that allows you to replace the `console` interface used to log errors. By default, the `window.console` object is used. If no global `console` object is found in the environment, nothing will be logged.

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default [
3232
sourcemap: true,
3333
},
3434
external,
35-
plugins: [resolve(), babel(), commonJS(), externalDeps()],
35+
plugins: [resolve(), babel(), commonJS(), externalDeps(), terser()],
3636
},
3737
{
3838
input: 'src/index.js',

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export { queryCache } from './queryCache'
1+
export {
2+
queryCache,
3+
makeQueryCache,
4+
ReactQueryCacheProvider,
5+
useQueryCache,
6+
} from './queryCache'
27
export { ReactQueryConfigProvider } from './config'
38
export { setFocusHandler } from './setFocusHandler'
49
export { useIsFetching } from './useIsFetching'

src/index.production.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export { queryCache } from './queryCache'
1+
export {
2+
queryCache,
3+
makeQueryCache,
4+
ReactQueryCacheProvider,
5+
useQueryCache,
6+
} from './queryCache'
27
export { ReactQueryConfigProvider } from './config'
38
export { setFocusHandler } from './setFocusHandler'
49
export { useIsFetching } from './useIsFetching'

src/queryCache.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react'
12
import {
23
isServer,
34
functionalUpdate,
@@ -14,6 +15,42 @@ import { defaultConfigRef } from './config'
1415

1516
export const queryCache = makeQueryCache()
1617

18+
export const queryCacheContext = React.createContext(queryCache)
19+
20+
export const queryCaches = [queryCache]
21+
22+
export function useQueryCache() {
23+
return React.useContext(queryCacheContext)
24+
}
25+
26+
export function ReactQueryCacheProvider({ queryCache, children }) {
27+
const cache = React.useMemo(() => queryCache || makeQueryCache(), [
28+
queryCache,
29+
])
30+
31+
React.useEffect(() => {
32+
queryCaches.push(cache)
33+
34+
return () => {
35+
// remove the cache from the active list
36+
const i = queryCaches.indexOf(cache)
37+
if (i >= 0) {
38+
queryCaches.splice(i, 1)
39+
}
40+
// if the cache was created by us, we need to tear it down
41+
if (queryCache == null) {
42+
cache.clear()
43+
}
44+
}
45+
}, [cache, queryCache])
46+
47+
return (
48+
<queryCacheContext.Provider value={cache}>
49+
{children}
50+
</queryCacheContext.Provider>
51+
)
52+
}
53+
1754
const actionInit = {}
1855
const actionFailed = {}
1956
const actionMarkStale = {}
@@ -32,7 +69,7 @@ export function makeQueryCache() {
3269
}
3370

3471
const notifyGlobalListeners = () => {
35-
cache.isFetching = Object.values(queryCache.queries).reduce(
72+
cache.isFetching = Object.values(cache.queries).reduce(
3673
(acc, query) => (query.state.isFetching ? acc + 1 : acc),
3774
0
3875
)
@@ -129,6 +166,7 @@ export function makeQueryCache() {
129166
query.config = { ...query.config, ...config }
130167
} else {
131168
query = makeQuery({
169+
cache,
132170
queryKey,
133171
queryHash,
134172
queryVariables,
@@ -212,6 +250,7 @@ export function makeQueryCache() {
212250
}
213251

214252
function makeQuery(options) {
253+
const queryCache = options.cache
215254
const reducer = options.config.queryReducer || defaultQueryReducer
216255

217256
const noQueryHash = typeof options.queryHash === 'undefined'

src/setFocusHandler.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isOnline, isDocumentVisible, Console, isServer } from './utils'
22
import { defaultConfigRef } from './config'
3-
import { queryCache } from './queryCache'
3+
import { queryCaches } from './queryCache'
44

55
const visibilityChangeEvent = 'visibilitychange'
66
const focusEvent = 'focus'
@@ -9,29 +9,31 @@ const onWindowFocus = () => {
99
const { refetchAllOnWindowFocus } = defaultConfigRef.current
1010

1111
if (isDocumentVisible() && isOnline()) {
12-
queryCache
13-
.refetchQueries(query => {
14-
if (!query.instances.length) {
15-
return false
16-
}
17-
18-
if (query.config.manual === true) {
19-
return false
20-
}
21-
22-
if (query.shouldContinueRetryOnFocus) {
23-
// delete promise, so `fetch` will create new one
24-
delete query.promise
25-
return true
26-
}
27-
28-
if (typeof query.config.refetchOnWindowFocus === 'undefined') {
29-
return refetchAllOnWindowFocus
30-
} else {
31-
return query.config.refetchOnWindowFocus
32-
}
33-
})
34-
.catch(Console.error)
12+
queryCaches.forEach(queryCache =>
13+
queryCache
14+
.refetchQueries(query => {
15+
if (!query.instances.length) {
16+
return false
17+
}
18+
19+
if (query.config.manual === true) {
20+
return false
21+
}
22+
23+
if (query.shouldContinueRetryOnFocus) {
24+
// delete promise, so `fetch` will create new one
25+
delete query.promise
26+
return true
27+
}
28+
29+
if (typeof query.config.refetchOnWindowFocus === 'undefined') {
30+
return refetchAllOnWindowFocus
31+
} else {
32+
return query.config.refetchOnWindowFocus
33+
}
34+
})
35+
.catch(Console.error)
36+
)
3537
}
3638
}
3739

0 commit comments

Comments
 (0)