Skip to content

Commit ec275b4

Browse files
authored
Initialise nextFocusRevalidatedAt on mount (#2857)
* Initialise nextFocusRevalidatedAt on mount * Update tests to reflect new behaviour
1 parent 67a2011 commit ec275b4

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/index/use-swr.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,15 @@ export const useSWRHandler = <Data = any, Error = any>(
589589

590590
const softRevalidate = revalidate.bind(UNDEFINED, WITH_DEDUPE)
591591

592+
let nextFocusRevalidatedAt = 0
593+
594+
if (getConfig().revalidateOnFocus) {
595+
const initNow = Date.now()
596+
nextFocusRevalidatedAt = initNow + getConfig().focusThrottleInterval
597+
}
598+
592599
// Expose revalidators to global event listeners. So we can trigger
593600
// revalidation from the outside.
594-
let nextFocusRevalidatedAt = 0
595601
const onRevalidate = (
596602
type: RevalidateEvent,
597603
opts: {

test/use-swr-cache.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ describe('useSWR - cache provider', () => {
180180
let value = 0
181181
function Page() {
182182
const { data } = useSWR(key, () => value++, {
183-
dedupingInterval: 0
183+
dedupingInterval: 0,
184+
focusThrottleInterval: 0
184185
})
185186
return <>{String(data)}</>
186187
}
@@ -402,7 +403,8 @@ describe('useSWR - global cache', () => {
402403
let value = 0
403404
function Page() {
404405
const { data } = useSWR(key, () => value++, {
405-
dedupingInterval: 0
406+
dedupingInterval: 0,
407+
focusThrottleInterval: 0
406408
})
407409
return <>{String(data)}</>
408410
}

test/use-swr-focus.test.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ describe('useSWR - focus', () => {
1717
const key = createKey()
1818
function Page() {
1919
const { data } = useSWR(key, () => value++, {
20-
dedupingInterval: 0
20+
dedupingInterval: 0,
21+
focusThrottleInterval: 0
2122
})
2223
return <div>data: {data}</div>
2324
}
@@ -131,19 +132,17 @@ describe('useSWR - focus', () => {
131132
await screen.findByText('data: 0')
132133

133134
await waitForNextTick()
134-
// trigger revalidation
135-
await focusWindow()
136135
// still in throttling interval
137136
await act(() => sleep(20))
138137
// should be throttled
139138
await focusWindow()
140-
await screen.findByText('data: 1')
139+
await screen.findByText('data: 0')
141140
// wait for focusThrottleInterval
142141
await act(() => sleep(100))
143142

144143
// trigger revalidation again
145144
await focusWindow()
146-
await screen.findByText('data: 2')
145+
await screen.findByText('data: 1')
147146
})
148147

149148
it('focusThrottleInterval should be stateful', async () => {
@@ -168,17 +167,17 @@ describe('useSWR - focus', () => {
168167
await screen.findByText('data: 0')
169168

170169
await waitForNextTick()
171-
// trigger revalidation
170+
// trigger revalidation, won't revalidate as within 50ms
172171
await focusWindow()
173172
// wait for throttle interval
174173
await act(() => sleep(100))
175-
// trigger revalidation
174+
// trigger revalidation, will revalidate as 50ms passed
176175
await focusWindow()
177-
await screen.findByText('data: 2')
176+
await screen.findByText('data: 1')
178177

179178
await waitForNextTick()
180179
// increase focusThrottleInterval
181-
fireEvent.click(screen.getByText('data: 2'))
180+
fireEvent.click(screen.getByText('data: 1'))
182181
// wait for throttle interval
183182
await act(() => sleep(100))
184183
// trigger revalidation
@@ -187,15 +186,15 @@ describe('useSWR - focus', () => {
187186
await act(() => sleep(100))
188187
// should be throttled
189188
await focusWindow()
190-
await screen.findByText('data: 3')
189+
await screen.findByText('data: 2')
191190

192191
// wait for throttle interval
193192
await act(() => sleep(150))
194193
// trigger revalidation
195194
await focusWindow()
196195
// wait for throttle intervals
197196
await act(() => sleep(150))
198-
await screen.findByText('data: 4')
197+
await screen.findByText('data: 3')
199198
})
200199

201200
it('should revalidate on focus even with custom cache', async () => {
@@ -205,7 +204,8 @@ describe('useSWR - focus', () => {
205204
function Page() {
206205
const { data } = useSWR(key, () => value++, {
207206
revalidateOnFocus: true,
208-
dedupingInterval: 0
207+
dedupingInterval: 0,
208+
focusThrottleInterval: 0
209209
})
210210
return <div>data: {data}</div>
211211
}

0 commit comments

Comments
 (0)