-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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, | ||
]) | ||
|
||
|
@@ -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 { | ||
/** | ||
* 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return ( | ||
promiseRef.current?.unwrap() || | ||
// skipToken handling | ||
Promise.resolve(undefined) | ||
).then(onfulfilled, onrejected) | ||
}, | ||
}), | ||
[] | ||
) | ||
[initiateQueryIfNeeded] | ||
), | ||
} | ||
} | ||
|
||
const useLazyQuerySubscription: UseLazyQuerySubscription<any> = ({ | ||
|
There was a problem hiding this comment.
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 keepuseMemo
maybe?There was a problem hiding this comment.
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 🤔