Skip to content

Commit

Permalink
fix: run hot in batched mode, fixes #1332
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Sep 11, 2019
1 parent 08d7ed1 commit e801daf
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/hot.dev.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import hoistNonReactStatic from 'hoist-non-react-statics';
import { getComponentDisplayName } from './internal/reactUtils';
import AppContainer from './AppContainer.dev';
import reactHotLoader from './reactHotLoader';
import { isOpened as isModuleOpened, hotModule, getLastModuleOpened } from './global/modules';
import logger from './logger';
import { clearExceptions, logException } from './errorReporter';
import { createQueue } from './utils/runQueue';

/* eslint-disable camelcase, no-undef */
const requireIndirect = typeof __webpack_require__ !== 'undefined' ? __webpack_require__ : require;
Expand All @@ -29,22 +31,36 @@ const createHoc = (SourceComponent, TargetComponent) => {
return TargetComponent;
};

const runInRequireQueue = createQueue();
const runInRenderQueue = createQueue(cb => {
if (ReactDOM.unstable_batchedUpdates) {
ReactDOM.unstable_batchedUpdates(cb);
} else {
cb();
}
});

const makeHotExport = (sourceModule, moduleId) => {
const updateInstances = possibleError => {
if (possibleError && possibleError instanceof Error) {
console.error(possibleError);
return;
}
const module = hotModule(moduleId);
clearTimeout(module.updateTimeout);
module.updateTimeout = setTimeout(() => {

// require all modules
runInRequireQueue(() => {
try {
requireIndirect(moduleId);
} catch (e) {
console.error('React-Hot-Loader: error detected while loading', moduleId);
console.error(e);
}
module.instances.forEach(inst => inst.forceUpdate());
}).then(() => {
// force flush all updates
runInRenderQueue(() => {
module.instances.forEach(inst => inst.forceUpdate());
});
});
};

Expand Down

0 comments on commit e801daf

Please sign in to comment.