fix(subscription): support async subscribe function in useSWRSubscription - #4345
Open
wasim-builds wants to merge 1 commit into
Open
wasim-builds wants to merge 1 commit into
wasim-builds wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #4214
Problem
Previously,
useSWRSubscriptionexecutedsubscribe(args, { next })synchronously and asserted that the returned disposer was a function viaif (typeof dispose !== 'function') throw new Error(...). Whensubscribeis an async function, it returns a Promise instead of an unsubscribe function, causing the hook to immediately throw an error. In addition, any cleanup function returned upon promise resolution was never registered or called, causing connection and memory leaks.Root Cause
SWRSubscriptiontype insrc/subscription/types.tsdid not explicitly typePromise<(() => void) | void>as an allowed return type.src/subscription/index.ts,subscribewas assumed to return a synchronous cleanup function. Promise returns were treated as invalid return values, and asynchronous resolution and unmount cleanup lifecycles were not handled.Fix
SWRSubscriptioninsrc/subscription/types.tsto allow(() => void) | void | Promise<(() => void) | void>as return types.src/subscription/index.ts:dispose && typeof dispose.then === 'function').disposers.next(error)if the subscription remains active.test/use-swr-subscription.test.tsxverifying:next.Verification
pnpm jest test/use-swr-subscription.test.tsx: 1 passed, 13 passed, 13 total.pnpm test-typing: Clean pass.pnpm test: 36 passed, 402 passed, 5 skipped, 407 total.