Skip to content

Commit

Permalink
Account for another DevTools + Fast Refresh edge case
Browse files Browse the repository at this point in the history
DevTools now 'untrack' Fibers (cleans up the ID-to-Fiber mapping) after a slight delay in order to support a Fast Refresh edge case:
1. Component type is updated and Fast Refresh schedules an update+remount.
2. flushPendingErrorsAndWarningsAfterDelay() runs, sees the old Fiber is no longer mounted (it's been disconnected by Fast Refresh), and calls untrackFiberID() to clear it from the Map.
3. React flushes pending passive effects before it runs the next render, which logs an error or warning, which causes a new ID to be generated for this Fiber.
4. DevTools now tries to unmount the old Component with the new ID.

The underlying problem here is the premature clearing of the Fiber ID, but DevTools has no way to detect that a given Fiber has been scheduled for Fast Refresh. (The '_debugNeedsRemount' flag won't necessarily be set.)

The best we can do is to delay untracking by a small amount, and give React time to process the Fast Refresh delay.
  • Loading branch information
Brian Vaughn committed May 18, 2021
1 parent 343776f commit 6b3d0ce
Showing 1 changed file with 80 additions and 16 deletions.
96 changes: 80 additions & 16 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ export function attach(
? getFiberIDUnsafe(parentFiber) || '<no-id>'
: '';

console.log(
console.groupCollapsed(
`[renderer] %c${name} %c${displayName} (${maybeID}) %c${
parentFiber ? `${parentDisplayName} (${maybeParentID})` : ''
} %c${extraString}`,
Expand All @@ -726,6 +726,13 @@ export function attach(
'color: purple;',
'color: black;',
);
console.log(
new Error().stack
.split('\n')
.slice(1)
.join('\n'),
);
console.groupEnd();
}
};

Expand Down Expand Up @@ -996,7 +1003,9 @@ export function attach(
}
}

let didGenerateID = false;
if (id === null) {
didGenerateID = true;
id = getUID();
}

Expand All @@ -1019,6 +1028,17 @@ export function attach(
}
}

if (__DEBUG__) {
if (didGenerateID) {
debug(
'getOrGenerateFiberID()',
fiber,
fiber.return,
'Generated a new UID',
);
}
}

return refinedID;
}

Expand Down Expand Up @@ -1050,17 +1070,61 @@ export function attach(
// Removes a Fiber (and its alternate) from the Maps used to track their id.
// This method should always be called when a Fiber is unmounting.
function untrackFiberID(fiber: Fiber) {
const fiberID = getFiberIDUnsafe(fiber);
if (fiberID !== null) {
idToArbitraryFiberMap.delete(fiberID);
if (__DEBUG__) {
debug('untrackFiberID()', fiber, fiber.return, 'schedule after delay');
}

fiberToIDMap.delete(fiber);
// Untrack Fibers after a slight delay in order to support a Fast Refresh edge case:
// 1. Component type is updated and Fast Refresh schedules an update+remount.
// 2. flushPendingErrorsAndWarningsAfterDelay() runs, sees the old Fiber is no longer mounted
// (it's been disconnected by Fast Refresh), and calls untrackFiberID() to clear it from the Map.
// 3. React flushes pending passive effects before it runs the next render,
// which logs an error or warning, which causes a new ID to be generated for this Fiber.
// 4. DevTools now tries to unmount the old Component with the new ID.
//
// The underlying problem here is the premature clearing of the Fiber ID,
// but DevTools has no way to detect that a given Fiber has been scheduled for Fast Refresh.
// (The "_debugNeedsRemount" flag won't necessarily be set.)
//
// The best we can do is to delay untracking by a small amount,
// and give React time to process the Fast Refresh delay.

const {alternate} = fiber;
if (alternate !== null) {
fiberToIDMap.delete(alternate);
untrackFibersSet.add(fiber);

if (untrackFibersTimeoutID !== null) {
untrackFibersTimeoutID = setTimeout(untrackFibers, 1000);
}
}

const untrackFibersSet: Set<Fiber> = new Set();
let untrackFibersTimeoutID: TimeoutID | null = null;

function untrackFibers() {
untrackFibersTimeoutID = null;

if (__DEBUG__) {
console.log(
'untrackFibers() after delay:',
Array.from(untrackFibersSet)
.map(getFiberIDUnsafe)
.join(','),
);
}

untrackFibersSet.forEach(fiber => {
const fiberID = getFiberIDUnsafe(fiber);
if (fiberID !== null) {
idToArbitraryFiberMap.delete(fiberID);
}

fiberToIDMap.delete(fiber);

const {alternate} = fiber;
if (alternate !== null) {
fiberToIDMap.delete(alternate);
}
});
untrackFibersSet.clear();
}

function getChangeDescription(
Expand Down Expand Up @@ -1607,13 +1671,13 @@ export function attach(
}

function recordMount(fiber: Fiber, parentFiber: Fiber | null) {
const isRoot = fiber.tag === HostRoot;
const id = getOrGenerateFiberID(fiber);

if (__DEBUG__) {
debug('recordMount()', fiber, parentFiber);
}

const isRoot = fiber.tag === HostRoot;
const id = getOrGenerateFiberID(fiber);

const hasOwnerMetadata = fiber.hasOwnProperty('_debugOwner');
const isProfilingSupported = fiber.hasOwnProperty('treeBaseDuration');

Expand Down Expand Up @@ -1745,6 +1809,9 @@ export function attach(
// This reduces the chance of stack overflow for wide trees (e.g. lists with many items).
let fiber: Fiber | null = firstChild;
while (fiber !== null) {
// Generate an ID even for filtered Fibers, in case it's needed later (e.g. for Profiling).
getOrGenerateFiberID(fiber);

if (__DEBUG__) {
debug('mountFiberRecursively()', fiber, parentFiber);
}
Expand All @@ -1758,9 +1825,6 @@ export function attach(
const shouldIncludeInTree = !shouldFilterFiber(fiber);
if (shouldIncludeInTree) {
recordMount(fiber, parentFiber);
} else {
// Generate an ID even for filtered Fibers, in case it's needed later (e.g. for Profiling).
getOrGenerateFiberID(fiber);
}

if (traceUpdatesEnabled) {
Expand Down Expand Up @@ -2005,12 +2069,12 @@ export function attach(
parentFiber: Fiber | null,
traceNearestHostComponentUpdate: boolean,
): boolean {
const id = getOrGenerateFiberID(nextFiber);

if (__DEBUG__) {
debug('updateFiberRecursively()', nextFiber, parentFiber);
}

const id = getOrGenerateFiberID(nextFiber);

if (traceUpdatesEnabled) {
const elementType = getElementTypeForFiber(nextFiber);
if (traceNearestHostComponentUpdate) {
Expand Down

0 comments on commit 6b3d0ce

Please sign in to comment.