Skip to content

Commit 690043a

Browse files
committed
use global state type guard and Revalidator type
1 parent c4e7864 commit 690043a

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export interface RevalidatorOptions {
169169

170170
export type Revalidator = (
171171
revalidateOpts?: RevalidatorOptions
172-
) => Promise<boolean>
172+
) => Promise<boolean> | void
173173

174174
export interface Cache<Data = any> {
175175
get(key: Key): Data | null | undefined

src/use-swr.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback, useRef, useDebugValue } from 'react'
22
import defaultConfig from './utils/config'
3-
import { wrapCache, SWRGlobalState } from './utils/cache'
3+
import { wrapCache, SWRGlobalState, GlobalState } from './utils/cache'
44
import { IS_SERVER, rAF, useIsomorphicLayoutEffect } from './utils/env'
55
import { serialize } from './utils/serialize'
66
import { isUndefined, UNDEFINED } from './utils/helper'
@@ -21,11 +21,10 @@ import {
2121
Cache,
2222
ScopedMutator,
2323
SWRHook,
24+
Revalidator,
2425
ProviderOptions
2526
} from './types'
2627

27-
type Revalidator = (...args: any[]) => void
28-
2928
// Generate strictly increasing timestamps.
3029
let __timestamp = 0
3130

@@ -37,7 +36,7 @@ const broadcastState: Broadcaster = (
3736
isValidating,
3837
shouldRevalidate = false
3938
) => {
40-
const [, , CACHE_REVALIDATORS] = SWRGlobalState.get(cache)!
39+
const [, , CACHE_REVALIDATORS] = SWRGlobalState.get(cache) as GlobalState
4140
const updaters = CACHE_REVALIDATORS[key]
4241
const promises = []
4342
if (updaters) {
@@ -59,7 +58,9 @@ async function internalMutate<Data = any>(
5958
const [key, , keyErr] = serialize(_key)
6059
if (!key) return UNDEFINED
6160

62-
const [, , , MUTATION_TS, MUTATION_END_TS] = SWRGlobalState.get(cache)!
61+
const [, , , MUTATION_TS, MUTATION_END_TS] = SWRGlobalState.get(
62+
cache
63+
) as GlobalState
6364

6465
// if there is no new data to update, let's just revalidate the key
6566
if (isUndefined(_data)) {
@@ -138,9 +139,9 @@ async function internalMutate<Data = any>(
138139
// Add a callback function to a list of keyed revalidation functions and returns
139140
// the unregister function.
140141
const addRevalidator = (
141-
revalidators: Record<string, Revalidator[]>,
142+
revalidators: Record<string, (Revalidator | Updater<any>)[]>,
142143
key: string,
143-
callback: Revalidator
144+
callback: Revalidator | Updater<any>
144145
) => {
145146
if (!revalidators[key]) {
146147
revalidators[key] = [callback]
@@ -185,7 +186,7 @@ export function useSWRHandler<Data = any, Error = any>(
185186
MUTATION_END_TS,
186187
CONCURRENT_PROMISES,
187188
CONCURRENT_PROMISES_TS
188-
] = SWRGlobalState.get(cache)!
189+
] = SWRGlobalState.get(cache) as GlobalState
189190

190191
// `key` is the identifier of the SWR `data` state.
191192
// `keyErr` and `keyValidating` are identifiers of `error` and `isValidating`
@@ -498,7 +499,7 @@ export function useSWRHandler<Data = any, Error = any>(
498499
}
499500
}
500501

501-
const onReconnect = () => {
502+
const onReconnect: Revalidator = () => {
502503
if (configRef.current.revalidateOnReconnect && isActive()) {
503504
softRevalidate()
504505
}

src/utils/cache.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import { UNDEFINED } from './helper'
44

55
import { Cache, Revalidator, Updater, ProviderOptions } from '../types'
66

7+
export type GlobalState = [
8+
Record<string, Revalidator[]>, // FOCUS_REVALIDATORS
9+
Record<string, Revalidator[]>, // RECONNECT_REVALIDATORS
10+
Record<string, (Revalidator | Updater<any>)[]>, // CACHE_REVALIDATORS
11+
Record<string, number>, // MUTATION_TS
12+
Record<string, number>, // MUTATION_END_TS
13+
Record<string, any>, // CONCURRENT_PROMISES
14+
Record<string, number> // CONCURRENT_PROMISES_TS
15+
]
16+
717
// Global state used to deduplicate requests and store listeners
8-
export const SWRGlobalState = new WeakMap<
9-
Cache,
10-
[
11-
Record<string, Revalidator[]>, // FOCUS_REVALIDATORS
12-
Record<string, Revalidator[]>, // RECONNECT_REVALIDATORS
13-
Record<string, Updater[]>, // CACHE_REVALIDATORS
14-
Record<string, number>, // MUTATION_TS
15-
Record<string, number>, // MUTATION_END_TS
16-
Record<string, any>, // CONCURRENT_PROMISES
17-
Record<string, number> // CONCURRENT_PROMISES_TS
18-
]
19-
>()
18+
export const SWRGlobalState = new WeakMap<Cache, GlobalState>()
2019

2120
function revalidateAllKeys(revalidators: Record<string, Revalidator[]>) {
2221
for (const key in revalidators) {
@@ -27,7 +26,7 @@ function revalidateAllKeys(revalidators: Record<string, Revalidator[]>) {
2726
function setupGlobalEvents(cache: Cache, options: ProviderOptions) {
2827
const [FOCUS_REVALIDATORS, RECONNECT_REVALIDATORS] = SWRGlobalState.get(
2928
cache
30-
)!
29+
) as GlobalState
3130
options.setupOnFocus(revalidateAllKeys.bind(UNDEFINED, FOCUS_REVALIDATORS))
3231
options.setupOnReconnect(
3332
revalidateAllKeys.bind(UNDEFINED, RECONNECT_REVALIDATORS)

0 commit comments

Comments
 (0)