Skip to content

Commit

Permalink
refactor cleanupState
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Oct 11, 2021
1 parent 59e8cf9 commit 2c47047
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const useSWRHandler = <Data = any, Error = any>(
}

let newData: Data
let startAt: number | undefined
let startAt: number
let loading = true
const opts = revalidateOpts || {}

Expand All @@ -144,10 +144,10 @@ export const useSWRHandler = <Data = any, Error = any>(
key === keyRef.current &&
initialMountedRef.current

const cleanupState = (ts?: number) => {
const cleanupState = () => {
// CONCURRENT_PROMISES_TS[key] might be overridden, check if it's still
// the same request before deleting.
if (CONCURRENT_PROMISES_TS[key] === ts) {
if (CONCURRENT_PROMISES_TS[key] === startAt) {
delete CONCURRENT_PROMISES[key]
delete CONCURRENT_PROMISES_TS[key]
}
Expand Down Expand Up @@ -201,7 +201,7 @@ export const useSWRHandler = <Data = any, Error = any>(
if (shouldStartNewRequest) {
// If the request isn't interrupted, clean it up after the
// deduplication interval.
setTimeout(() => cleanupState(startAt), config.dedupingInterval)
setTimeout(cleanupState, config.dedupingInterval)
}

// If there're other ongoing request(s), started after the current one,
Expand Down Expand Up @@ -272,7 +272,7 @@ export const useSWRHandler = <Data = any, Error = any>(
}
}
} catch (err) {
cleanupState(startAt)
cleanupState()

// Not paused, we continue handling the error. Otherwise discard it.
if (!getConfig().isPaused()) {
Expand Down

0 comments on commit 2c47047

Please sign in to comment.