Skip to content

Commit 86b9072

Browse files
committed
Merge branch 'feature/5052-subscription-perf' of https://github.com/reduxjs/redux-toolkit into feature/5052-subscription-perf
2 parents 6a72fb6 + 0393bb5 commit 86b9072

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { InternalHandlerBuilder, SubscriptionSelectors } from './types'
22
import type { SubscriptionInternalState, SubscriptionState } from '../apiState'
33
import { produceWithPatches } from 'immer'
44
import type { Action } from '@reduxjs/toolkit'
5-
import { getOrInsert } from '../../utils/getOrInsert'
5+
import { getOrInsertComputed, createNewMap } from '../../utils/getOrInsert'
66

77
export const buildBatchedActionsHandler: InternalHandlerBuilder<
88
[actionShouldContinue: boolean, returnValue: SubscriptionSelectors | boolean]
@@ -48,10 +48,10 @@ export const buildBatchedActionsHandler: InternalHandlerBuilder<
4848
const {
4949
meta: { arg, requestId },
5050
} = action
51-
const substate = getOrInsert(
51+
const substate = getOrInsertComputed(
5252
currentSubscriptions,
5353
arg.queryCacheKey,
54-
new Map(),
54+
createNewMap,
5555
)
5656
if (arg.subscribe) {
5757
substate.set(
@@ -68,10 +68,10 @@ export const buildBatchedActionsHandler: InternalHandlerBuilder<
6868
meta: { condition, arg, requestId },
6969
} = action
7070
if (condition && arg.subscribe) {
71-
const substate = getOrInsert(
71+
const substate = getOrInsertComputed(
7272
currentSubscriptions,
7373
arg.queryCacheKey,
74-
new Map(),
74+
createNewMap,
7575
)
7676
substate.set(
7777
requestId,
@@ -88,9 +88,8 @@ export const buildBatchedActionsHandler: InternalHandlerBuilder<
8888
const getSubscriptions = () => internalState.currentSubscriptions
8989
const getSubscriptionCount = (queryCacheKey: string) => {
9090
const subscriptions = getSubscriptions()
91-
const subscriptionsForQueryArg =
92-
subscriptions.get(queryCacheKey) ?? new Map()
93-
return subscriptionsForQueryArg.size
91+
const subscriptionsForQueryArg = subscriptions.get(queryCacheKey)
92+
return subscriptionsForQueryArg?.size ?? 0
9493
}
9594
const isRequestSubscribed = (queryCacheKey: string, requestId: string) => {
9695
const subscriptions = getSubscriptions()

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
InternalHandlerBuilder,
1919
ApiMiddlewareInternalHandler,
2020
} from './types'
21-
import { getOrInsert } from '../../utils/getOrInsert'
21+
import { getOrInsertComputed, createNewMap } from '../../utils/getOrInsert'
2222

2323
export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
2424
reducerPath,
@@ -110,10 +110,10 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
110110
const valuesArray = Array.from(toInvalidate.values())
111111
for (const { queryCacheKey } of valuesArray) {
112112
const querySubState = state.queries[queryCacheKey]
113-
const subscriptionSubState = getOrInsert(
113+
const subscriptionSubState = getOrInsertComputed(
114114
internalState.currentSubscriptions,
115115
queryCacheKey,
116-
new Map(),
116+
createNewMap,
117117
)
118118

119119
if (querySubState) {

packages/toolkit/src/query/utils/getOrInsert.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Duplicate some of the utils in `/src/utils` to ensure
2+
// we don't end up dragging in larger chunks of the RTK core
3+
// into the RTKQ bundle
4+
15
export function getOrInsert<K extends object, V>(
26
map: WeakMap<K, V>,
37
key: K,
@@ -13,3 +17,25 @@ export function getOrInsert<K extends object, V>(
1317

1418
return map.set(key, value).get(key) as V
1519
}
20+
21+
export function getOrInsertComputed<K extends object, V>(
22+
map: WeakMap<K, V>,
23+
key: K,
24+
compute: (key: K) => V,
25+
): V
26+
export function getOrInsertComputed<K, V>(
27+
map: Map<K, V>,
28+
key: K,
29+
compute: (key: K) => V,
30+
): V
31+
export function getOrInsertComputed<K extends object, V>(
32+
map: Map<K, V> | WeakMap<K, V>,
33+
key: K,
34+
compute: (key: K) => V,
35+
): V {
36+
if (map.has(key)) return map.get(key) as V
37+
38+
return map.set(key, compute(key)).get(key) as V
39+
}
40+
41+
export const createNewMap = () => new Map()

0 commit comments

Comments
 (0)