Skip to content

Commit 8d2f8a8

Browse files
shudingnevilm-lt
authored andcommitted
merge mutation states (vercel#1748)
1 parent 367cc96 commit 8d2f8a8

File tree

5 files changed

+16
-23
lines changed

5 files changed

+16
-23
lines changed

src/use-swr.ts

Lines changed: 7 additions & 11 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`,
@@ -249,14 +244,15 @@ export const useSWRHandler = <Data = any, Error = any>(
249244
// mutate-------...---------->
250245
// we have to ignore the revalidation result (res) because it's no longer fresh.
251246
// meanwhile, a new revalidation should be triggered when the mutation ends.
247+
const mutationInfo = MUTATION[key]
252248
if (
253-
!isUndefined(MUTATION_TS[key]) &&
249+
!isUndefined(mutationInfo) &&
254250
// case 1
255-
(startAt <= MUTATION_TS[key] ||
251+
(startAt <= mutationInfo[0] ||
256252
// case 2
257-
startAt <= MUTATION_END_TS[key] ||
253+
startAt <= mutationInfo[1] ||
258254
// case 3
259-
MUTATION_END_TS[key] === 0)
255+
mutationInfo[1] === 0)
260256
) {
261257
finishRequestAndUpdateState()
262258
if (shouldStartNewRequest) {

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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export const internalMutate = async <Data>(
3131
const [key, , keyErr] = serialize(_key)
3232
if (!key) return
3333

34-
const [, , MUTATION_TS, MUTATION_END_TS] = SWRGlobalState.get(
35-
cache
36-
) as GlobalState
34+
const [, , MUTATION] = SWRGlobalState.get(cache) as GlobalState
3735

3836
// If there is no new data provided, revalidate the key with current state.
3937
if (args.length < 3) {
@@ -53,8 +51,8 @@ export const internalMutate = async <Data>(
5351
let error: unknown
5452

5553
// Update global timestamps.
56-
const beforeMutationTs = (MUTATION_TS[key] = getTimestamp())
57-
MUTATION_END_TS[key] = 0
54+
const beforeMutationTs = getTimestamp()
55+
MUTATION[key] = [beforeMutationTs, 0]
5856
const hasOptimisticData = !isUndefined(optimisticData)
5957
const rollbackData = cache.get(key)
6058

@@ -85,7 +83,7 @@ export const internalMutate = async <Data>(
8583
// Check if other mutations have occurred since we've started this mutation.
8684
// If there's a race we don't update cache or broadcast the change,
8785
// just return the data.
88-
if (beforeMutationTs !== MUTATION_TS[key]) {
86+
if (beforeMutationTs !== MUTATION[key][0]) {
8987
if (error) throw error
9088
return data
9189
} else if (error && hasOptimisticData && rollbackOnError) {
@@ -106,7 +104,7 @@ export const internalMutate = async <Data>(
106104
}
107105

108106
// Reset the timestamp to mark the mutation has ended.
109-
MUTATION_END_TS[key] = getTimestamp()
107+
MUTATION[key][1] = getTimestamp()
110108

111109
// Update existing SWR Hooks' internal states:
112110
const res = await broadcastState(

0 commit comments

Comments
 (0)