Skip to content

Commit

Permalink
merge mutation states (vercel#1748)
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 7096084 commit 18f9295
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 14 deletions.
9 changes: 2 additions & 7 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ export const useSWRHandler = <Data = any, Error = any>(
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`,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/broadcast-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const initCache = <Data = any>(
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.
Expand Down Expand Up @@ -96,5 +96,5 @@ export const initCache = <Data = any>(
return [provider, mutate, unmount]
}

return [provider, (SWRGlobalState.get(provider) as GlobalState)[5]]
return [provider, (SWRGlobalState.get(provider) as GlobalState)[4]]
}
3 changes: 1 addition & 2 deletions src/utils/global-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
export type GlobalState = [
Record<string, RevalidateCallback[]>, // EVENT_REVALIDATORS
Record<string, StateUpdateCallback[]>, // STATE_UPDATERS
Record<string, number>, // MUTATION_TS
Record<string, number>, // MUTATION_END_TS
Record<string, [number, number]>, // MUTATION: [ts, end_ts]
Record<string, [any, number]>, // CONCURRENT_REQUESTS: [data, ts]
ScopedMutator // Mutator
]
Expand Down
4 changes: 2 additions & 2 deletions src/utils/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export const internalMutate = async <Data>(
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)

Expand Down

0 comments on commit 18f9295

Please sign in to comment.