mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[Fresh] Set up initial scaffolding (#15619)
* Add a minimal failing test for hot reload * Set up scaffolding for React Fresh * Consider type family when comparing elementType Rendering an element with stale type should not cause it to remount. We only do this for FunctionComponent tag since checking is unnecessary for classes or host components. * Add support for forwardRef() Initially I thought I would compare families of inner .render functions. However, there is a corner case where this can create false positives. Such as when you forwardRef(X) the same X twice. Those are supposed to be distinct. But if we compare .render functions, we wouldn't be able to distinguish them after first reload. It seems safer to rely on explicit registration for those. This should be easy when forwardRef() call is in the same file, and usually it would be. For cases like HOCs and style.div`...` factories that return forwardRef(), we could have the __register__ helper itself "dig deeper" and register the inner function. * Show how forwardRef inner identity can be inferred The __register__ implementation can read the inner identity itself. * Add missing __DEV__ to tests * Add support for memo() (without fixing bailouts) This adds rudimentary support for memo components. However, we don't actually skip bailouts yet, so this is not very useful by itself alone. Tests have TODOs that we need to remove after bailout skipping is done. * Refactor type comparison for clarity * Hot update shouldn't re-render ancestor components unnecessarily My code had a bug where it checked for a wrong thing in a wrong set, leading us to always re-render. This fixes the checks so that we only schedule updates for things that were actually edited. * Add test coverage for memo(fn, areEqual) * Explicitly skip bailouts for hot reloading fibers This forces even memo() with shallow comparison to re-render on hot update. * Refactor scheduling update to reduce duplication * Remove unused variable in test * Don't check presence in a set while not hot reloading * Make scheduleHotUpdate() take named arguments * Don't keep unedited component types in the type => family map It's unnecessary because if they haven't been edited, there's no special reconciliation logic. * Add signatures that force remounting Signatures let us force a remount of a type even if from React's point of view, type is the same. A type has one current signature. If that signature changes during next hot update, all Fibers with that type should be deleted and remounted. We do this by mutating elementType scheduling a parent. This will be handy to force remount of mismatching Hooks, as well as failed error boundaries. For this to fully work, we'll need to add a way to skip built-in bailouts for all Fiber types. This will be the most invasive and annoying change. I did it for HostRoot in this PR but there's more. I'll add an automated test case that catches the missing bailout bailouts. * Support forced remounting for all component types This teaches all parent component types to remount their child if necessary. It also adds tests for them. * Remount effects while preserving state for hot reloaded components This makes sure that changes to *code* always propagate. It can break components that aren't resilient to useEffect over-firing, but that seems like a good constraint since you might need to add a dependency later anyway, and this helps avoid coding yourself into the corner. * Add missing __DEV__ blocks to tests * Fix unused variables in tests * Remove outdated TODO * Expose scheduleHotUpdate directly * Inline isCompatibleType * Run one check per component for invalidating deps This also makes the bailouts more targeted--no need to remount useEffect for a parent component of remounted fiber. * Resolve .type early This moves resolving to set up the right .type early instead of doing this before render. A bit more future-proof in case we want to restructure the begin phase later. ForwardRef is special because its type is a wrapper but it can be hot reloaded by itself. So we have a special overload for it that reconstucts the wrapper type if needed. * Add a Suspense todo * Use current.type !== workInProgress.type for ignoring deps This gets rid of one of the sets. * Use workInProgress.type !== current.type check for force re-render We still use a set for forced remount though. * Use wip.type !== current.type check in more places This also disables the remounting tests. They need a separate approach. * Use a dedicated remount mechanism * Add a test for offscreen trees It has a TODO because it seems like offscreen updates are incorrectly applied too soon. * Enable offscreen test now that it is fixed * Fix corner cases in the new remounting mechanism * Remount failed error boundaries on hot reload * Fix test now that act() flushes This test is manual so I don't actually want act here. * Nits * Add comments
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+12
-2
@@ -43,6 +43,7 @@ import {
|
||||
getCurrentFiberStackInDev,
|
||||
getStackByFiberInDevAndProd,
|
||||
} from './ReactCurrentFiber';
|
||||
import {isCompatibleFamilyForHotReloading} from './ReactFiberHotReloading';
|
||||
import {StrictMode} from './ReactTypeOfMode';
|
||||
|
||||
let didWarnAboutMaps;
|
||||
@@ -378,7 +379,12 @@ function ChildReconciler(shouldTrackSideEffects) {
|
||||
element: ReactElement,
|
||||
expirationTime: ExpirationTime,
|
||||
): Fiber {
|
||||
if (current !== null && current.elementType === element.type) {
|
||||
if (
|
||||
current !== null &&
|
||||
(current.elementType === element.type ||
|
||||
// Keep this check inline so it only runs on the false path:
|
||||
(__DEV__ ? isCompatibleFamilyForHotReloading(current, element) : false))
|
||||
) {
|
||||
// Move based on index
|
||||
const existing = useFiber(current, element.props, expirationTime);
|
||||
existing.ref = coerceRef(returnFiber, current, element);
|
||||
@@ -1121,7 +1127,11 @@ function ChildReconciler(shouldTrackSideEffects) {
|
||||
if (
|
||||
child.tag === Fragment
|
||||
? element.type === REACT_FRAGMENT_TYPE
|
||||
: child.elementType === element.type
|
||||
: child.elementType === element.type ||
|
||||
// Keep this check inline so it only runs on the false path:
|
||||
(__DEV__
|
||||
? isCompatibleFamilyForHotReloading(child, element)
|
||||
: false)
|
||||
) {
|
||||
deleteRemainingChildren(returnFiber, child.sibling);
|
||||
const existing = useFiber(
|
||||
|
||||
+31
@@ -45,6 +45,7 @@ import {
|
||||
SuspenseComponent,
|
||||
FunctionComponent,
|
||||
MemoComponent,
|
||||
SimpleMemoComponent,
|
||||
LazyComponent,
|
||||
EventComponent,
|
||||
EventTarget,
|
||||
@@ -52,6 +53,10 @@ import {
|
||||
import getComponentName from 'shared/getComponentName';
|
||||
|
||||
import {isDevToolsPresent} from './ReactFiberDevToolsHook';
|
||||
import {
|
||||
resolveFunctionForHotReloading,
|
||||
resolveForwardRefForHotReloading,
|
||||
} from './ReactFiberHotReloading';
|
||||
import {NoWork} from './ReactFiberExpirationTime';
|
||||
import {
|
||||
NoMode,
|
||||
@@ -218,6 +223,7 @@ export type Fiber = {|
|
||||
_debugSource?: Source | null,
|
||||
_debugOwner?: Fiber | null,
|
||||
_debugIsCurrentlyTiming?: boolean,
|
||||
_debugNeedsRemount?: boolean,
|
||||
|
||||
// Used to verify that the order of hooks does not change between renders.
|
||||
_debugHookTypes?: Array<HookType> | null,
|
||||
@@ -302,6 +308,7 @@ function FiberNode(
|
||||
this._debugSource = null;
|
||||
this._debugOwner = null;
|
||||
this._debugIsCurrentlyTiming = false;
|
||||
this._debugNeedsRemount = false;
|
||||
this._debugHookTypes = null;
|
||||
if (!hasBadMapPolyfill && typeof Object.preventExtensions === 'function') {
|
||||
Object.preventExtensions(this);
|
||||
@@ -434,6 +441,22 @@ export function createWorkInProgress(
|
||||
workInProgress.treeBaseDuration = current.treeBaseDuration;
|
||||
}
|
||||
|
||||
if (__DEV__) {
|
||||
workInProgress._debugNeedsRemount = current._debugNeedsRemount;
|
||||
switch (workInProgress.tag) {
|
||||
case IndeterminateComponent:
|
||||
case FunctionComponent:
|
||||
case SimpleMemoComponent:
|
||||
workInProgress.type = resolveFunctionForHotReloading(current.type);
|
||||
break;
|
||||
case ForwardRef:
|
||||
workInProgress.type = resolveForwardRefForHotReloading(current.type);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return workInProgress;
|
||||
}
|
||||
|
||||
@@ -473,6 +496,10 @@ export function createFiberFromTypeAndProps(
|
||||
if (typeof type === 'function') {
|
||||
if (shouldConstruct(type)) {
|
||||
fiberTag = ClassComponent;
|
||||
} else {
|
||||
if (__DEV__) {
|
||||
resolvedType = resolveFunctionForHotReloading(resolvedType);
|
||||
}
|
||||
}
|
||||
} else if (typeof type === 'string') {
|
||||
fiberTag = HostComponent;
|
||||
@@ -509,6 +536,9 @@ export function createFiberFromTypeAndProps(
|
||||
break getTag;
|
||||
case REACT_FORWARD_REF_TYPE:
|
||||
fiberTag = ForwardRef;
|
||||
if (__DEV__) {
|
||||
resolvedType = resolveForwardRefForHotReloading(resolvedType);
|
||||
}
|
||||
break getTag;
|
||||
case REACT_MEMO_TYPE:
|
||||
fiberTag = MemoComponent;
|
||||
@@ -777,6 +807,7 @@ export function assignFiberPropertiesInDEV(
|
||||
target._debugSource = source._debugSource;
|
||||
target._debugOwner = source._debugOwner;
|
||||
target._debugIsCurrentlyTiming = source._debugIsCurrentlyTiming;
|
||||
target._debugNeedsRemount = source._debugNeedsRemount;
|
||||
target._debugHookTypes = source._debugHookTypes;
|
||||
return target;
|
||||
}
|
||||
|
||||
+100
-6
@@ -71,6 +71,7 @@ import {
|
||||
getCurrentFiberStackInDev,
|
||||
} from './ReactCurrentFiber';
|
||||
import {startWorkTimer, cancelWorkTimer} from './ReactDebugFiberPerf';
|
||||
import {resolveFunctionForHotReloading} from './ReactFiberHotReloading';
|
||||
|
||||
import {
|
||||
mountChildFibers,
|
||||
@@ -358,18 +359,22 @@ function updateMemoComponent(
|
||||
// SimpleMemoComponent codepath doesn't resolve outer props either.
|
||||
Component.defaultProps === undefined
|
||||
) {
|
||||
let resolvedType = type;
|
||||
if (__DEV__) {
|
||||
resolvedType = resolveFunctionForHotReloading(type);
|
||||
}
|
||||
// If this is a plain function component without default props,
|
||||
// and with only the default shallow comparison, we upgrade it
|
||||
// to a SimpleMemoComponent to allow fast path updates.
|
||||
workInProgress.tag = SimpleMemoComponent;
|
||||
workInProgress.type = type;
|
||||
workInProgress.type = resolvedType;
|
||||
if (__DEV__) {
|
||||
validateFunctionComponentInDev(workInProgress, type);
|
||||
}
|
||||
return updateSimpleMemoComponent(
|
||||
current,
|
||||
workInProgress,
|
||||
type,
|
||||
resolvedType,
|
||||
nextProps,
|
||||
updateExpirationTime,
|
||||
renderExpirationTime,
|
||||
@@ -486,7 +491,9 @@ function updateSimpleMemoComponent(
|
||||
const prevProps = current.memoizedProps;
|
||||
if (
|
||||
shallowEqual(prevProps, nextProps) &&
|
||||
current.ref === workInProgress.ref
|
||||
current.ref === workInProgress.ref &&
|
||||
// Prevent bailout if the implementation changed due to hot reload:
|
||||
(__DEV__ ? workInProgress.type === current.type : true)
|
||||
) {
|
||||
didReceiveUpdate = false;
|
||||
if (updateExpirationTime < renderExpirationTime) {
|
||||
@@ -1023,6 +1030,9 @@ function mountLazyComponent(
|
||||
// Cancel and resume right after we know the tag.
|
||||
cancelWorkTimer(workInProgress);
|
||||
let Component = readLazyComponentType(elementType);
|
||||
if (__DEV__) {
|
||||
// TODO: resolve type for hot reloading.
|
||||
}
|
||||
// Store the unwrapped component in the type.
|
||||
workInProgress.type = Component;
|
||||
const resolvedTag = (workInProgress.tag = resolveLazyComponentTag(Component));
|
||||
@@ -2106,6 +2116,68 @@ function bailoutOnAlreadyFinishedWork(
|
||||
}
|
||||
}
|
||||
|
||||
function remountFiber(
|
||||
current: Fiber,
|
||||
oldWorkInProgress: Fiber,
|
||||
newWorkInProgress: Fiber,
|
||||
): Fiber | null {
|
||||
if (__DEV__) {
|
||||
const returnFiber = oldWorkInProgress.return;
|
||||
if (returnFiber === null) {
|
||||
throw new Error('Cannot swap the root fiber.');
|
||||
}
|
||||
|
||||
// Disconnect from the old current.
|
||||
// It will get deleted.
|
||||
current.alternate = null;
|
||||
oldWorkInProgress.alternate = null;
|
||||
|
||||
// Connect to the new tree.
|
||||
newWorkInProgress.index = oldWorkInProgress.index;
|
||||
newWorkInProgress.sibling = oldWorkInProgress.sibling;
|
||||
newWorkInProgress.return = oldWorkInProgress.return;
|
||||
|
||||
// Replace the child/sibling pointers above it.
|
||||
if (oldWorkInProgress === returnFiber.child) {
|
||||
returnFiber.child = newWorkInProgress;
|
||||
} else {
|
||||
let prevSibling = returnFiber.child;
|
||||
if (prevSibling === null) {
|
||||
throw new Error('Expected parent to have a child.');
|
||||
}
|
||||
while (prevSibling.sibling !== oldWorkInProgress) {
|
||||
prevSibling = prevSibling.sibling;
|
||||
if (prevSibling === null) {
|
||||
throw new Error('Expected to find the previous sibling.');
|
||||
}
|
||||
}
|
||||
prevSibling.sibling = newWorkInProgress;
|
||||
}
|
||||
|
||||
// Delete the old fiber and place the new one.
|
||||
// Since the old fiber is disconnected, we have to schedule it manually.
|
||||
const last = returnFiber.lastEffect;
|
||||
if (last !== null) {
|
||||
last.nextEffect = current;
|
||||
returnFiber.lastEffect = current;
|
||||
} else {
|
||||
returnFiber.firstEffect = returnFiber.lastEffect = current;
|
||||
}
|
||||
current.nextEffect = null;
|
||||
current.effectTag = Deletion;
|
||||
|
||||
newWorkInProgress.effectTag |= Placement;
|
||||
|
||||
// Restart work from the new fiber.
|
||||
return newWorkInProgress;
|
||||
} else {
|
||||
throw new Error(
|
||||
'Did not expect this call in production. ' +
|
||||
'This is a bug in React. Please file an issue.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function beginWork(
|
||||
current: Fiber | null,
|
||||
workInProgress: Fiber,
|
||||
@@ -2113,11 +2185,34 @@ function beginWork(
|
||||
): Fiber | null {
|
||||
const updateExpirationTime = workInProgress.expirationTime;
|
||||
|
||||
if (__DEV__) {
|
||||
if (workInProgress._debugNeedsRemount && current !== null) {
|
||||
// This will restart the begin phase with a new fiber.
|
||||
return remountFiber(
|
||||
current,
|
||||
workInProgress,
|
||||
createFiberFromTypeAndProps(
|
||||
workInProgress.type,
|
||||
workInProgress.key,
|
||||
workInProgress.pendingProps,
|
||||
workInProgress._debugOwner || null,
|
||||
workInProgress.mode,
|
||||
workInProgress.expirationTime,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (current !== null) {
|
||||
const oldProps = current.memoizedProps;
|
||||
const newProps = workInProgress.pendingProps;
|
||||
|
||||
if (oldProps !== newProps || hasLegacyContextChanged()) {
|
||||
if (
|
||||
oldProps !== newProps ||
|
||||
hasLegacyContextChanged() ||
|
||||
// Force a re-render if the implementation changed due to hot reload:
|
||||
(__DEV__ ? workInProgress.type !== current.type : false)
|
||||
) {
|
||||
// If props or context changed, mark the fiber as having performed work.
|
||||
// This may be unset if the props are determined to be equal later (memo).
|
||||
didReceiveUpdate = true;
|
||||
@@ -2256,11 +2351,10 @@ function beginWork(
|
||||
|
||||
switch (workInProgress.tag) {
|
||||
case IndeterminateComponent: {
|
||||
const elementType = workInProgress.elementType;
|
||||
return mountIndeterminateComponent(
|
||||
current,
|
||||
workInProgress,
|
||||
elementType,
|
||||
workInProgress.type,
|
||||
renderExpirationTime,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -189,6 +189,11 @@ let currentHookNameInDev: ?HookType = null;
|
||||
let hookTypesDev: Array<HookType> | null = null;
|
||||
let hookTypesUpdateIndexDev: number = -1;
|
||||
|
||||
// In DEV, this tracks whether currently rendering component needs to ignore
|
||||
// the dependencies for Hooks that need them (e.g. useEffect or useMemo).
|
||||
// When true, such Hooks will always be "remounted". Only used during hot reload.
|
||||
let ignorePreviousDependencies: boolean = false;
|
||||
|
||||
function mountHookTypesDev() {
|
||||
if (__DEV__) {
|
||||
const hookName = ((currentHookNameInDev: any): HookType);
|
||||
@@ -296,6 +301,13 @@ function areHookInputsEqual(
|
||||
nextDeps: Array<mixed>,
|
||||
prevDeps: Array<mixed> | null,
|
||||
) {
|
||||
if (__DEV__) {
|
||||
if (ignorePreviousDependencies) {
|
||||
// Only true when this component is being hot reloaded.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (prevDeps === null) {
|
||||
if (__DEV__) {
|
||||
warning(
|
||||
@@ -352,6 +364,9 @@ export function renderWithHooks(
|
||||
? ((current._debugHookTypes: any): Array<HookType>)
|
||||
: null;
|
||||
hookTypesUpdateIndexDev = -1;
|
||||
// Used for hot reloading:
|
||||
ignorePreviousDependencies =
|
||||
current !== null && current.type !== workInProgress.type;
|
||||
}
|
||||
|
||||
// The following should have already been reset
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
*/
|
||||
|
||||
import type {ReactElement} from 'shared/ReactElementType';
|
||||
import type {Fiber} from './ReactFiber';
|
||||
import type {FiberRoot} from './ReactFiberRoot';
|
||||
|
||||
import {
|
||||
flushSync,
|
||||
scheduleWork,
|
||||
flushPassiveEffects,
|
||||
} from './ReactFiberScheduler';
|
||||
import {Sync} from './ReactFiberExpirationTime';
|
||||
import {
|
||||
FunctionComponent,
|
||||
ForwardRef,
|
||||
MemoComponent,
|
||||
SimpleMemoComponent,
|
||||
} from 'shared/ReactWorkTags';
|
||||
import {REACT_FORWARD_REF_TYPE, REACT_MEMO_TYPE} from 'shared/ReactSymbols';
|
||||
|
||||
type Family = {|
|
||||
currentType: any,
|
||||
currentSignature: null | string,
|
||||
|};
|
||||
|
||||
type HotUpdate = {|
|
||||
familiesByType: WeakMap<any, Family>,
|
||||
root: FiberRoot,
|
||||
staleFamilies: Set<Family>,
|
||||
updatedFamilies: Set<Family>,
|
||||
|};
|
||||
|
||||
let familiesByType: WeakMap<any, Family> | null = null;
|
||||
// $FlowFixMe Flow gets confused by a WeakSet feature check below.
|
||||
let failedBoundaries: WeakSet<Fiber> | null = null;
|
||||
|
||||
export function resolveFunctionForHotReloading(type: any): any {
|
||||
if (__DEV__) {
|
||||
if (familiesByType === null) {
|
||||
// Hot reloading is disabled.
|
||||
return type;
|
||||
}
|
||||
let family = familiesByType.get(type);
|
||||
if (family === undefined) {
|
||||
return type;
|
||||
}
|
||||
// Use the latest known implementation.
|
||||
return family.currentType;
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveForwardRefForHotReloading(type: any): any {
|
||||
if (__DEV__) {
|
||||
if (familiesByType === null) {
|
||||
// Hot reloading is disabled.
|
||||
return type;
|
||||
}
|
||||
let family = familiesByType.get(type);
|
||||
if (family === undefined) {
|
||||
// Check if we're dealing with a real forwardRef. Don't want to crash early.
|
||||
if (
|
||||
type !== null &&
|
||||
type !== undefined &&
|
||||
typeof type.render === 'function'
|
||||
) {
|
||||
// ForwardRef is special because its resolved .type is an object,
|
||||
// but it's possible that we only have its inner render function in the map.
|
||||
// If that inner render function is different, we'll build a new forwardRef type.
|
||||
const currentRender = resolveFunctionForHotReloading(type.render);
|
||||
if (type.render !== currentRender) {
|
||||
const syntheticType = {
|
||||
$$typeof: REACT_FORWARD_REF_TYPE,
|
||||
render: currentRender,
|
||||
};
|
||||
if (type.displayName !== undefined) {
|
||||
(syntheticType: any).displayName = type.displayName;
|
||||
}
|
||||
return syntheticType;
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
// Use the latest known implementation.
|
||||
return family.currentType;
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
export function isCompatibleFamilyForHotReloading(
|
||||
fiber: Fiber,
|
||||
element: ReactElement,
|
||||
): boolean {
|
||||
if (__DEV__) {
|
||||
if (familiesByType === null) {
|
||||
// Hot reloading is disabled.
|
||||
return false;
|
||||
}
|
||||
|
||||
const prevType = fiber.elementType;
|
||||
const nextType = element.type;
|
||||
// If we got here, we know types aren't === equal.
|
||||
let needsCompareFamilies = false;
|
||||
switch (fiber.tag) {
|
||||
case FunctionComponent: {
|
||||
if (typeof nextType === 'function') {
|
||||
needsCompareFamilies = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ForwardRef: {
|
||||
if (
|
||||
typeof nextType === 'object' &&
|
||||
nextType !== null &&
|
||||
nextType.$$typeof === REACT_FORWARD_REF_TYPE
|
||||
) {
|
||||
needsCompareFamilies = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case MemoComponent:
|
||||
case SimpleMemoComponent: {
|
||||
if (
|
||||
typeof nextType === 'object' &&
|
||||
nextType !== null &&
|
||||
nextType.$$typeof === REACT_MEMO_TYPE
|
||||
) {
|
||||
// TODO: if it was but can no longer be simple,
|
||||
// we shouldn't set this.
|
||||
needsCompareFamilies = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// TODO: maybe support lazy?
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if both types have a family and it's the same one.
|
||||
if (needsCompareFamilies) {
|
||||
// Note: memo() and forwardRef() we'll compare outer rather than inner type.
|
||||
// This means both of them need to be registered to preserve state.
|
||||
// If we unwrapped and compared the inner types for wrappers instead,
|
||||
// then we would risk falsely saying two separate memo(Foo)
|
||||
// calls are equivalent because they wrap the same Foo function.
|
||||
const prevFamily = familiesByType.get(prevType);
|
||||
if (
|
||||
prevFamily !== undefined &&
|
||||
prevFamily === familiesByType.get(nextType)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function markFailedErrorBoundaryForHotReloading(fiber: Fiber) {
|
||||
if (__DEV__) {
|
||||
if (familiesByType === null) {
|
||||
// Not hot reloading.
|
||||
return;
|
||||
}
|
||||
if (typeof WeakSet !== 'function') {
|
||||
return;
|
||||
}
|
||||
if (failedBoundaries === null) {
|
||||
failedBoundaries = new WeakSet();
|
||||
}
|
||||
failedBoundaries.add(fiber);
|
||||
}
|
||||
}
|
||||
|
||||
export function scheduleHotUpdate(hotUpdate: HotUpdate): void {
|
||||
if (__DEV__) {
|
||||
// TODO: warn if its identity changes over time?
|
||||
familiesByType = hotUpdate.familiesByType;
|
||||
|
||||
const {root, staleFamilies, updatedFamilies} = hotUpdate;
|
||||
flushPassiveEffects();
|
||||
flushSync(() => {
|
||||
scheduleFibersWithFamiliesRecursively(
|
||||
root.current,
|
||||
updatedFamilies,
|
||||
staleFamilies,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleFibersWithFamiliesRecursively(
|
||||
fiber: Fiber,
|
||||
updatedFamilies: Set<Family>,
|
||||
staleFamilies: Set<Family>,
|
||||
) {
|
||||
if (__DEV__) {
|
||||
const {alternate, child, sibling, tag, type} = fiber;
|
||||
|
||||
let candidateType = null;
|
||||
switch (tag) {
|
||||
case FunctionComponent:
|
||||
case SimpleMemoComponent:
|
||||
candidateType = type;
|
||||
break;
|
||||
case ForwardRef:
|
||||
candidateType = type.render;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (familiesByType === null) {
|
||||
throw new Error('Expected familiesByType to be set during hot reload.');
|
||||
}
|
||||
|
||||
if (candidateType !== null) {
|
||||
const family = familiesByType.get(candidateType);
|
||||
if (family !== undefined) {
|
||||
if (staleFamilies.has(family)) {
|
||||
fiber._debugNeedsRemount = true;
|
||||
scheduleWork(fiber, Sync);
|
||||
} else if (updatedFamilies.has(family)) {
|
||||
scheduleWork(fiber, Sync);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failedBoundaries !== null) {
|
||||
if (
|
||||
failedBoundaries.has(fiber) ||
|
||||
(alternate !== null && failedBoundaries.has(alternate))
|
||||
) {
|
||||
fiber._debugNeedsRemount = true;
|
||||
scheduleWork(fiber, Sync);
|
||||
}
|
||||
}
|
||||
|
||||
if (child !== null) {
|
||||
scheduleFibersWithFamiliesRecursively(
|
||||
child,
|
||||
updatedFamilies,
|
||||
staleFamilies,
|
||||
);
|
||||
}
|
||||
if (sibling !== null) {
|
||||
scheduleFibersWithFamiliesRecursively(
|
||||
sibling,
|
||||
updatedFamilies,
|
||||
staleFamilies,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@ import {StrictMode} from './ReactTypeOfMode';
|
||||
import {Sync} from './ReactFiberExpirationTime';
|
||||
import {revertPassiveEffectsChange} from 'shared/ReactFeatureFlags';
|
||||
import {requestCurrentSuspenseConfig} from './ReactFiberSuspenseConfig';
|
||||
import {scheduleHotUpdate} from './ReactFiberHotReloading';
|
||||
|
||||
type OpaqueRoot = FiberRoot;
|
||||
|
||||
@@ -460,6 +461,7 @@ export function injectIntoDevTools(devToolsConfig: DevToolsConfig): boolean {
|
||||
|
||||
return injectInternals({
|
||||
...devToolsConfig,
|
||||
scheduleHotUpdate: __DEV__ ? scheduleHotUpdate : null,
|
||||
overrideHookState,
|
||||
overrideProps,
|
||||
setSuspenseHandler,
|
||||
|
||||
@@ -56,6 +56,7 @@ import {
|
||||
import {logError} from './ReactFiberCommitWork';
|
||||
import {getStackByFiberInDevAndProd} from './ReactCurrentFiber';
|
||||
import {popHostContainer, popHostContext} from './ReactFiberHostContext';
|
||||
import {markFailedErrorBoundaryForHotReloading} from './ReactFiberHotReloading';
|
||||
import {
|
||||
suspenseStackCursor,
|
||||
InvisibleParentSuspenseContext,
|
||||
@@ -122,6 +123,9 @@ function createClassErrorUpdate(
|
||||
const inst = fiber.stateNode;
|
||||
if (inst !== null && typeof inst.componentDidCatch === 'function') {
|
||||
update.callback = function callback() {
|
||||
if (__DEV__) {
|
||||
markFailedErrorBoundaryForHotReloading(fiber);
|
||||
}
|
||||
if (typeof getDerivedStateFromError !== 'function') {
|
||||
// To preserve the preexisting retry behavior of error boundaries,
|
||||
// we keep track of which ones already failed during this batch.
|
||||
@@ -150,6 +154,10 @@ function createClassErrorUpdate(
|
||||
}
|
||||
}
|
||||
};
|
||||
} else if (__DEV__) {
|
||||
update.callback = () => {
|
||||
markFailedErrorBoundaryForHotReloading(fiber);
|
||||
};
|
||||
}
|
||||
return update;
|
||||
}
|
||||
|
||||
+4
-3
@@ -10,21 +10,22 @@
|
||||
'use strict';
|
||||
|
||||
describe('ReactIncrementalErrorReplay-test', () => {
|
||||
const React = require('react');
|
||||
const ReactTestRenderer = require('react-test-renderer');
|
||||
|
||||
it('copies all keys when stashing potentially failing work', () => {
|
||||
// Note: this test is fragile and relies on internals.
|
||||
// We almost always try to avoid such tests, but here the cost of
|
||||
// the list getting out of sync (and causing subtle bugs in rare cases)
|
||||
// is higher than the cost of maintaining the test.
|
||||
const {
|
||||
// Any Fiber factory function will do.
|
||||
createHostRootFiber,
|
||||
// This is the method we're going to test.
|
||||
// If this is no longer used, you can delete this test file.
|
||||
assignFiberPropertiesInDEV,
|
||||
} = require('../ReactFiber');
|
||||
|
||||
// Get a real fiber.
|
||||
const realFiber = createHostRootFiber(false);
|
||||
const realFiber = ReactTestRenderer.create(<div />).root._currentFiber();
|
||||
const stash = assignFiberPropertiesInDEV(null, realFiber);
|
||||
|
||||
// Verify we get all the same fields.
|
||||
|
||||
Reference in New Issue
Block a user