vercel / swr

React Hooks for Data Fetching
https://swr.vercel.app
MIT License
30.35k stars 1.21k forks source link

feat: add clear method to cache object type #2950

Open shhonarmandi opened 5 months ago

shhonarmandi commented 5 months ago

This commit updates the type definition of the Cache object to align with the native JavaScript Map data structure. This offers users access to clear method available on Map.

cache.clear(): Removes all key-value pairs from the cache (useful scenarios include user logout).

Relevant issue: https://github.com/vercel/swr/issues/2877

codesandbox-ci[bot] commented 5 months ago

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.

koba04 commented 5 months ago

Thank you for your PR! As you mentioned, although the default Cache object is implemented with Map internally, it doesn't mean the Cache must be Map. We intentionally restrict the Cache interface to only APIs SWR uses internally so that users can implement their own Cache store without relying on Map.

Let me consider the clearing cache feature separately.

shhonarmandi commented 5 months ago

@koba04

Thank you for your feedback.

I updated the pull request and removed unnecessary methods and properties as you mentioned.