Skip to content

Commit

Permalink
rename CONCURRENT_REQUESTS to FETCH (vercel#1749)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding authored and nevilm-lt committed Apr 22, 2022
1 parent 18f9295 commit 1bad971
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
17 changes: 7 additions & 10 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const useSWRHandler = <Data = any, Error = any>(
refreshWhenOffline
} = config

const [EVENT_REVALIDATORS, STATE_UPDATERS, MUTATION, CONCURRENT_REQUESTS] =
const [EVENT_REVALIDATORS, STATE_UPDATERS, MUTATION, FETCH] =
SWRGlobalState.get(cache) as GlobalState

// `key` is the identifier of the SWR `data` state, `keyErr` and
Expand Down Expand Up @@ -146,7 +146,7 @@ export const useSWRHandler = <Data = any, Error = any>(

// If there is no ongoing concurrent request, or `dedupe` is not set, a
// new request should be initiated.
const shouldStartNewRequest = !CONCURRENT_REQUESTS[key] || !opts.dedupe
const shouldStartNewRequest = !FETCH[key] || !opts.dedupe

// Do unmount check for calls:
// If key has changed during the revalidation, or the component has been
Expand All @@ -159,9 +159,9 @@ export const useSWRHandler = <Data = any, Error = any>(

const cleanupState = () => {
// Check if it's still the same request before deleting.
const requestInfo = CONCURRENT_REQUESTS[key]
const requestInfo = FETCH[key]
if (requestInfo && requestInfo[1] === startAt) {
delete CONCURRENT_REQUESTS[key]
delete FETCH[key]
}
}

Expand Down Expand Up @@ -203,12 +203,12 @@ export const useSWRHandler = <Data = any, Error = any>(
}

// Start the request and save the timestamp.
CONCURRENT_REQUESTS[key] = [currentFetcher(...fnArgs), getTimestamp()]
FETCH[key] = [currentFetcher(...fnArgs), getTimestamp()]
}

// Wait until the ongoing request is done. Deduplication is also
// considered here.
;[newData, startAt] = CONCURRENT_REQUESTS[key]
;[newData, startAt] = FETCH[key]
newData = await newData

if (shouldStartNewRequest) {
Expand All @@ -223,10 +223,7 @@ export const useSWRHandler = <Data = any, Error = any>(
// req2---------------->res2
// the request that fired later will always be kept.
// The timestamp maybe be `undefined` or a number
if (
!CONCURRENT_REQUESTS[key] ||
CONCURRENT_REQUESTS[key][1] !== startAt
) {
if (!FETCH[key] || FETCH[key][1] !== startAt) {
if (shouldStartNewRequest) {
if (isCurrentKeyMounted()) {
getConfig().onDiscarded(key)
Expand Down
7 changes: 4 additions & 3 deletions src/utils/broadcast-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export const broadcastState: Broadcaster = (
revalidate,
broadcast = true
) => {
const [EVENT_REVALIDATORS, STATE_UPDATERS, , CONCURRENT_REQUESTS] =
SWRGlobalState.get(cache) as GlobalState
const [EVENT_REVALIDATORS, STATE_UPDATERS, , FETCH] = SWRGlobalState.get(
cache
) as GlobalState
const revalidators = EVENT_REVALIDATORS[key]
const updaters = STATE_UPDATERS[key]

Expand All @@ -27,7 +28,7 @@ export const broadcastState: Broadcaster = (
if (revalidate) {
// Invalidate the key by deleting the concurrent request markers so new
// requests will not be deduped.
delete CONCURRENT_REQUESTS[key]
delete FETCH[key]

if (revalidators && revalidators[0]) {
return revalidators[0](revalidateEvents.MUTATE_EVENT).then(() =>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/global-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type GlobalState = [
Record<string, RevalidateCallback[]>, // EVENT_REVALIDATORS
Record<string, StateUpdateCallback[]>, // STATE_UPDATERS
Record<string, [number, number]>, // MUTATION: [ts, end_ts]
Record<string, [any, number]>, // CONCURRENT_REQUESTS: [data, ts]
Record<string, [any, number]>, // FETCH: [data, ts]
ScopedMutator // Mutator
]

Expand Down

0 comments on commit 1bad971

Please sign in to comment.