New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix cache types #1961
Fix cache types #1961
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
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 | ||
Comment on lines
+262
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From release notes:
|
||
delete(key: Key): void | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -442,7 +442,8 @@ export const useSWRHandler = <Data = any, Error = any>( | |
mergeObjects( | ||
{ | ||
error: state.error, | ||
isValidating: state.isValidating | ||
isValidating: state.isValidating, | ||
isLoading: state.isLoading | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
// Since `setState` only shallowly compares states, we do a deep | ||
// comparison here. | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ import * as revalidateEvents from '../constants' | |||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||
Key, | ||||||||||||||||||||||||||
Cache, | ||||||||||||||||||||||||||
State, | ||||||||||||||||||||||||||
ScopedMutator, | ||||||||||||||||||||||||||
RevalidateEvent, | ||||||||||||||||||||||||||
RevalidateCallback, | ||||||||||||||||||||||||||
|
@@ -106,13 +107,9 @@ export const createCacheHelper = <Data = any, ExtendedInfo = {}>( | |||||||||||||||||||||||||
) => | ||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||
// Getter | ||||||||||||||||||||||||||
() => cache.get(key) || {}, | ||||||||||||||||||||||||||
() => (cache.get(key) || {}) as State<Data, any> & Partial<ExtendedInfo>, | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was necessary to make Lines 67 to 77 in 09d80c5
I couldn't figure out a way to do it without type assertion. I did notice that this line below had a type error ( Line 147 in 09d80c5
|
||||||||||||||||||||||||||
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reuses the
State
type, which will addisLoading
which was missing in the original type definition.swr/src/types.ts
Lines 167 to 172 in 09d80c5