Skip to content

Commit

Permalink
add options to control tail updates
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Sep 23, 2019
1 parent c89e306 commit d4b2fd5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export interface Config {
* Global error overlay
*/
ErrorOverlay: React.ComponentType<{ errors: Array<HotError> }>;

/**
* Controls tail(deferred) update checking
*/
trackTailUpdates: boolean;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const configuration = {
// flag to completely disable RHL for SFC. Probably don't use it without dom patch made.
ignoreSFC: false,

// flag to disable special treatment for React.lazy
ignoreLazy: false,

// ignoreSFC when injection into react-dom is made
ignoreSFCWhenInjected: true,

Expand All @@ -55,6 +58,9 @@ const configuration = {
// Global error overlay
ErrorOverlay: undefined,

// Actively track lazy loaded components
trackTailUpdates: true,

// react hot dom features enabled
IS_REACT_MERGE_ENABLED: false,
};
Expand Down
35 changes: 22 additions & 13 deletions src/hot.dev.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import hoistNonReactStatic from 'hoist-non-react-statics';

import { getComponentDisplayName } from './internal/reactUtils';
import configuration from './configuration';
import AppContainer from './AppContainer.dev';
import reactHotLoader from './reactHotLoader';
import { isOpened as isModuleOpened, hotModule, getLastModuleOpened } from './global/modules';
Expand Down Expand Up @@ -56,19 +58,26 @@ const makeHotExport = (sourceModule, moduleId) => {
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();
if (configuration.trackTailUpdates) {
let runLimit = 0;
const checkTailUpdates = () => {
setTimeout(() => {
if (getHotGeneration() !== gen) {
// we know that some components were updated, but not tracking which ones
// even if their updates might be incorporated automatically (like lazy)
// we dont know which one should be tracked, and which updates are important
console.warn(
'React-Hot-Loader: some components were updated out-of-bound. Updating your app to reconcile the changes.',
);
deepUpdate();
} else if (++runLimit < 5) {
checkTailUpdates();
}
}, 16);
};

checkTailUpdates();
}
});
};

Expand Down
10 changes: 5 additions & 5 deletions src/reconciler/fiberUpdater.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';

import configuration from '../configuration';
import { enterHotUpdate } from '../global/generation';
import AppContainer from '../AppContainer.dev';
import { resolveType } from './resolver';

const lazyConstructor = '_ctor';

const patchLazyContructor = target => {
if (!target[lazyConstructor].isPatchedByReactHotLoader) {
const patchLazyConstructor = target => {
if (!configuration.ignoreLazy && !target[lazyConstructor].isPatchedByReactHotLoader) {
const ctor = target[lazyConstructor];
target[lazyConstructor] = () =>
ctor().then(m => {
Expand Down Expand Up @@ -41,8 +41,8 @@ export const updateLazy = (target, type) => {
// just execute `import` and RHL.register will do the job
ctor();
}
patchLazyContructor(target);
patchLazyContructor(type);
patchLazyConstructor(target);
patchLazyConstructor(type);
};

export const updateMemo = (target, { type }) => {
Expand Down
2 changes: 1 addition & 1 deletion test/hot/react-dom.integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe(`🔥-dom`, () => {

if (React.useContext && String(() => 42).indexOf('=>') > 0) {
it('shall integrate with React', () => {
expect(ReactHotLoader.IS_REACT_MERGE_ENABLED).toBe(true);
expect(configuration.IS_REACT_MERGE_ENABLED).toBe(true);
expect(configuration.integratedResolver).toBe(true);
expect(React.useEffect.isPatchedByReactHotLoader).toBe(true);
});
Expand Down
2 changes: 1 addition & 1 deletion test/hot/react-dom.no-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe(`🔥-dom`, () => {

if (React.useContext && String(() => 42).indexOf('=>') > 0) {
it('shall (not) integrate with React', () => {
expect(ReactHotLoader.IS_REACT_MERGE_ENABLED).toBe(false);
expect(configuration.IS_REACT_MERGE_ENABLED).toBe(false);
expect(configuration.integratedResolver).toBe(false);
expect(React.useEffect.isPatchedByReactHotLoader).toBe(true);
});
Expand Down

0 comments on commit d4b2fd5

Please sign in to comment.