Skip to content

Commit 517a9f9

Browse files
jimbollatimdorr
authored andcommitted
groups factories options and move *areEqual defaults into connect (#477)
* groups factories options and move *areEqual defaults into connect * refactors connect so that new "factories" extension points aren't in main API
1 parent 9fccae5 commit 517a9f9

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

src/connect/connect.js

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import connectAdvanced from '../components/connectAdvanced'
2+
import shallowEqual from '../utils/shallowEqual'
23
import defaultMapDispatchToPropsFactories from './mapDispatchToProps'
34
import defaultMapStateToPropsFactories from './mapStateToProps'
45
import defaultMergePropsFactories from './mergeProps'
@@ -29,46 +30,58 @@ function match(arg, factories) {
2930
return undefined
3031
}
3132

32-
export function buildConnectOptions(
33-
mapStateToProps,
34-
mapDispatchToProps,
35-
mergeProps,
36-
{
37-
mapStateToPropsFactories = defaultMapStateToPropsFactories,
38-
mapDispatchToPropsFactories = defaultMapDispatchToPropsFactories,
39-
mergePropsFactories = defaultMergePropsFactories,
40-
selectorFactory = defaultSelectorFactory,
41-
pure = true,
42-
...options
43-
} = {}
44-
) {
45-
const initMapStateToProps = match(mapStateToProps, mapStateToPropsFactories)
46-
const initMapDispatchToProps = match(mapDispatchToProps, mapDispatchToPropsFactories)
47-
const initMergeProps = match(mergeProps, mergePropsFactories)
33+
function strictEqual(a, b) { return a === b }
4834

49-
return {
50-
// used in error messages
51-
methodName: 'connect',
35+
// createConnect with default args builds the 'official' connect behavior. Calling it with
36+
// different options opens up some testing and extensibility scenarios
37+
export function createConnect({
38+
connectHOC = connectAdvanced,
39+
mapStateToPropsFactories = defaultMapStateToPropsFactories,
40+
mapDispatchToPropsFactories = defaultMapDispatchToPropsFactories,
41+
mergePropsFactories = defaultMergePropsFactories,
42+
selectorFactory = defaultSelectorFactory
43+
} = {}) {
44+
return function connect(
45+
mapStateToProps,
46+
mapDispatchToProps,
47+
mergeProps,
48+
{
49+
pure = true,
50+
areStatesEqual = strictEqual,
51+
areOwnPropsEqual = shallowEqual,
52+
areStatePropsEqual = shallowEqual,
53+
areMergedPropsEqual = shallowEqual,
54+
...extraOptions
55+
} = {}
56+
) {
57+
const initMapStateToProps = match(mapStateToProps, mapStateToPropsFactories)
58+
const initMapDispatchToProps = match(mapDispatchToProps, mapDispatchToPropsFactories)
59+
const initMergeProps = match(mergeProps, mergePropsFactories)
5260

53-
// used to compute Connect's displayName from the wrapped component's displayName.
54-
getDisplayName: name => `Connect(${name})`,
61+
return connectHOC(selectorFactory, {
62+
// used in error messages
63+
methodName: 'connect',
5564

56-
// if mapStateToProps is falsy, the Connect component doesn't subscribe to store state changes
57-
shouldHandleStateChanges: Boolean(mapStateToProps),
65+
// used to compute Connect's displayName from the wrapped component's displayName.
66+
getDisplayName: name => `Connect(${name})`,
5867

59-
// passed through to selectorFactory
60-
selectorFactory,
61-
initMapStateToProps,
62-
initMapDispatchToProps,
63-
initMergeProps,
64-
pure,
68+
// if mapStateToProps is falsy, the Connect component doesn't subscribe to store state changes
69+
shouldHandleStateChanges: Boolean(mapStateToProps),
6570

66-
// any addional options args can override defaults of connect or connectAdvanced
67-
...options
71+
// passed through to selectorFactory
72+
initMapStateToProps,
73+
initMapDispatchToProps,
74+
initMergeProps,
75+
pure,
76+
areStatesEqual,
77+
areOwnPropsEqual,
78+
areStatePropsEqual,
79+
areMergedPropsEqual,
80+
81+
// any extra options args can override defaults of connect or connectAdvanced
82+
...extraOptions
83+
})
6884
}
6985
}
7086

71-
export default function connect(...args) {
72-
const options = buildConnectOptions(...args)
73-
return connectAdvanced(options.selectorFactory, options)
74-
}
87+
export default createConnect()

src/connect/mergeProps.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import shallowEqual from '../utils/shallowEqual'
21
import verifyPlainObject from '../utils/verifyPlainObject'
32
import warning from '../utils/warning'
43

@@ -23,7 +22,7 @@ export function defaultMergeProps(stateProps, dispatchProps, ownProps) {
2322

2423
export function wrapMergePropsFunc(mergeProps) {
2524
return function initMergePropsProxy(
26-
dispatch, { displayName, pure, areMergedPropsEqual = shallowEqual }
25+
dispatch, { displayName, pure, areMergedPropsEqual }
2726
) {
2827
let hasRunOnce = false
2928
let mergedProps

src/connect/selectorFactory.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import verifySubselectors from './verifySubselectors'
2-
import shallowEqual from '../utils/shallowEqual'
32

43
export function impureFinalPropsSelectorFactory(
54
mapStateToProps,
@@ -16,18 +15,12 @@ export function impureFinalPropsSelectorFactory(
1615
}
1716
}
1817

19-
function strictEqual(a, b) { return a === b }
20-
2118
export function pureFinalPropsSelectorFactory(
2219
mapStateToProps,
2320
mapDispatchToProps,
2421
mergeProps,
2522
dispatch,
26-
{
27-
areStatesEqual = strictEqual,
28-
areOwnPropsEqual = shallowEqual,
29-
areStatePropsEqual = shallowEqual
30-
}
23+
{ areStatesEqual, areOwnPropsEqual, areStatePropsEqual }
3124
) {
3225
let hasRunAtLeastOnce = false
3326
let state

0 commit comments

Comments
 (0)