Skip to content

Commit

Permalink
Refactored unsubscribe matching to make better use of type narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
riqts committed Mar 18, 2024
1 parent 2b186ef commit 527a6b7
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/toolkit/src/query/core/buildMiddleware/cacheCollection.ts
@@ -1,3 +1,4 @@
import { isAnyOf } from '@reduxjs/toolkit'
import type { BaseQueryFn } from '../../baseQueryTypes'
import type { QueryDefinition } from '../../endpointDefinitions'
import type { ConfigState, QueryCacheKey } from '../apiState'
Expand Down Expand Up @@ -55,6 +56,12 @@ export const buildCacheCollectionHandler: InternalHandlerBuilder = ({
}) => {
const { removeQueryResult, unsubscribeQueryResult } = api.internalActions

const canTriggerUnsubscribe = isAnyOf(
unsubscribeQueryResult.match,
queryThunk.fulfilled,
queryThunk.rejected
)

function anySubscriptionsRemainingForKey(queryCacheKey: string) {
const subscriptions = internalState.currentSubscriptions[queryCacheKey]
return !!subscriptions && !isObjectEmpty(subscriptions)
Expand All @@ -67,18 +74,11 @@ export const buildCacheCollectionHandler: InternalHandlerBuilder = ({
mwApi,
internalState
) => {
if (unsubscribeQueryResult.match(action) ||
queryThunk.fulfilled.match(action) ||
queryThunk.rejected.match(action)
) {
if (canTriggerUnsubscribe(action)) {
const state = mwApi.getState()[reducerPath]
let queryCacheKey: QueryCacheKey

if (unsubscribeQueryResult.match(action)) {
queryCacheKey = action.payload.queryCacheKey
} else {
queryCacheKey = action.meta.arg.queryCacheKey
}
const { queryCacheKey } = unsubscribeQueryResult.match(action)
? action.payload
: action.meta.arg

handleUnsubscribe(
queryCacheKey,
Expand Down

0 comments on commit 527a6b7

Please sign in to comment.