diff --git a/src/use-swr.ts b/src/use-swr.ts index f54e78e30..4ddcc8191 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -49,13 +49,8 @@ export const useSWRHandler = ( refreshWhenOffline } = config - const [ - EVENT_REVALIDATORS, - STATE_UPDATERS, - MUTATION_TS, - MUTATION_END_TS, - CONCURRENT_REQUESTS - ] = SWRGlobalState.get(cache) as GlobalState + const [EVENT_REVALIDATORS, STATE_UPDATERS, MUTATION, CONCURRENT_REQUESTS] = + SWRGlobalState.get(cache) as GlobalState // `key` is the identifier of the SWR `data` state, `keyErr` and // `keyValidating` are identifiers of `error` and `isValidating`, @@ -249,14 +244,15 @@ export const useSWRHandler = ( // mutate-------...----------> // we have to ignore the revalidation result (res) because it's no longer fresh. // meanwhile, a new revalidation should be triggered when the mutation ends. + const mutationInfo = MUTATION[key] if ( - !isUndefined(MUTATION_TS[key]) && + !isUndefined(mutationInfo) && // case 1 - (startAt <= MUTATION_TS[key] || + (startAt <= mutationInfo[0] || // case 2 - startAt <= MUTATION_END_TS[key] || + startAt <= mutationInfo[1] || // case 3 - MUTATION_END_TS[key] === 0) + mutationInfo[1] === 0) ) { finishRequestAndUpdateState() if (shouldStartNewRequest) { diff --git a/src/utils/broadcast-state.ts b/src/utils/broadcast-state.ts index 939cca939..9725995ef 100644 --- a/src/utils/broadcast-state.ts +++ b/src/utils/broadcast-state.ts @@ -11,7 +11,7 @@ export const broadcastState: Broadcaster = ( revalidate, broadcast = true ) => { - const [EVENT_REVALIDATORS, STATE_UPDATERS, , , CONCURRENT_REQUESTS] = + const [EVENT_REVALIDATORS, STATE_UPDATERS, , CONCURRENT_REQUESTS] = SWRGlobalState.get(cache) as GlobalState const revalidators = EVENT_REVALIDATORS[key] const updaters = STATE_UPDATERS[key] diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 04d10fbc2..7ff30f4a8 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -48,7 +48,7 @@ export const initCache = ( let unmount = noop // Update the state if it's new, or the provider has been extended. - SWRGlobalState.set(provider, [EVENT_REVALIDATORS, {}, {}, {}, {}, mutate]) + SWRGlobalState.set(provider, [EVENT_REVALIDATORS, {}, {}, {}, mutate]) // This is a new provider, we need to initialize it and setup DOM events // listeners for `focus` and `reconnect` actions. @@ -96,5 +96,5 @@ export const initCache = ( return [provider, mutate, unmount] } - return [provider, (SWRGlobalState.get(provider) as GlobalState)[5]] + return [provider, (SWRGlobalState.get(provider) as GlobalState)[4]] } diff --git a/src/utils/global-state.ts b/src/utils/global-state.ts index a57d4d42d..2116cce99 100644 --- a/src/utils/global-state.ts +++ b/src/utils/global-state.ts @@ -8,8 +8,7 @@ import { export type GlobalState = [ Record, // EVENT_REVALIDATORS Record, // STATE_UPDATERS - Record, // MUTATION_TS - Record, // MUTATION_END_TS + Record, // MUTATION: [ts, end_ts] Record, // CONCURRENT_REQUESTS: [data, ts] ScopedMutator // Mutator ] diff --git a/src/utils/mutate.ts b/src/utils/mutate.ts index 1812e0c51..f77c0293f 100644 --- a/src/utils/mutate.ts +++ b/src/utils/mutate.ts @@ -31,9 +31,7 @@ export const internalMutate = async ( const [key, , keyErr] = serialize(_key) if (!key) return - const [, , MUTATION_TS, MUTATION_END_TS] = SWRGlobalState.get( - cache - ) as GlobalState + const [, , MUTATION] = SWRGlobalState.get(cache) as GlobalState // If there is no new data provided, revalidate the key with current state. if (args.length < 3) { @@ -53,8 +51,8 @@ export const internalMutate = async ( let error: unknown // Update global timestamps. - const beforeMutationTs = (MUTATION_TS[key] = getTimestamp()) - MUTATION_END_TS[key] = 0 + const beforeMutationTs = getTimestamp() + MUTATION[key] = [beforeMutationTs, 0] const hasOptimisticData = !isUndefined(optimisticData) const rollbackData = cache.get(key) @@ -85,7 +83,7 @@ export const internalMutate = async ( // Check if other mutations have occurred since we've started this mutation. // If there's a race we don't update cache or broadcast the change, // just return the data. - if (beforeMutationTs !== MUTATION_TS[key]) { + if (beforeMutationTs !== MUTATION[key][0]) { if (error) throw error return data } else if (error && hasOptimisticData && rollbackOnError) { @@ -106,7 +104,7 @@ export const internalMutate = async ( } // Reset the timestamp to mark the mutation has ended. - MUTATION_END_TS[key] = getTimestamp() + MUTATION[key][1] = getTimestamp() // Update existing SWR Hooks' internal states: const res = await broadcastState(