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

fix: should serialize subscription fn key #2711

Merged
merged 2 commits into from
Jul 13, 2023
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
4 changes: 2 additions & 2 deletions _internal/src/utils/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { stableHash } from './hash'
import { isFunction } from './shared'

import type { Key } from '../types'
import type { Key, Arguments } from '../types'

export const serialize = (key: Key): [string, Key] => {
export const serialize = (key: Key): [string, Arguments] => {
if (isFunction(key)) {
try {
key = key()
Expand Down
5 changes: 2 additions & 3 deletions subscription/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export const subscription = (<Data = any, Error = any>(useSWRNext: SWRHook) =>
subscribe: SWRSubscription<any, Data, Error>,
config: SWRConfiguration & typeof SWRConfig.defaultValue
): SWRSubscriptionResponse<Data, Error> => {
const [key] = serialize(_key)
const originKey = _key
const [key, args] = serialize(_key)

// Prefix the key to avoid conflicts with other SWR resources.
const subscriptionKey = key ? SUBSCRIPTION_PREFIX + key : undefined
Expand Down Expand Up @@ -68,7 +67,7 @@ export const subscription = (<Data = any, Error = any>(useSWRNext: SWRHook) =>
subscriptions.set(subscriptionKey, refCount + 1)

if (!refCount) {
const dispose = subscribe(originKey, { next })
const dispose = subscribe(args, { next })
if (typeof dispose !== 'function') {
throw new Error(
'The `subscribe` function must return a function to unsubscribe.'
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-subscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('useSWRSubscription', () => {
}

function Page() {
const { data, error } = useSWRSubscription([swrKey], subscribe, {
const { data, error } = useSWRSubscription(() => [swrKey], subscribe, {
fallbackData: 'fallback'
})
return (
Expand Down