Skip to content

Commit

Permalink
fix: should serialize subscription fn key (#2711)
Browse files Browse the repository at this point in the history
* fix: should serialize subscription fn key

* simpilify code
  • Loading branch information
promer94 committed Jul 13, 2023
1 parent 2efed28 commit 5e5e01a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
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

0 comments on commit 5e5e01a

Please sign in to comment.