Skip to content

Commit f6b20ef

Browse files
committed
rename api
1 parent 942c739 commit f6b20ef

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

infinite/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import useSWR, {
1111
Middleware
1212
} from 'swr'
1313
import { useIsomorphicLayoutEffect } from '../src/utils/env'
14-
import { serialize as serializeKey } from '../src/utils/serialize'
14+
import { serialize } from '../src/utils/serialize'
1515
import { isUndefined, UNDEFINED } from '../src/utils/helper'
1616
import { withMiddleware } from '../src/utils/with-middleware'
1717
import { SWRInfiniteConfiguration, SWRInfiniteResponse } from './types'
1818

1919
const INFINITE_PREFIX = '$inf$'
2020

2121
const getFirstPageKey = (getKey: KeyLoader<any>) => {
22-
return serializeKey(getKey ? getKey(0, null) : null)[0]
22+
return serialize(getKey ? getKey(0, null) : null)[0]
2323
}
2424

25-
export const serialize = (getKey: KeyLoader<any>) => {
25+
export const unstable_serialize = (getKey: KeyLoader<any>) => {
2626
return INFINITE_PREFIX + getFirstPageKey(getKey)
2727
}
2828

@@ -110,7 +110,7 @@ export const infinite = ((<Data, Error>(useSWRNext: SWRHook) => (
110110

111111
let previousPageData = null
112112
for (let i = 0; i < pageSize; ++i) {
113-
const [pageKey, pageArgs] = serializeKey(
113+
const [pageKey, pageArgs] = serialize(
114114
getKey ? getKey(i, previousPageData) : null
115115
)
116116

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Core APIs
2-
export { SWRConfig, mutate, createCache } from './use-swr'
3-
export { serialize } from './utils/serialize'
2+
export { SWRConfig, mutate, createCache, unstable_serialize } from './use-swr'
43

54
// useSWR
65
import useSWR from './use-swr'

src/use-swr.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,6 @@ export const createCache = <Data = any>(
650650
}
651651
}
652652

653+
export const unstable_serialize = (key: Key) => serialize(key)[0]
654+
653655
export default withArgs<SWRHook>(useSWRHandler)

test/use-swr-infinite.test.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from 'react'
22
import { render, fireEvent, act, screen } from '@testing-library/react'
33
import { mutate, createCache, SWRConfig } from 'swr'
4-
import useSWRInfinite, { serialize } from 'swr/infinite'
4+
import useSWRInfinite, { unstable_serialize } from 'swr/infinite'
55
import { sleep, createKey, createResponse } from './utils'
66

77
describe('useSWRInfinite', () => {
@@ -581,7 +581,7 @@ describe('useSWRInfinite', () => {
581581
await screen.findByText('data:')
582582
})
583583

584-
it('should mutate a cache with `serialize`', async () => {
584+
it('should mutate a cache with `unstable_serialize`', async () => {
585585
let count = 0
586586
function Page() {
587587
const { data } = useSWRInfinite<string, string>(
@@ -596,20 +596,22 @@ describe('useSWRInfinite', () => {
596596

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

599-
await act(() => mutate(serialize(index => `page-test-12-${index}`)))
599+
await act(() =>
600+
mutate(unstable_serialize(index => `page-test-12-${index}`))
601+
)
600602
await screen.findByText('data:page-test-12-0:2')
601603

602604
await act(() =>
603605
mutate(
604-
serialize(index => `page-test-12-${index}`),
606+
unstable_serialize(index => `page-test-12-${index}`),
605607
'local-mutation',
606608
false
607609
)
608610
)
609611
await screen.findByText('data:local-mutation')
610612
})
611613

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

631633
await act(() =>
632634
mutate(
633-
serialize(getKey),
635+
unstable_serialize(getKey),
634636
data => data.map(value => `(edited)${value}`),
635637
false
636638
)
637639
)
638640
await screen.findByText('data:(edited)page 0, (edited)page 1,')
639641
})
640642

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

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

662-
await act(() => mutateCustomCache(serialize(() => key)))
664+
await act(() => mutateCustomCache(unstable_serialize(() => key)))
663665
await screen.findByText('data:response data')
664666

665667
await act(() =>
666-
mutateCustomCache(serialize(() => key), 'local-mutation', false)
668+
mutateCustomCache(unstable_serialize(() => key), 'local-mutation', false)
667669
)
668670
await screen.findByText('data:local-mutation')
669671
})

0 commit comments

Comments
 (0)