Skip to content

Export unstable_serialize from SWR #1337

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

Merged
merged 4 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const getFirstPageKey = (getKey: KeyLoader<any>) => {
return serialize(getKey ? getKey(0, null) : null)[0]
}

export const getInfiniteKey = (getKey: KeyLoader<any>) => {
export const unstable_serialize = (getKey: KeyLoader<any>) => {
return INFINITE_PREFIX + getFirstPageKey(getKey)
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Core APIs
export { SWRConfig, mutate, createCache } from './use-swr'
export { SWRConfig, mutate, createCache, unstable_serialize } from './use-swr'

// useSWR
import useSWR from './use-swr'
Expand Down
2 changes: 2 additions & 0 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,6 @@ export const createCache = <Data = any>(
}
}

export const unstable_serialize = (key: Key) => serialize(key)[0]

export default withArgs<SWRHook>(useSWRHandler)
20 changes: 11 additions & 9 deletions test/use-swr-infinite.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react'
import { render, fireEvent, act, screen } from '@testing-library/react'
import { mutate, createCache, SWRConfig } from 'swr'
import useSWRInfinite, { getInfiniteKey } from 'swr/infinite'
import useSWRInfinite, { unstable_serialize } from 'swr/infinite'
import { sleep, createKey, createResponse } from './utils'

describe('useSWRInfinite', () => {
Expand Down Expand Up @@ -581,7 +581,7 @@ describe('useSWRInfinite', () => {
await screen.findByText('data:')
})

it('should mutate a cache with getInfiniteKey', async () => {
it('should mutate a cache with `unstable_serialize`', async () => {
let count = 0
function Page() {
const { data } = useSWRInfinite<string, string>(
Expand All @@ -596,20 +596,22 @@ describe('useSWRInfinite', () => {

await screen.findByText('data:page-test-12-0:1')

await act(() => mutate(getInfiniteKey(index => `page-test-12-${index}`)))
await act(() =>
mutate(unstable_serialize(index => `page-test-12-${index}`))
)
await screen.findByText('data:page-test-12-0:2')

await act(() =>
mutate(
getInfiniteKey(index => `page-test-12-${index}`),
unstable_serialize(index => `page-test-12-${index}`),
'local-mutation',
false
)
)
await screen.findByText('data:local-mutation')
})

it('should mutate a cache with getInfiniteKey based on a current data', async () => {
it('should mutate a cache with `unstable_serialize` based on a current data', async () => {
const getKey = index => [`pagetest-13`, index]
function Comp() {
const { data, size, setSize } = useSWRInfinite<string, string>(
Expand All @@ -630,15 +632,15 @@ describe('useSWRInfinite', () => {

await act(() =>
mutate(
getInfiniteKey(getKey),
unstable_serialize(getKey),
data => data.map(value => `(edited)${value}`),
false
)
)
await screen.findByText('data:(edited)page 0, (edited)page 1,')
})

it('should be able to use getInfiniteKey with a custom cache', async () => {
it('should be able to use `unstable_serialize` with a custom cache', async () => {
const key = 'page-test-14;'
const customCache1 = new Map([[key, 'initial-cache']])
const { cache, mutate: mutateCustomCache } = createCache(customCache1)
Expand All @@ -659,11 +661,11 @@ describe('useSWRInfinite', () => {

await screen.findByText('data:initial-cache')

await act(() => mutateCustomCache(getInfiniteKey(() => key)))
await act(() => mutateCustomCache(unstable_serialize(() => key)))
await screen.findByText('data:response data')

await act(() =>
mutateCustomCache(getInfiniteKey(() => key), 'local-mutation', false)
mutateCustomCache(unstable_serialize(() => key), 'local-mutation', false)
)
await screen.findByText('data:local-mutation')
})
Expand Down