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

types: infer provider type as cache type #1506

Closed
wants to merge 5 commits into from

Conversation

huozhi
Copy link
Member

@huozhi huozhi commented Sep 28, 2021

  • make Cache a type that extends from required provider interface
  • make useSWRConfig generic with provider type useSWRConfig<Provider = Cache>
  • add typing test

Closes #1502
Closes #1505

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 28, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit bdddf3a:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

get(key: Key): Data | null | undefined
set(key: Key, value: Data): void
delete(key: Key): void
}

export type DefaultProvider = Map<string | null, any>
Copy link
Member

Choose a reason for hiding this comment

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

Why string | null instead of just string?

Copy link
Member Author

Choose a reason for hiding this comment

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

because swr/infinite has some case that cache key is string | null

like

  const resolvePageSize = useCallback((): number => {
    const cachedPageSize = cache.get(pageSizeCacheKey)
    return isUndefined(cachedPageSize) ? initialSize : cachedPageSize

Copy link
Member

Choose a reason for hiding this comment

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

That feels like a bug (facepalm)

@shuding
Copy link
Member

shuding commented Oct 1, 2021

I wonder if there's a way to automatically infer the type, without requiring a generic type to be passed.

}> = ({ children, value }) => {
type SWRConfigValue = SWRConfiguration &
Partial<ProviderConfiguration> & {
provider?: CacheProvider<any>
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not look correct to me.

export type Cache<T = DefaultProvider, Data = any> = T extends ICache<Data>
  ? T
  : never

export type CacheProvider<Data = any> = (cache: Cache<Data>) => Cache<Data>

CacheProvider<any> = (cache: Cache<any>) => Cache<any> = (cache: any) => any

We need provider to return a Cache here

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't get what do you mean here. Can you rephrase it more specificly?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Previous

type Provider: (cache: Readonly<Cache>) => Cache
const cache = new Map()
<SWRConfig value={{ provider: () => cache }}>{children}</SWRConfig>
// This will throw type error since { a: foo } does not satisfy the requirement
<SWRConfig value={{ provider: () => {a: foo } }}>{children}</SWRConfig>

Current

type Provider = CacheProvider<any> = (_: any) => any
const cache = new Map()
<SWRConfig value={{ provider: () => cache }}>{children}</SWRConfig>
// This will not throw type error any more
<SWRConfig value={{ provider: () => {a: foo } }}>{children}</SWRConfig>

@promer94
Copy link
Collaborator

promer94 commented Oct 3, 2021

I think the actual problem here is that we can't safely type the cache without runtime checking.

const { cache } = useSWRConfig()

The cache which is returned from the hook probably does not have the clear interface.

Currently if users want to extend the cache interface. They have to write a type assertion.

// CustomCache.tsx
import type { Cache } from "swr"
import { SWRConfig, useSWRConfig } from "swr"
type MyCustomCache = Map<string, any>

const customCache: MyCustomCache = new Map()

const CustomCacheProvider = ({ children }) => {
  return <SWRConfig value={{ provider: () => cache }}>{children}</SWRConfig>
}
// This is just a example. The actual runtime check might be more complex
function checkCache(cache: Cache): cache is MyCustomCache {
  return cache === c
}

const useCustomCache = () => {
  const { cache } = useSWRConfig()
  if (checkCache(cache)) return cache
  throw new Error("customCache can only be used within CustomCacheProvider")
}
// App.tsx
const ClearButton = () => {
  const cache = useCustomCache()
  return (
    <button
      onClick={() => {
        // type safe and runtime safe
        cache.clear()
      }}
    ></button>
  )
}

const App = () => {
  return (
    <>
      <CustomCacheProvider>
        <ClearButton />
      </CustomCacheProvider>
      {/* <ClearButton/>*/}
    </>
  )
}

We need to find a batter way to help user to extend current cache interface (runtime checking is still required) and provider batter type for default cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: cache object doesn't have the method clear
3 participants