Skip to content

Commit

Permalink
Merge pull request #1258 from gaearon/reload-effect
Browse files Browse the repository at this point in the history
Reload effect
  • Loading branch information
theKashey committed May 30, 2019
2 parents 3b4f4cc + 7ab076c commit d74c7d9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -53,6 +53,22 @@ export default hot(App);

4. If you need hooks support, use React-🔥-Dom

### Hook support

To enable hot hook update you have to enable it (not setting by default for now):

```js
import { setConfig } from 'react-hot-loader';

setConfig({
hotHooks: true,
});
```

With this option set **all** `useEffects`, `useCallbacks` and `useMemo` would be updated on Hot Module Replacement.

Please try it so we can enable it by default. (or not)

## React-🔥-Dom

React-🔥-Dom ([hot-loader/react-dom](https://github.com/hot-loader/react-dom)) replaces the "react-dom" package of the same version, but with additional patches to support hot reloading.
Expand Down
3 changes: 3 additions & 0 deletions src/configuration.js
Expand Up @@ -11,6 +11,9 @@ const configuration = {
// Allows SFC to be used, enables "intermediate" components used by Relay, should be disabled for Preact
allowSFC: true,

// Allow hot reload of effect hooks
hotHooks: false,

// Disable "hot-replacement-render"
disableHotRenderer: false,

Expand Down
14 changes: 14 additions & 0 deletions src/global/generation.js
@@ -1,13 +1,20 @@
import { forEachKnownClass } from '../proxy/createClassProxy';

// this counter tracks `register` invocations.
// works good, but code splitting is breaking it
let generation = 1;

// these counters are aimed to mitigate the "first render"
let hotComparisonCounter = 0;
let hotComparisonRuns = 0;
const nullFunction = () => ({});

// these callbacks would be called on component update
let onHotComparisonOpen = nullFunction;
let onHotComparisonElement = nullFunction;
let onHotComparisonClose = nullFunction;

// inversion of control
export const setComparisonHooks = (open, element, close) => {
onHotComparisonOpen = open;
onHotComparisonElement = element;
Expand Down Expand Up @@ -43,12 +50,19 @@ export const configureGeneration = (counter, runs) => {
hotComparisonRuns = runs;
};

// TODO: shall it be called from incrementHotGeneration?
export const enterHotUpdate = () => {
Promise.resolve(incrementHot()).then(() => setTimeout(decrementHot, 0));
};

// TODO: deprecate?
export const increment = () => {
enterHotUpdate();
return generation++;
};
export const get = () => generation;

// These counters tracks HMR generations, and probably should be used instead of the old one
let hotReplacementGeneration = 0;
export const incrementHotGeneration = () => hotReplacementGeneration++;
export const getHotGeneration = () => hotReplacementGeneration;
17 changes: 16 additions & 1 deletion src/reactHotLoader.js
Expand Up @@ -7,7 +7,7 @@ import {
isForwardType,
isContextType,
} from './internal/reactUtils';
import { increment as incrementGeneration } from './global/generation';
import { increment as incrementGeneration, getHotGeneration } from './global/generation';
import {
updateProxyById,
resetProxies,
Expand All @@ -26,6 +26,13 @@ import { hotComponentCompare } from './reconciler/componentComparator';

const forceSimpleSFC = { proxy: { pureSFC: true } };

const hookWrapper = hook => (cb, deps) => {
if (configuration.hotHooks) {
return hook(cb, deps ? [...deps, getHotGeneration()] : deps);
}
return hook(cb, deps);
};

const reactHotLoader = {
IS_REACT_MERGE_ENABLED: false,
register(type, uniqueLocalName, fileName, options = {}) {
Expand Down Expand Up @@ -75,6 +82,7 @@ const reactHotLoader = {
incrementGeneration();
}
if (isForwardType({ type })) {
reactHotLoader.register(type.render, `${uniqueLocalName}:render`, fileName, forceSimpleSFC);
updateFunctionProxyById(id, type, updateForward);
incrementGeneration();
}
Expand Down Expand Up @@ -158,6 +166,13 @@ const reactHotLoader = {
React.Children.only.isPatchedByReactHotLoader = true;
}

if (React.useEffect && !React.useState.isPatchedByReactHotLoader) {
React.useEffect = hookWrapper(React.useEffect);
React.useLayoutEffect = hookWrapper(React.useLayoutEffect);
React.useCallback = hookWrapper(React.useCallback);
React.useMemo = hookWrapper(React.useMemo);
}

// reactHotLoader.reset()
},
};
Expand Down
17 changes: 16 additions & 1 deletion src/reconciler/componentComparator.js
Expand Up @@ -11,6 +11,15 @@ const getInnerComponentType = component => {
return unwrapper ? unwrapper() : component;
};

const compareRegistered = (a, b) => {
if (isRegisteredComponent(a) || isRegisteredComponent(b)) {
if (resolveType(a) !== resolveType(b)) {
return false;
}
}
return true;
};

const compareComponents = (oldType, newType, setNewType, baseType) => {
let defaultResult = oldType === newType;

Expand All @@ -19,13 +28,16 @@ const compareComponents = (oldType, newType, setNewType, baseType) => {
}

if (isRegisteredComponent(oldType) || isRegisteredComponent(newType)) {
if (resolveType(oldType) !== resolveType(newType)) {
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 || areSwappable(oldType.render, newType.render)) {
setNewType(newType);
return true;
Expand All @@ -34,6 +46,9 @@ const compareComponents = (oldType, newType, setNewType, baseType) => {
}

if (isMemoType({ type: oldType }) && isMemoType({ type: newType })) {
if (!compareRegistered(oldType.type, newType.type)) {
return false;
}
if (oldType.type === newType.type || areSwappable(oldType.type, newType.type)) {
if (baseType) {
// memo form different fibers, why?
Expand Down
3 changes: 3 additions & 0 deletions src/reconciler/proxies.js
Expand Up @@ -2,6 +2,7 @@ import createProxy, { PROXY_KEY } from '../proxy';
import { resetClassProxies } from '../proxy/createClassProxy';
import { isCompositeComponent, isReactClass } from '../internal/reactUtils';
import configuration from '../configuration';
import { incrementHotGeneration } from '../global/generation';

const merge = require('lodash/merge');

Expand Down Expand Up @@ -58,6 +59,8 @@ export const updateProxyById = (id, type, options = {}) => {
);
} else {
proxiesByID[id].update(type);
// proxy could be registered again only in case of HMR
incrementHotGeneration();
}
return proxiesByID[id];
};
Expand Down

0 comments on commit d74c7d9

Please sign in to comment.