Skip to content

Commit 18f9295

Browse files
shudingnevilm-lt
authored andcommitted
merge mutation states (vercel#1748)
1 parent 7096084 commit 18f9295

File tree

5 files changed

+8
-14
lines changed

5 files changed

+8
-14
lines changed

src/use-swr.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@ export const useSWRHandler = <Data = any, Error = any>(
4949
refreshWhenOffline
5050
} = config
5151

52-
const [
53-
EVENT_REVALIDATORS,
54-
STATE_UPDATERS,
55-
MUTATION_TS,
56-
MUTATION_END_TS,
57-
CONCURRENT_REQUESTS
58-
] = SWRGlobalState.get(cache) as GlobalState
52+
const [EVENT_REVALIDATORS, STATE_UPDATERS, MUTATION, CONCURRENT_REQUESTS] =
53+
SWRGlobalState.get(cache) as GlobalState
5954

6055
// `key` is the identifier of the SWR `data` state, `keyErr` and
6156
// `keyValidating` are identifiers of `error` and `isValidating`,

src/utils/broadcast-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const broadcastState: Broadcaster = (
1111
revalidate,
1212
broadcast = true
1313
) => {
14-
const [EVENT_REVALIDATORS, STATE_UPDATERS, , , CONCURRENT_REQUESTS] =
14+
const [EVENT_REVALIDATORS, STATE_UPDATERS, , CONCURRENT_REQUESTS] =
1515
SWRGlobalState.get(cache) as GlobalState
1616
const revalidators = EVENT_REVALIDATORS[key]
1717
const updaters = STATE_UPDATERS[key]

src/utils/cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const initCache = <Data = any>(
4848
let unmount = noop
4949

5050
// Update the state if it's new, or the provider has been extended.
51-
SWRGlobalState.set(provider, [EVENT_REVALIDATORS, {}, {}, {}, {}, mutate])
51+
SWRGlobalState.set(provider, [EVENT_REVALIDATORS, {}, {}, {}, mutate])
5252

5353
// This is a new provider, we need to initialize it and setup DOM events
5454
// listeners for `focus` and `reconnect` actions.
@@ -96,5 +96,5 @@ export const initCache = <Data = any>(
9696
return [provider, mutate, unmount]
9797
}
9898

99-
return [provider, (SWRGlobalState.get(provider) as GlobalState)[5]]
99+
return [provider, (SWRGlobalState.get(provider) as GlobalState)[4]]
100100
}

src/utils/global-state.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
export type GlobalState = [
99
Record<string, RevalidateCallback[]>, // EVENT_REVALIDATORS
1010
Record<string, StateUpdateCallback[]>, // STATE_UPDATERS
11-
Record<string, number>, // MUTATION_TS
12-
Record<string, number>, // MUTATION_END_TS
11+
Record<string, [number, number]>, // MUTATION: [ts, end_ts]
1312
Record<string, [any, number]>, // CONCURRENT_REQUESTS: [data, ts]
1413
ScopedMutator // Mutator
1514
]

src/utils/mutate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export const internalMutate = async <Data>(
5050
let error: unknown
5151

5252
// Update global timestamps.
53-
const beforeMutationTs = (MUTATION_TS[key] = getTimestamp())
54-
MUTATION_END_TS[key] = 0
53+
const beforeMutationTs = getTimestamp()
54+
MUTATION[key] = [beforeMutationTs, 0]
5555
const hasOptimisticData = !isUndefined(optimisticData)
5656
const rollbackData = cache.get(key)
5757

0 commit comments

Comments
 (0)