-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
139 lines (112 loc) · 3.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {connect as reactReduxConnect, Provider} from 'react-redux';
import memoize, {shouldBePure} from 'memoize-state';
import functionDouble from 'function-double';
let config = {
onNotPure: (name, ...args) => {
console.group(`why-did-you-update-redux: ${name}`)
console.log('mapStateToProps:', args[1]);
console.log('functions'+args[2]);
console.groupEnd();
}
};
const realReactReduxConnect = reactReduxConnect;
const connect = (mapStateToProps,
mapDispatchToProps,
mergeProps,
options = {}) => {
if (options && ('pure' in options) && !options.pure) {
return realReactReduxConnect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
options
)
}
return WrappedComponent => {
let lastAffectedPaths = null;
let isFabric = 0;
const affectedMap = {};
function mapStateToPropsFabric() {
let localMapStateToProps;
let firstCall = true;
let result;
function finalMapStateToProps(state, props) {
if (firstCall) {
const defaultMapStateToProps = memoize(mapStateToProps, {strictArity: true});
if (isFabric === 0) {
result = defaultMapStateToProps(state, props);
if (typeof result === 'function') {
isFabric = 1;
functionDouble(finalMapStateToProps, result);
} else {
isFabric = -1;
}
}
if (isFabric === 1) {
localMapStateToProps = memoize(defaultMapStateToProps(state, props), {strictArity: true});
} else {
localMapStateToProps = defaultMapStateToProps;
}
}
result = localMapStateToProps(state, props);
if (firstCall || !localMapStateToProps.cacheStatistics.lastCallWasMemoized) {
// get state related paths
const affected = localMapStateToProps.getAffectedPaths()[0];
affected.forEach(key => (affectedMap[key.split('.')[1]] = true));
lastAffectedPaths = Object.keys(affectedMap);
}
firstCall = false;
return result;
}
functionDouble(finalMapStateToProps, mapStateToProps);
Object.defineProperty(finalMapStateToProps, 'trackedKeys', {
get: () => lastAffectedPaths,
configurable: true,
enumerable: false,
});
return finalMapStateToProps;
}
function areStatesEqual(state1, state2) {
if (!lastAffectedPaths) {
return state1 === state2;
}
return lastAffectedPaths.reduce((acc, key) => acc && state1[key] === state2[key], true);
}
const ImprovedComponent = realReactReduxConnect(
mapStateToProps && mapStateToPropsFabric,
mapDispatchToProps,
mergeProps,
Object.assign({areStatesEqual}, options)
)(WrappedComponent);
Object.defineProperty(ImprovedComponent, '__trackingPaths', {
get: () => lastAffectedPaths,
configurable: true,
enumerable: false,
});
return ImprovedComponent;
}
};
const onNotPure = (...args) => config.onNotPure(...args);
const connectAndCheck = (a, ...rest) => (
(WrappedComponent) => {
const Component = realReactReduxConnect(
a
? shouldBePure(a, {
onTrigger: (...args) => onNotPure(Component.displayName || Component.name, ...args),
checkAffectedKeys: false,
})
: a
, ...rest
)(WrappedComponent);
return Component;
}
);
const setConfig = options => {
config = Object.assign(config, options);
};
export {
connect,
Provider,
connectAndCheck,
setConfig
};