Skip to content
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

Merged
merged 4 commits into from May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/types.ts
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>
Copy link
Member Author

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 add isLoading which was missing in the original type definition.

swr/src/types.ts

Lines 167 to 172 in 09d80c5

export type State<Data, Error> = {
data?: Data
error?: Error
isValidating?: boolean
isLoading?: boolean
}

) => void

export interface Cache<Data = any> {
get(key: Key): Data | null | undefined
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed null as it seemed like it's no longer being used.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From release notes:

So you will have to do the following change to your code, get:

- cache.get(key)
+ cache.get(key)?.data

And set:

- cache.set(key, data)
+ cache.set(key, { ...cache.get(key), data })

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isLoading was not being copied over originally, but I believe it should be.

},
// 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>,
Copy link
Member Author

@chibicode chibicode May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was necessary to make ExtendedInfo work:

swr/infinite/index.ts

Lines 67 to 77 in 09d80c5

const [get, set] = createCacheHelper<
Data,
{
// We use cache to pass extra info (context) to fetcher so it can be globally
// shared. The key of the context data is based on the first page key.
$ctx: [boolean] | [boolean, Data[] | undefined]
// Page size is also cached to share the page data between hooks with the
// same key.
$len: number
}
>(cache, infiniteKey)

I couldn't figure out a way to do it without type assertion.


I did notice that this line below had a type error (Type 'Data' is not assignable to type 'Data[]'), but I couldn't figure out how to resolve this. This line already had a different type error even before this PR.

!config.compare(originalData[i], pageData))

// 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