Skip to content

Commit 3d01c60

Browse files
committed
Deduplicate promise unsubscribes and endpoint names
1 parent 31a2720 commit 3d01c60

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,15 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
14741474
deps?: DependencyList,
14751475
) => void = unstable__sideEffectsInRender ? (cb) => cb() : useEffect
14761476

1477+
type UnsubscribePromiseRef = React.RefObject<
1478+
{ unsubscribe?: () => void } | undefined
1479+
>
1480+
1481+
const unsubscribePromiseRef = (ref: UnsubscribePromiseRef) =>
1482+
ref.current?.unsubscribe?.()
1483+
1484+
const endpointDefinitions = context.endpointDefinitions
1485+
14771486
return {
14781487
buildQueryHooks,
14791488
buildInfiniteQueryHooks,
@@ -1491,7 +1500,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
14911500
// in this case, reset the hook
14921501
if (lastResult?.endpointName && currentState.isUninitialized) {
14931502
const { endpointName } = lastResult
1494-
const endpointDefinition = context.endpointDefinitions[endpointName]
1503+
const endpointDefinition = endpointDefinitions[endpointName]
14951504
if (
14961505
queryArgs !== skipToken &&
14971506
serializeQueryArgs({
@@ -1551,7 +1560,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
15511560
// in this case, reset the hook
15521561
if (lastResult?.endpointName && currentState.isUninitialized) {
15531562
const { endpointName } = lastResult
1554-
const endpointDefinition = context.endpointDefinitions[endpointName]
1563+
const endpointDefinition = endpointDefinitions[endpointName]
15551564
if (
15561565
queryArgs !== skipToken &&
15571566
serializeQueryArgs({
@@ -1724,9 +1733,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
17241733
initiate(stableArg, {
17251734
subscriptionOptions: stableSubscriptionOptions,
17261735
forceRefetch: refetchOnMountOrArgChange,
1727-
...(isInfiniteQueryDefinition(
1728-
context.endpointDefinitions[endpointName],
1729-
)
1736+
...(isInfiniteQueryDefinition(endpointDefinitions[endpointName])
17301737
? {
17311738
initialPageParam: stableInitialPageParam,
17321739
}
@@ -1832,11 +1839,11 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
18321839
}
18331840

18341841
function usePromiseRefUnsubscribeOnUnmount(
1835-
promiseRef: React.RefObject<{ unsubscribe?: () => void } | undefined>,
1842+
promiseRef: UnsubscribePromiseRef,
18361843
) {
18371844
useEffect(() => {
18381845
return () => {
1839-
promiseRef.current?.unsubscribe?.()
1846+
unsubscribePromiseRef(promiseRef)
18401847
// eslint-disable-next-line react-hooks/exhaustive-deps
18411848
;(promiseRef.current as any) = undefined
18421849
}
@@ -1924,7 +1931,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
19241931
let promise: QueryActionCreatorResult<any>
19251932

19261933
batch(() => {
1927-
promiseRef.current?.unsubscribe()
1934+
unsubscribePromiseRef(promiseRef)
19281935

19291936
promiseRef.current = promise = dispatch(
19301937
initiate(arg, {
@@ -1954,7 +1961,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
19541961
/* cleanup on unmount */
19551962
useEffect(() => {
19561963
return () => {
1957-
promiseRef?.current?.unsubscribe()
1964+
unsubscribePromiseRef(promiseRef)
19581965
}
19591966
}, [])
19601967

@@ -2038,7 +2045,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
20382045
let promise: InfiniteQueryActionCreatorResult<any>
20392046

20402047
batch(() => {
2041-
promiseRef.current?.unsubscribe()
2048+
unsubscribePromiseRef(promiseRef)
20422049

20432050
promiseRef.current = promise = dispatch(
20442051
(initiate as StartInfiniteQueryActionCreator<any>)(arg, {

0 commit comments

Comments
 (0)