/
componentComparator.js
230 lines (198 loc) · 6.5 KB
/
componentComparator.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { getIdByType, getProxyByType, getSignature, isColdType, updateProxyById } from './proxies';
import { hotComparisonOpen } from '../global/generation';
import {
getElementType,
isContextType,
isForwardType,
isLazyType,
isMemoType,
isReactClass,
isReloadableComponent,
} from '../internal/reactUtils';
import { areSwappable } from './utils';
import { PROXY_KEY, UNWRAP_PROXY } from '../proxy';
import { resolveType } from './resolver';
import logger from '../logger';
import configuration from '../configuration';
const getInnerComponentType = component => {
const unwrapper = component[UNWRAP_PROXY];
return unwrapper ? unwrapper() : component;
};
function haveEqualSignatures(prevType, nextType) {
try {
const prevSignature = getSignature(prevType);
const nextSignature = getSignature(nextType);
if (prevSignature === undefined && nextSignature === undefined) {
return true;
}
if (prevSignature === undefined || nextSignature === undefined) {
return false;
}
if (prevSignature.key !== nextSignature.key) {
return false;
}
// TODO: we might need to calculate previous signature earlier in practice,
// such as during the first time a component is resolved. We'll revisit this.
const prevCustomHooks = prevSignature.getCustomHooks();
const nextCustomHooks = nextSignature.getCustomHooks();
if (prevCustomHooks.length !== nextCustomHooks.length) {
return false;
}
for (let i = 0; i < nextCustomHooks.length; i++) {
if (!haveEqualSignatures(prevCustomHooks[i], nextCustomHooks[i])) {
return false;
}
}
} catch (e) {
logger.error('React-Hot-Loader: error occurred while comparing hook signature', e);
return false;
}
return true;
}
const areSignaturesCompatible = (a, b) => {
// compare signatures of two components
// non-equal component have to remount and there is two options to do it
// - fail the comparison, remounting all tree below
// - fulfill it, but set `_debugNeedsRemount` on a fiber to drop only local state
// the second way is not published yet, so going with the first one
if (!haveEqualSignatures(a, b)) {
logger.warn('⚛️🔥🎣 Hook order change detected: component', a, 'has been remounted');
return false;
}
return true;
};
const compareRegistered = (a, b) =>
getIdByType(a) === getIdByType(b) && getProxyByType(a) === getProxyByType(b) && areSignaturesCompatible(a, b);
const areDeepSwappable = (oldType, newType) => {
const type = { type: oldType };
if (typeof oldType === 'function') {
return areSwappable(oldType, newType);
}
if (isForwardType(type)) {
return areDeepSwappable(oldType.render, newType.render);
}
if (isMemoType(type)) {
return areDeepSwappable(oldType.type, newType.type);
}
// that's not safe
// if (isLazyType(type)) {
// return areDeepSwappable(oldType._ctor, newType._ctor)
// }
return false;
};
const compareComponents = (oldType, newType, setNewType, baseType) => {
let defaultResult = oldType === newType;
if (
(oldType && !newType) ||
(!oldType && newType) ||
typeof oldType !== typeof newType ||
getElementType(oldType) !== getElementType(newType) ||
0
) {
return defaultResult;
}
if (getIdByType(newType) || getIdByType(oldType)) {
if (!compareRegistered(oldType, newType)) {
return false;
}
defaultResult = true;
}
if (isForwardType({ type: oldType }) && isForwardType({ type: newType })) {
if (!compareRegistered(oldType.render, newType.render)) {
return false;
}
if (oldType.render === newType.render || areDeepSwappable(oldType, newType)) {
setNewType(newType);
return true;
}
return defaultResult;
}
if (isMemoType({ type: oldType }) && isMemoType({ type: newType })) {
if (!compareRegistered(oldType.type, newType.type)) {
return false;
}
if (oldType.type === newType.type || areDeepSwappable(oldType, newType)) {
if (baseType) {
// memo form different fibers, why?
if (baseType.$$typeof === newType.$$typeof) {
setNewType(newType);
} else {
setNewType(newType.type);
}
} else {
logger.warn('Please update hot-loader/react-dom');
if (isReactClass(newType.type)) {
setNewType(newType);
} else {
setNewType(newType.type);
}
}
return true;
}
return defaultResult;
}
if (isLazyType({ type: oldType })) {
// no need to update
// setNewType(newType);
return defaultResult;
}
if (isContextType({ type: oldType })) {
// update provider
setNewType(newType);
return defaultResult;
}
if (
typeof newType === 'function' &&
(defaultResult ||
(newType !== oldType && areSignaturesCompatible(newType, oldType) && areSwappable(newType, oldType)))
) {
const unwrapFactory = newType[UNWRAP_PROXY];
const oldProxy = unwrapFactory && getProxyByType(unwrapFactory());
if (oldProxy) {
oldProxy.dereference();
updateProxyById(oldType[PROXY_KEY] || getIdByType(oldType), getInnerComponentType(newType));
} else {
setNewType(newType);
}
return true;
}
return defaultResult;
};
const knownPairs = new WeakMap();
const emptyMap = new WeakMap();
const getKnownPair = (oldType, newType) => {
const pair = knownPairs.get(oldType) || emptyMap;
return pair.get(newType);
};
export const hotComponentCompare = (oldType, preNewType, setNewType, baseType) => {
const hotActive = hotComparisonOpen();
const newType = configuration.integratedResolver ? resolveType(preNewType) : preNewType;
// TODO: find out the root cause
// we could not use "fast result" here - go a full part to update a fiber.
// const knownType = getKnownPair(oldType, newType);
// if (knownType !== undefined) {
// return knownType;
// }
let result = oldType === newType;
if (hotActive) {
// pre fail components which could not be merged
if (
!isReloadableComponent(oldType) ||
!isReloadableComponent(newType) ||
isColdType(oldType) ||
isColdType(oldType) ||
!oldType ||
!newType ||
0
) {
return result;
}
result = compareComponents(oldType, newType, setNewType, baseType);
const pair = knownPairs.get(oldType) || new WeakMap();
pair.set(newType, result);
knownPairs.set(oldType, pair);
return result;
}
// result - true if components are equal, or were "equal" at any point in the past
return result || getKnownPair(oldType, newType) || false;
};