Skip to content

Commit

Permalink
fix: autodetect underupdated state and trigger an automatic update, f…
Browse files Browse the repository at this point in the history
…ixes #1342
  • Loading branch information
theKashey committed Sep 23, 2019
1 parent 62bc67e commit 33c54f5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const configuration = {

// Global error overlay
ErrorOverlay: undefined,

// react hot dom features enabled
IS_REACT_MERGE_ENABLED: false,
};

export const internalConfiguration = {
Expand Down
31 changes: 25 additions & 6 deletions src/hot.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isOpened as isModuleOpened, hotModule, getLastModuleOpened } from './gl
import logger from './logger';
import { clearExceptions, logException } from './errorReporter';
import { createQueue } from './utils/runQueue';
import { enterHotUpdate, getHotGeneration } from './global/generation';

/* eslint-disable camelcase, no-undef */
const requireIndirect = typeof __webpack_require__ !== 'undefined' ? __webpack_require__ : require;
Expand Down Expand Up @@ -48,6 +49,29 @@ const makeHotExport = (sourceModule, moduleId) => {
}
const module = hotModule(moduleId);

const deepUpdate = () => {
// force flush all updates
runInRenderQueue(() => {
enterHotUpdate();
const gen = getHotGeneration();
module.instances.forEach(inst => inst.forceUpdate());

let runLimit = 0;
const checkTailUpdates = () => {
setTimeout(() => {
if (getHotGeneration() !== gen) {
console.warn('React-Hot-Loader has detected a stale state. Updating...');
deepUpdate();
} else if (++runLimit < 5) {
checkTailUpdates();
}
}, 16);
};

checkTailUpdates();
});
};

// require all modules
runInRequireQueue(() => {
try {
Expand All @@ -58,12 +82,7 @@ const makeHotExport = (sourceModule, moduleId) => {
console.error('React-Hot-Loader: error detected while loading', moduleId);
console.error(e);
}
}).then(() => {
// force flush all updates
runInRenderQueue(() => {
module.instances.forEach(inst => inst.forceUpdate());
});
});
}).then(deepUpdate);
};

if (sourceModule.hot) {
Expand Down
5 changes: 2 additions & 3 deletions src/reactHotLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const hookWrapper = hook => {
const noDeps = () => [];

const reactHotLoader = {
IS_REACT_MERGE_ENABLED: false,
signature(type, key, getCustomHooks = noDeps) {
addSignature(type, { key, getCustomHooks });
return type;
Expand All @@ -75,7 +74,7 @@ const reactHotLoader = {
const proxy = getProxyById(id);

if (proxy && proxy.getCurrent() !== type) {
if (!reactHotLoader.IS_REACT_MERGE_ENABLED) {
if (!configuration.IS_REACT_MERGE_ENABLED) {
if (isTypeBlacklisted(type) || isTypeBlacklisted(proxy.getCurrent())) {
logger.error('React-hot-loader: Cold component', uniqueLocalName, 'at', fileName, 'has been updated');
}
Expand Down Expand Up @@ -146,7 +145,7 @@ const reactHotLoader = {

configuration.ignoreSFC = configuration.ignoreSFCWhenInjected;

reactHotLoader.IS_REACT_MERGE_ENABLED = true;
configuration.IS_REACT_MERGE_ENABLED = true;
configuration.showReactDomPatchNotification = false;
configuration.integratedComparator = true;

Expand Down
5 changes: 2 additions & 3 deletions src/reconciler/hotReplacementRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
isLazyType,
isForwardType,
} from '../internal/reactUtils';
import reactHotLoader from '../reactHotLoader';
import logger from '../logger';
import configuration, { internalConfiguration } from '../configuration';
import { areSwappable } from './utils';
Expand Down Expand Up @@ -142,7 +141,7 @@ const mergeInject = (a, b, instance) => {
}
if (flatB.length === 0 && flatA.length === 1 && typeof flatA[0] !== 'object') {
// terminal node
} else if (!reactHotLoader.IS_REACT_MERGE_ENABLED) {
} else if (!configuration.IS_REACT_MERGE_ENABLED) {
logger.warn(`React-hot-loader: unable to merge `, a, 'and children of ', instance);
stackReport();
}
Expand Down Expand Up @@ -312,7 +311,7 @@ const hotReplacementRender = (instance, stack) => {
}

if (!stackChild.type[PROXY_KEY]) {
if (!reactHotLoader.IS_REACT_MERGE_ENABLED) {
if (!configuration.IS_REACT_MERGE_ENABLED) {
if (isTypeBlacklisted(stackChild.type)) {
logger.warn('React-hot-loader: cold element got updated ', stackChild.type);
}
Expand Down

0 comments on commit 33c54f5

Please sign in to comment.