Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Merge mutation states #1748

Merged
merged 1 commit into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 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 Expand Up @@ -249,14 +244,15 @@ export const useSWRHandler = <Data = any, Error = any>(
// 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) {
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
12 changes: 5 additions & 7 deletions src/utils/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export const internalMutate = async <Data>(
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) {
Expand All @@ -53,8 +51,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 Expand Up @@ -85,7 +83,7 @@ export const internalMutate = async <Data>(
// 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) {
Expand All @@ -106,7 +104,7 @@ export const internalMutate = async <Data>(
}

// 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(
Expand Down