Skip to content

Commit 9d1ce62

Browse files
shudingnevilm-lt
authored andcommitted
1 parent 213f18d commit 9d1ce62

File tree

2 files changed

+6
-75
lines changed

2 files changed

+6
-75
lines changed

src/use-swr.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const useSWRHandler = <Data = any, Error = any>(
110110
// Resolve the current validating state.
111111
const resolveValidating = () => {
112112
if (!key || !fetcher) return false
113-
if (info.isValidating) return true
113+
if (cache.get(keyValidating)) return true
114114

115115
// If it's not mounted yet and it should revalidate on mount, revalidate.
116116
return isInitialMount && shouldRevalidate()
@@ -204,8 +204,9 @@ export const useSWRHandler = <Data = any, Error = any>(
204204
}, config.loadingTimeout)
205205
}
206206

207-
// Start the request and save the timestamp.
208-
FETCH[key] = [currentFetcher(...fnArgs), getTimestamp()]
207+
// Start the request and keep the timestamp.
208+
CONCURRENT_PROMISES_TS[key] = getTimestamp()
209+
CONCURRENT_PROMISES[key] = currentFetcher(...fnArgs)
209210
}
210211

211212
// Wait until the ongoing request is done. Deduplication is also

test/use-swr-fetcher.test.tsx

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { act, fireEvent, screen } from '@testing-library/react'
2-
import React, { Suspense, useState } from 'react'
1+
import { act, screen } from '@testing-library/react'
2+
import React, { useState } from 'react'
33
import useSWR from 'swr'
44
import { createKey, renderWithConfig, nextTick } from './utils'
55

@@ -33,74 +33,4 @@ describe('useSWR - fetcher', () => {
3333
// Should fetch with the new fetcher.
3434
await screen.findByText('data:bar')
3535
})
36-
37-
it('should use the latest fetcher reference when the key has been changed', async () => {
38-
const key = createKey()
39-
let fetcher = () => 'foo'
40-
41-
function Page() {
42-
const [prefix, setPrefix] = useState('a')
43-
const { data } = useSWR(prefix + key, fetcher)
44-
45-
return (
46-
<div>
47-
<p>data:{data}</p>
48-
<button
49-
onClick={() => {
50-
setPrefix('b')
51-
}}
52-
>
53-
mutate
54-
</button>
55-
</div>
56-
)
57-
}
58-
59-
renderWithConfig(<Page />)
60-
await screen.findByText('data:foo')
61-
62-
// Change the fetcher and make sure the ref is updated.
63-
fetcher = () => 'bar'
64-
fireEvent.click(screen.getByText('mutate'))
65-
66-
// Should fetch with the new fetcher.
67-
await screen.findByText('data:bar')
68-
})
69-
70-
it('should use the latest fetcher reference with the suspense mode when the key has been changed', async () => {
71-
const key = createKey()
72-
let fetcher = () => 'foo'
73-
74-
function Page() {
75-
const [prefix, setPrefix] = useState('a')
76-
const { data } = useSWR(prefix + key, fetcher, { suspense: true })
77-
78-
return (
79-
<div>
80-
<p>data:{data}</p>
81-
<button
82-
onClick={() => {
83-
setPrefix('b')
84-
}}
85-
>
86-
mutate
87-
</button>
88-
</div>
89-
)
90-
}
91-
92-
renderWithConfig(
93-
<Suspense fallback="loading">
94-
<Page />
95-
</Suspense>
96-
)
97-
await screen.findByText('data:foo')
98-
99-
// Change the fetcher and make sure the ref is updated.
100-
fetcher = () => 'bar'
101-
fireEvent.click(screen.getByText('mutate'))
102-
103-
// Should fetch with the new fetcher.
104-
await screen.findByText('data:bar')
105-
})
10636
})

0 commit comments

Comments
 (0)