From 18f929587bad5469f4783a0444f97c786c05ac25 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 28 Dec 2021 01:19:27 +0100 Subject: [PATCH] merge mutation states (#1748) --- src/use-swr.ts | 9 ++------- src/utils/broadcast-state.ts | 2 +- src/utils/cache.ts | 4 ++-- src/utils/global-state.ts | 3 +-- src/utils/mutate.ts | 4 ++-- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/use-swr.ts b/src/use-swr.ts index a4c4421a1..52e09bc3a 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`, 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 74be9e8ee..fa62069fa 100644 --- a/src/utils/mutate.ts +++ b/src/utils/mutate.ts @@ -50,8 +50,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)