Skip to content

Commit

Permalink
fix: false negative comparisons with react-hot-dom enabled, fixes #1299
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jul 14, 2019
1 parent 0d665c8 commit a1c5c31
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Expand Up @@ -9,4 +9,5 @@ babel.js
index.js
patch.js
root.js
test/hot/react-dom
test/hot/react-dom
coverage
50 changes: 27 additions & 23 deletions src/reconciler/componentComparator.js
Expand Up @@ -182,41 +182,45 @@ const compareComponents = (oldType, newType, setNewType, baseType) => {
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;
let result = oldType === newType;

if (!hotActive) {
return result;
}
// 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;
// }

if (
!isReloadableComponent(oldType) ||
!isReloadableComponent(newType) ||
isColdType(oldType) ||
isColdType(oldType) ||
!oldType ||
!newType ||
0
) {
return result;
}
let result = oldType === newType;

// comparison should be active only if hot update window
// or it would merge components it shall not
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;
}

if (result) {
return result;
}

const pair = knownPairs.get(oldType) || emptyMap;
return pair.get(newType) || false;
// result - true if components are equal, or were "equal" at any point in the past
return result || getKnownPair(oldType, newType) || false;
};
16 changes: 11 additions & 5 deletions src/reconciler/resolver.js
Expand Up @@ -12,17 +12,23 @@ import configuration, { internalConfiguration } from '../configuration';
const shouldNotPatchComponent = type => isTypeBlacklisted(type);

export function resolveType(type, options = {}) {
const element = { type };
if (isLazyType(element) || isMemoType(element) || isForwardType(element) || isContextType(element)) {
return getProxyByType(type) || type;
}

// fast return
if (!isCompositeComponent(type) || isProxyType(type)) {
return type;
}

const element = { type };

// fast meta
if (typeof element === 'object') {
if (isLazyType(element) || isMemoType(element) || isForwardType(element) || isContextType(element)) {
return getProxyByType(type) || type;
}
}

const existingProxy = getProxyByType(type);

// cold API
if (shouldNotPatchComponent(type)) {
return existingProxy ? existingProxy.getCurrent() : type;
}
Expand Down

0 comments on commit a1c5c31

Please sign in to comment.