Skip to content

Commit af0c3a7

Browse files
Andaristmarkerikson
authored andcommitted
Fix perf problems with out-of-bounds array access
Matches state as of v7.0.0-alpha.3
1 parent 8b72631 commit af0c3a7

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/components/connectAdvanced.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import Subscription from '../utils/Subscription'
1212

1313
import { ReactReduxContext } from './Context'
1414

15+
const EMPTY_ARRAY = []
16+
17+
const NO_SUBSCRIPTION_ARRAY = [null, null]
18+
1519
const stringifyComponent = Comp => {
1620
try {
1721
return JSON.stringify(Comp)
@@ -20,6 +24,13 @@ const stringifyComponent = Comp => {
2024
}
2125
}
2226

27+
function storeStateUpdatesReducer(state, action) {
28+
const [, updateCount] = state
29+
return [action.payload, updateCount + 1]
30+
}
31+
32+
const initStateUpdates = () => [null, 0]
33+
2334
export default function connectAdvanced(
2435
/*
2536
selectorFactory is a func that is responsible for returning the selector function used to
@@ -132,11 +143,6 @@ export default function connectAdvanced(
132143

133144
const usePureOnlyMemo = pure ? useMemo : x => x()
134145

135-
function storeStateUpdatesReducer(state, action) {
136-
const [, updateCount = 0] = state
137-
return [action.payload, updateCount + 1]
138-
}
139-
140146
function ConnectFunction(props) {
141147
const [propsContext, forwardedRef, wrapperProps] = useMemo(() => {
142148
const { context, forwardedRef, ...wrapperProps } = props
@@ -168,7 +174,7 @@ export default function connectAdvanced(
168174
}, [store])
169175

170176
const [subscription, notifyNestedSubs] = useMemo(() => {
171-
if (!shouldHandleStateChanges) return []
177+
if (!shouldHandleStateChanges) return NO_SUBSCRIPTION_ARRAY
172178

173179
// parentSub's source should match where store came from: props vs. context. A component
174180
// connected to the store via props shouldn't use subscription from context, or vice versa.
@@ -197,7 +203,8 @@ export default function connectAdvanced(
197203

198204
const [[previousStateUpdateResult], dispatch] = useReducer(
199205
storeStateUpdatesReducer,
200-
[]
206+
EMPTY_ARRAY,
207+
initStateUpdates
201208
)
202209

203210
if (previousStateUpdateResult && previousStateUpdateResult.error) {
@@ -311,6 +318,7 @@ export default function connectAdvanced(
311318
}
312319

313320
const Connect = pure ? React.memo(ConnectFunction) : ConnectFunction
321+
314322
Connect.WrappedComponent = WrappedComponent
315323
Connect.displayName = displayName
316324

0 commit comments

Comments
 (0)