Skip to content

Commit 1696f7a

Browse files
committed
Move subscription tracking logic over to batchActions file
1 parent 8d506ba commit 1696f7a

File tree

2 files changed

+62
-59
lines changed

2 files changed

+62
-59
lines changed

packages/toolkit/src/query/core/buildMiddleware/batchActions.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { QueryThunk, RejectedAction } from '../buildThunks'
22
import type { InternalHandlerBuilder } from './types'
3-
import type { SubscriptionState } from '../apiState'
3+
import type {
4+
SubscriptionState,
5+
QuerySubstateIdentifier,
6+
Subscribers,
7+
} from '../apiState'
48
import { produceWithPatches } from 'immer'
9+
import { createSlice, PayloadAction, AnyAction } from '@reduxjs/toolkit'
510

611
// Copied from https://github.com/feross/queue-microtask
712
let promise: Promise<any>
@@ -19,14 +24,69 @@ const queueMicrotaskShim =
1924
export const buildBatchedActionsHandler: InternalHandlerBuilder<
2025
[actionShouldContinue: boolean, subscriptionExists: boolean]
2126
> = ({ api, queryThunk, internalState }) => {
22-
const { actuallyMutateSubscriptions } = api.internalActions
2327
const subscriptionsPrefix = `${api.reducerPath}/subscriptions`
2428

2529
let previousSubscriptions: SubscriptionState =
2630
null as unknown as SubscriptionState
2731

2832
let dispatchQueued = false
2933

34+
const { updateSubscriptionOptions, unsubscribeQueryResult } =
35+
api.internalActions
36+
37+
// Actually intentionally mutate the subscriptions state used in the middleware
38+
// This is done to speed up perf when loading many components
39+
const actuallyMutateSubscriptions = (
40+
mutableState: SubscriptionState,
41+
action: AnyAction
42+
) => {
43+
if (updateSubscriptionOptions.match(action)) {
44+
const { queryCacheKey, requestId, options } = action.payload
45+
46+
if (mutableState?.[queryCacheKey]?.[requestId]) {
47+
mutableState[queryCacheKey]![requestId] = options
48+
}
49+
return true
50+
}
51+
if (unsubscribeQueryResult.match(action)) {
52+
const { queryCacheKey, requestId } = action.payload
53+
if (mutableState[queryCacheKey]) {
54+
delete mutableState[queryCacheKey]![requestId]
55+
}
56+
return true
57+
}
58+
if (api.internalActions.removeQueryResult.match(action)) {
59+
delete mutableState[action.payload.queryCacheKey]
60+
return true
61+
}
62+
if (queryThunk.pending.match(action)) {
63+
const {
64+
meta: { arg, requestId },
65+
} = action
66+
if (arg.subscribe) {
67+
const substate = (mutableState[arg.queryCacheKey] ??= {})
68+
substate[requestId] =
69+
arg.subscriptionOptions ?? substate[requestId] ?? {}
70+
71+
return true
72+
}
73+
}
74+
if (queryThunk.rejected.match(action)) {
75+
const {
76+
meta: { condition, arg, requestId },
77+
} = action
78+
if (condition && arg.subscribe) {
79+
const substate = (mutableState[arg.queryCacheKey] ??= {})
80+
substate[requestId] =
81+
arg.subscriptionOptions ?? substate[requestId] ?? {}
82+
83+
return true
84+
}
85+
}
86+
87+
return false
88+
}
89+
3090
return (action, mwApi) => {
3191
if (!previousSubscriptions) {
3292
// Initialize it the first time this handler runs

packages/toolkit/src/query/core/buildSlice.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -412,62 +412,6 @@ export function buildSlice({
412412
},
413413
})
414414

415-
const { updateSubscriptionOptions, unsubscribeQueryResult } =
416-
subscriptionSlice.actions
417-
418-
// Actually intentionally mutate the subscriptions state used in the middleware
419-
// This is done to speed up perf when loading many components
420-
const actuallyMutateSubscriptions = (
421-
draft: SubscriptionState,
422-
action: AnyAction
423-
) => {
424-
if (updateSubscriptionOptions.match(action)) {
425-
const { queryCacheKey, requestId, options } = action.payload
426-
427-
if (draft?.[queryCacheKey]?.[requestId]) {
428-
draft[queryCacheKey]![requestId] = options
429-
}
430-
return true
431-
}
432-
if (unsubscribeQueryResult.match(action)) {
433-
const { queryCacheKey, requestId } = action.payload
434-
if (draft[queryCacheKey]) {
435-
delete draft[queryCacheKey]![requestId]
436-
}
437-
return true
438-
}
439-
if (querySlice.actions.removeQueryResult.match(action)) {
440-
delete draft[action.payload.queryCacheKey]
441-
return true
442-
}
443-
if (queryThunk.pending.match(action)) {
444-
const {
445-
meta: { arg, requestId },
446-
} = action
447-
if (arg.subscribe) {
448-
const substate = (draft[arg.queryCacheKey] ??= {})
449-
substate[requestId] =
450-
arg.subscriptionOptions ?? substate[requestId] ?? {}
451-
452-
return true
453-
}
454-
}
455-
if (queryThunk.rejected.match(action)) {
456-
const {
457-
meta: { condition, arg, requestId },
458-
} = action
459-
if (condition && arg.subscribe) {
460-
const substate = (draft[arg.queryCacheKey] ??= {})
461-
substate[requestId] =
462-
arg.subscriptionOptions ?? substate[requestId] ?? {}
463-
464-
return true
465-
}
466-
}
467-
468-
return false
469-
}
470-
471415
const internalSubscriptionsSlice = createSlice({
472416
name: `${reducerPath}/internalSubscriptions`,
473417
initialState: initialState as SubscriptionState,
@@ -536,7 +480,6 @@ export function buildSlice({
536480
/** @deprecated has been renamed to `removeMutationResult` */
537481
unsubscribeMutationResult: mutationSlice.actions.removeMutationResult,
538482
resetApiState,
539-
actuallyMutateSubscriptions,
540483
}
541484

542485
return { reducer, actions }

0 commit comments

Comments
 (0)