Skip to content

experimental use support for suspense #2781

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
80 changes: 53 additions & 27 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,24 +728,14 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
promiseRef.current = undefined
}, [subscriptionRemoved])

usePossiblyImmediateEffect((): void | undefined => {
const initiateQueryIfNeeded = useCallback(() => {
const lastPromise = promiseRef.current
if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'removeMeOnCompilation'
) {
// this is only present to enforce the rule of hooks to keep `isSubscribed` in the dependency array
console.log(subscriptionRemoved)
}

if (stableArg === skipToken) {
lastPromise?.unsubscribe()
promiseRef.current = undefined
return
}

const lastSubscriptionOptions = promiseRef.current?.subscriptionOptions

if (!lastPromise || lastPromise.arg !== stableArg) {
lastPromise?.unsubscribe()
const promise = dispatch(
Expand All @@ -756,15 +746,38 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
)

promiseRef.current = promise
} else if (stableSubscriptionOptions !== lastSubscriptionOptions) {
lastPromise.updateSubscriptionOptions(stableSubscriptionOptions)
}
}, [
dispatch,
initiate,
refetchOnMountOrArgChange,
stableArg,
stableSubscriptionOptions,
])

usePossiblyImmediateEffect((): void | undefined => {
if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'removeMeOnCompilation'
) {
// this is only present to enforce the rule of hooks to keep `subscriptionRemoved` in the dependency array
// and will be removed on compilation
console.log(subscriptionRemoved)
}

initiateQueryIfNeeded()

if (
promiseRef.current &&
stableSubscriptionOptions !== promiseRef.current?.subscriptionOptions
) {
promiseRef.current.updateSubscriptionOptions(
stableSubscriptionOptions
)
}
}, [
initiateQueryIfNeeded,
stableSubscriptionOptions,
subscriptionRemoved,
])

Expand All @@ -775,21 +788,34 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
}
}, [])

return useMemo(
() => ({
/**
* A method to manually refetch data for the query
*/
refetch: () => {
if (!promiseRef.current)
throw new Error(
'Cannot refetch a query that has not been started yet.'
)
return promiseRef.current?.refetch()
return {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the entire code, but use will attach .value prop, so you want to keep useMemo maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point, that might be very tricky.

The return value of this hook will be spread in yet another hook, so having a stable value here will not help a lot - and that other value can be memoized, but will change more often since it has other "changing values" in it.

We would probably have to manage .value on our own in that context 🤔

/**
* A method to manually refetch data for the query
*/
refetch: useCallback(() => {
if (!promiseRef.current)
throw new Error(
'Cannot refetch a query that has not been started yet.'
)
return promiseRef.current?.refetch()
}, []),
/**
* a `.then` method that allows the return value to be used with the `use` hook like
* `const result = use(useSomeQuery(args))`
*/
then: useCallback<Promise<unknown>['then']>(
(onfulfilled, onrejected) => {
initiateQueryIfNeeded()
Copy link
Member Author

@phryneas phryneas Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is still open if React will throw away the current component state & refs when suspending before the component was rendered at least once.

If it would, this would not work (we would add new component subscriptions that would never be cleaned up) and we would have to try something with getRunningQueryThunk here instead. But let's hope that we won't have that problem in the future.


return (
promiseRef.current?.unwrap() ||
// skipToken handling
Promise.resolve(undefined)
).then(onfulfilled, onrejected)
},
}),
[]
)
[initiateQueryIfNeeded]
),
}
}

const useLazyQuerySubscription: UseLazyQuerySubscription<any> = ({
Expand Down