From cd8531e8d6b4fd9c38947c0135b873a8ecb01cc5 Mon Sep 17 00:00:00 2001 From: Christian Baroni <7061887+christianbaroni@users.noreply.github.com> Date: Sun, 5 Jan 2025 21:03:24 +0000 Subject: [PATCH] Avoid redefinition of subscription handling logic across selectors --- src/state/internal/createQueryStore.ts | 62 +++++++++++++++----------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/src/state/internal/createQueryStore.ts b/src/state/internal/createQueryStore.ts index 60873a6ad1b..16fc1753625 100644 --- a/src/state/internal/createQueryStore.ts +++ b/src/state/internal/createQueryStore.ts @@ -815,38 +815,27 @@ export function createQueryStore< } }; - const subscribeWithSelector = api.subscribe; - api.subscribe = (listener: (state: S, prevState: S) => void) => { - set(state => ({ - ...state, - subscriptionCount: state.subscriptionCount + 1, - })); - - const unsubscribe = subscribeWithSelector((state: S, prevState: S) => { - listener(state, prevState); - handleSetEnabled(state, prevState); - }); - - const shouldWaitForParams = params !== undefined && !directValues && !paramAttachVals; - - if (shouldWaitForParams) { + const handleSubscribe = () => { + if (!directValues && !paramAttachVals && params !== undefined) { fetchAfterParamCreation = true; - } else { - const { enabled, fetch, isStale, subscriptionCount } = get(); - const currentParams = getCurrentResolvedParams(); - const currentQueryKey = getQueryKey(currentParams); + return; + } + const { enabled, fetch, isStale, subscriptionCount } = get(); + const currentParams = getCurrentResolvedParams(); + const currentQueryKey = getQueryKey(currentParams); - set(state => ({ ...state, queryKey: currentQueryKey })); + set(state => ({ ...state, queryKey: currentQueryKey })); - if ((subscriptionCount === 1 || disableAutoRefetching) && enabled) { - if (isStale() || !get().queryCache[currentQueryKey]?.lastFetchedAt) { - fetch(currentParams); - } else { - scheduleNextFetch(currentParams, undefined); - } + if ((subscriptionCount === 1 || disableAutoRefetching) && enabled) { + if (isStale() || !get().queryCache[currentQueryKey]?.lastFetchedAt) { + fetch(currentParams); + } else { + scheduleNextFetch(currentParams, undefined); } } + }; + const handleUnsubscribe = (unsubscribe: () => void) => { return () => { unsubscribe(); set(state => { @@ -863,6 +852,27 @@ export function createQueryStore< }; }; + const incrementSubscriptionCount = () => { + set(state => ({ + ...state, + subscriptionCount: state.subscriptionCount + 1, + })); + }; + + const subscribeWithSelector = api.subscribe; + api.subscribe = (listener: (state: S, prevState: S) => void) => { + incrementSubscriptionCount(); + + const unsubscribe = subscribeWithSelector((state: S, prevState: S) => { + listener(state, prevState); + handleSetEnabled(state, prevState); + }); + + handleSubscribe(); + + return handleUnsubscribe(unsubscribe); + }; + const userState = customStateCreator?.(set, get, api) ?? ({} as U); /* Merge base data, user state, and methods into the final store state */