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

enhance: add $ prefix for non-major keys #1174

Merged
merged 7 commits into from
Jun 30, 2021

Conversation

huozhi
Copy link
Member

@huozhi huozhi commented May 21, 2021

specialize keys for error and isValidating, and some non major keys in swr infinite, to identify them easier later if developers want to play tricks with keys or filter them from cache provider.

Resolves #242

e.g.

access all keys

const provider = new Map()
const { cache, mutate } = createCache(provider)

// ...
const serializedKeys = Array.from(provider.keys()).filter(k => !k.startsWith('$'))

access pending requests

const validatingKeys = serializedKeys.filter(key => key.startsWith('$req$'))

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 21, 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 4084724:

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

Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

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

Let's also add it to inf, len and ctx in swr-infinite 👍

@huozhi
Copy link
Member Author

huozhi commented May 21, 2021

curious about the reason of designing page key as firstPageKey ? ['$inf', firstPageKey] : null, then the array key will always be serialized into arg@blablabla which losing the $ prefix

@huozhi huozhi changed the title enhance: add $ prefix for err and isValidating keys enhance: add $ prefix for non-major keys May 21, 2021
@shuding
Copy link
Member

shuding commented May 21, 2021

Oh that's a good point, since it already has $args.

curious about the reason of designing page key as firstPageKey ? ['$inf', firstPageKey] : null

That's because if we directly use firstPageKey as the key (without adding inf), the infinite page hook will share cache with the single page hook if it exists (useSWR(firstPageKey)).

src/use-swr-infinite.ts Outdated Show resolved Hide resolved
shuding and others added 2 commits May 27, 2021 00:30
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
@shuding
Copy link
Member

shuding commented Jun 30, 2021

Looks good to me, let's also change existing @ concatenation symbols to $ so it looks better :)

Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

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

make symbol rich

Thanks! 🤣

@shuding shuding merged commit e27311c into vercel:master Jun 30, 2021
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.

How to check if there are no pending requests?
2 participants