Skip to content

Commit

Permalink
Fix cache types (#1961)
Browse files Browse the repository at this point in the history
* Fix cache types

* Guard against undefined originalData

* Change useSwrConfig

* populateCache: undefined for currentData
  • Loading branch information
chibicode committed May 13, 2022
1 parent a990966 commit caa61a7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
16 changes: 7 additions & 9 deletions src/types.ts
Expand Up @@ -144,7 +144,7 @@ export type MutatorCallback<Data = any> = (

export type MutatorOptions<Data = any> = {
revalidate?: boolean
populateCache?: boolean | ((result: any, currentData: Data) => Data)
populateCache?: boolean | ((result: any, currentData?: Data) => Data)
optimisticData?: Data | ((currentData?: Data) => Data)
rollbackOnError?: boolean
}
Expand Down Expand Up @@ -255,14 +255,12 @@ export type RevalidateCallback = <K extends RevalidateEvent>(
type: K
) => RevalidateCallbackReturnType[K]

export type StateUpdateCallback<Data = any, Error = any> = (state: {
data?: Data
error?: Error
isValidating?: boolean
}) => void
export type StateUpdateCallback<Data = any, Error = any> = (
state: State<Data, Error>
) => void

export interface Cache<Data = any> {
get(key: Key): Data | null | undefined
set(key: Key, value: Data): void
export interface Cache<Data = any, Error = any> {
get(key: Key): State<Data, Error> | undefined
set(key: Key, value: State<Data, Error>): void
delete(key: Key): void
}
5 changes: 3 additions & 2 deletions src/use-swr.ts
Expand Up @@ -178,7 +178,7 @@ export const useSWRHandler = <Data = any, Error = any>(
// new request should be initiated.
const shouldStartNewRequest = !FETCH[key] || !opts.dedupe

/*
/*
For React 17
Do unmount check for calls:
If key has changed during the revalidation, or the component has been
Expand Down Expand Up @@ -442,7 +442,8 @@ export const useSWRHandler = <Data = any, Error = any>(
mergeObjects(
{
error: state.error,
isValidating: state.isValidating
isValidating: state.isValidating,
isLoading: state.isLoading
},
// Since `setState` only shallowly compares states, we do a deep
// comparison here.
Expand Down
9 changes: 3 additions & 6 deletions src/utils/cache.ts
Expand Up @@ -8,6 +8,7 @@ import * as revalidateEvents from '../constants'
import {
Key,
Cache,
State,
ScopedMutator,
RevalidateEvent,
RevalidateCallback,
Expand Down Expand Up @@ -106,13 +107,9 @@ export const createCacheHelper = <Data = any, ExtendedInfo = {}>(
) =>
[
// Getter
() => cache.get(key) || {},
() => (cache.get(key) || {}) as State<Data, any> & Partial<ExtendedInfo>,
// Setter
(
info: Partial<
{ data: Data; error: any; isValidating: boolean } | ExtendedInfo
>
) => {
(info: Partial<State<Data, any> | ExtendedInfo>) => {
cache.set(key, mergeObjects(cache.get(key), info))
}
] as const
2 changes: 1 addition & 1 deletion src/utils/use-swr-config.ts
Expand Up @@ -5,7 +5,7 @@ import { mergeObjects } from './helper'
import { FullConfiguration, Cache } from '../types'

export const useSWRConfig = <
T extends Cache = Map<string, any>
T extends Cache = Cache
>(): FullConfiguration<T> => {
return mergeObjects(defaultConfig, useContext(SWRConfigContext))
}
3 changes: 1 addition & 2 deletions test/type/config.ts
Expand Up @@ -6,7 +6,6 @@ interface CustomCache<Data = any> extends Cache<Data> {
}

export function useTestCache() {
expectType<Map<string, any>>(useSWRConfig().cache)
expectType<Map<string, number>>(useSWRConfig<Map<string, number>>().cache)
expectType<Cache>(useSWRConfig().cache)
expectType<CustomCache>(useSWRConfig<CustomCache>().cache)
}

0 comments on commit caa61a7

Please sign in to comment.