Skip to content

Commit 8a80ab8

Browse files
authored
Merge branch 'master' into jc/8wd2
2 parents 2574373 + 37ba0e9 commit 8a80ab8

File tree

7 files changed

+69
-24
lines changed

7 files changed

+69
-24
lines changed

immutable/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "swr-immutable",
33
"version": "0.0.1",
4+
"type": "module",
45
"main": "./dist/index.js",
5-
"module": "./dist/index.esm.js",
6+
"module": "./dist/index.mjs",
67
"types": "./dist/immutable",
78
"peerDependencies": {
89
"swr": "*",

infinite/index.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,34 @@ export const infinite = ((<Data, Error, Args extends Arguments>(
168168

169169
const mutate = useCallback(
170170
(
171-
data: Data[] | undefined | Promise<Data[]> | MutatorCallback<Data[]>,
172-
shouldRevalidate = true
171+
...args:
172+
| []
173+
| [undefined | Data[] | Promise<Data[]> | MutatorCallback<Data[]>]
174+
| [
175+
undefined | Data[] | Promise<Data[]> | MutatorCallback<Data[]>,
176+
boolean
177+
]
173178
) => {
179+
const data = args[0]
180+
181+
// Default to true.
182+
const shouldRevalidate = args[1] !== false
183+
174184
// It is possible that the key is still falsy.
175185
if (!contextCacheKey) return
176186

177-
if (shouldRevalidate && !isUndefined(data)) {
178-
// We only revalidate the pages that are changed
179-
const originalData = dataRef.current
180-
cache.set(contextCacheKey, [false, originalData])
181-
} else if (shouldRevalidate) {
182-
// Calling `mutate()`, we revalidate all pages
183-
cache.set(contextCacheKey, [true])
187+
if (shouldRevalidate) {
188+
if (!isUndefined(data)) {
189+
// We only revalidate the pages that are changed
190+
const originalData = dataRef.current
191+
cache.set(contextCacheKey, [false, originalData])
192+
} else {
193+
// Calling `mutate()`, we revalidate all pages
194+
cache.set(contextCacheKey, [true])
195+
}
184196
}
185197

186-
return swr.mutate(data, shouldRevalidate)
198+
return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate()
187199
},
188200
// swr.mutate is always the same reference
189201
// eslint-disable-next-line react-hooks/exhaustive-deps

infinite/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "swr-infinite",
33
"version": "0.0.1",
4+
"type": "module",
45
"main": "./dist/index.js",
5-
"module": "./dist/index.esm.js",
6+
"module": "./dist/index.mjs",
67
"types": "./dist/infinite",
78
"peerDependencies": {
89
"swr": "*",

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
testEnvironment: 'jsdom',
33
testRegex: '/test/.*\\.test\\.tsx?$',
44
modulePathIgnorePatterns: ['<rootDir>/examples/'],
@@ -8,10 +8,10 @@ module.exports = {
88
'@swc-node/jest',
99
{
1010
jsc: {
11-
minify: false,
11+
minify: false
1212
}
13-
},
14-
],
13+
}
14+
]
1515
},
1616
coveragePathIgnorePatterns: ['/node_modules/', '/dist/', '/test/'],
1717
coverageProvider: 'v8',

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
{
22
"name": "swr",
3-
"version": "1.1.0-beta.6",
3+
"version": "1.1.0-beta.8",
44
"description": "React Hooks library for remote data fetching",
5+
"type": "module",
56
"main": "./dist/index.js",
6-
"module": "./dist/index.esm.js",
7+
"module": "./dist/index.mjs",
78
"exports": {
89
"./package.json": "./package.json",
910
".": {
10-
"import": "./dist/index.esm.js",
11+
"import": "./dist/index.mjs",
1112
"require": "./dist/index.js",
1213
"types": "./dist/index.d.ts"
1314
},
1415
"./infinite": {
15-
"import": "./infinite/dist/index.esm.js",
16+
"import": "./infinite/dist/index.mjs",
1617
"require": "./infinite/dist/index.js",
1718
"types": "./infinite/dist/infinite/index.d.ts"
1819
},
1920
"./immutable": {
20-
"import": "./immutable/dist/index.esm.js",
21+
"import": "./immutable/dist/index.mjs",
2122
"require": "./immutable/dist/index.js",
2223
"types": "./immutable/dist/immutable/index.d.ts"
2324
}
2425
},
25-
"react-native": "./dist/index.esm.js",
26+
"react-native": "./dist/index.mjs",
2627
"types": "./dist/index.d.ts",
2728
"files": [
2829
"dist/**",

src/utils/with-middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const withMiddleware = (
1515
| [Key, Fetcher<Data> | null, SWRConfiguration | undefined]
1616
) => {
1717
const [key, fn, config] = normalize(args)
18-
config.use = (config.use || []).concat(middleware)
19-
return useSWR<Data, Error>(key, fn, config)
18+
const middlewares = (config.use || []).concat(middleware)
19+
return useSWR<Data, Error>(key, fn, { ...config, use: middlewares })
2020
}
2121
}

test/use-swr-infinite.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,4 +954,34 @@ describe('useSWRInfinite', () => {
954954
await nextTick()
955955
screen.getByText('data:fallback-1,fallback-2')
956956
})
957+
958+
it('should revalidate the resource with bound mutate when no argument is passed', async () => {
959+
let t = 0
960+
const key = createKey()
961+
const fetcher = jest.fn(async () =>
962+
createResponse(`foo-${t++}`, { delay: 10 })
963+
)
964+
const logger = []
965+
function Page() {
966+
const { data, mutate } = useSWRInfinite(() => key, fetcher, {
967+
dedupingInterval: 0
968+
})
969+
logger.push(data)
970+
return (
971+
<>
972+
<div>data: {String(data)}</div>
973+
<button onClick={() => mutate()}>mutate</button>
974+
</>
975+
)
976+
}
977+
978+
renderWithConfig(<Page />)
979+
await screen.findByText('data: foo-0')
980+
981+
fireEvent.click(screen.getByText('mutate'))
982+
await screen.findByText('data: foo-1')
983+
expect(fetcher).toBeCalledTimes(2)
984+
985+
expect(logger).toEqual([undefined, ['foo-0'], ['foo-1']])
986+
})
957987
})

0 commit comments

Comments
 (0)