|
1 | 1 | import connectAdvanced from '../components/connectAdvanced' |
| 2 | +import shallowEqual from '../utils/shallowEqual' |
2 | 3 | import defaultMapDispatchToPropsFactories from './mapDispatchToProps' |
3 | 4 | import defaultMapStateToPropsFactories from './mapStateToProps' |
4 | 5 | import defaultMergePropsFactories from './mergeProps' |
@@ -29,46 +30,58 @@ function match(arg, factories) { |
29 | 30 | return undefined |
30 | 31 | } |
31 | 32 |
|
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 } |
48 | 34 |
|
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) |
52 | 60 |
|
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', |
55 | 64 |
|
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})`, |
58 | 67 |
|
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), |
65 | 70 |
|
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 | + }) |
68 | 84 | } |
69 | 85 | } |
70 | 86 |
|
71 | | -export default function connect(...args) { |
72 | | - const options = buildConnectOptions(...args) |
73 | | - return connectAdvanced(options.selectorFactory, options) |
74 | | -} |
| 87 | +export default createConnect() |
0 commit comments