1
1
import { useCallback , useRef , useDebugValue } from 'react'
2
2
import defaultConfig from './utils/config'
3
- import { wrapCache , SWRGlobalState } from './utils/cache'
3
+ import { wrapCache , SWRGlobalState , GlobalState } from './utils/cache'
4
4
import { IS_SERVER , rAF , useIsomorphicLayoutEffect } from './utils/env'
5
5
import { serialize } from './utils/serialize'
6
6
import { isUndefined , UNDEFINED } from './utils/helper'
@@ -21,11 +21,10 @@ import {
21
21
Cache ,
22
22
ScopedMutator ,
23
23
SWRHook ,
24
+ Revalidator ,
24
25
ProviderOptions
25
26
} from './types'
26
27
27
- type Revalidator = ( ...args : any [ ] ) => void
28
-
29
28
// Generate strictly increasing timestamps.
30
29
let __timestamp = 0
31
30
@@ -37,7 +36,7 @@ const broadcastState: Broadcaster = (
37
36
isValidating ,
38
37
shouldRevalidate = false
39
38
) => {
40
- const [ , , CACHE_REVALIDATORS ] = SWRGlobalState . get ( cache ) !
39
+ const [ , , CACHE_REVALIDATORS ] = SWRGlobalState . get ( cache ) as GlobalState
41
40
const updaters = CACHE_REVALIDATORS [ key ]
42
41
const promises = [ ]
43
42
if ( updaters ) {
@@ -59,7 +58,9 @@ async function internalMutate<Data = any>(
59
58
const [ key , , keyErr ] = serialize ( _key )
60
59
if ( ! key ) return UNDEFINED
61
60
62
- const [ , , , MUTATION_TS , MUTATION_END_TS ] = SWRGlobalState . get ( cache ) !
61
+ const [ , , , MUTATION_TS , MUTATION_END_TS ] = SWRGlobalState . get (
62
+ cache
63
+ ) as GlobalState
63
64
64
65
// if there is no new data to update, let's just revalidate the key
65
66
if ( isUndefined ( _data ) ) {
@@ -138,9 +139,9 @@ async function internalMutate<Data = any>(
138
139
// Add a callback function to a list of keyed revalidation functions and returns
139
140
// the unregister function.
140
141
const addRevalidator = (
141
- revalidators : Record < string , Revalidator [ ] > ,
142
+ revalidators : Record < string , ( Revalidator | Updater < any > ) [ ] > ,
142
143
key : string ,
143
- callback : Revalidator
144
+ callback : Revalidator | Updater < any >
144
145
) => {
145
146
if ( ! revalidators [ key ] ) {
146
147
revalidators [ key ] = [ callback ]
@@ -185,7 +186,7 @@ export function useSWRHandler<Data = any, Error = any>(
185
186
MUTATION_END_TS ,
186
187
CONCURRENT_PROMISES ,
187
188
CONCURRENT_PROMISES_TS
188
- ] = SWRGlobalState . get ( cache ) !
189
+ ] = SWRGlobalState . get ( cache ) as GlobalState
189
190
190
191
// `key` is the identifier of the SWR `data` state.
191
192
// `keyErr` and `keyValidating` are identifiers of `error` and `isValidating`
@@ -498,7 +499,7 @@ export function useSWRHandler<Data = any, Error = any>(
498
499
}
499
500
}
500
501
501
- const onReconnect = ( ) => {
502
+ const onReconnect : Revalidator = ( ) => {
502
503
if ( configRef . current . revalidateOnReconnect && isActive ( ) ) {
503
504
softRevalidate ( )
504
505
}
0 commit comments