Skip to content

Commit

Permalink
update debug()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Sep 4, 2024
1 parent e974bda commit 7768190
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions packages/react-devtools-shared/src/backend/fiber/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,10 @@ export function attach(
// even if objects are different
const message = formatConsoleArgumentsToSingleString(...args);
if (__DEBUG__) {
debug('onErrorOrWarning', fiber, null, `${type}: "${message}"`);
const fiberInstance = fiberToFiberInstanceMap.get(fiber);
if (fiberInstance !== undefined) {
debug('onErrorOrWarning', fiberInstance, null, `${type}: "${message}"`);
}
}

// Mark this Fiber as needed its warning/error count updated during the next flush.
Expand Down Expand Up @@ -1131,32 +1134,37 @@ export function attach(
// It relies on the extension to pass the preference through via the global.
patchConsoleUsingWindowValues();

const debug = (
function debug(
name: string,
fiber: Fiber,
instance: DevToolsInstance,
parentInstance: null | DevToolsInstance,
extraString: string = '',
): void => {
): void {
if (__DEBUG__) {
const displayName =
fiber.tag + ':' + (getDisplayNameForFiber(fiber) || 'null');

const maybeID = getFiberIDUnsafe(fiber) || '<no id>';

let parentDisplayName;
let maybeParentID;
if (parentInstance !== null && parentInstance.kind === FIBER_INSTANCE) {
const parentFiber = parentInstance.data;
parentDisplayName =
parentFiber.tag +
':' +
(getDisplayNameForFiber(parentFiber) || 'null');
maybeParentID = String(parentInstance.id);
} else {
// TODO: Handle VirtualInstance
parentDisplayName = '';
maybeParentID = '<no-id>';
}
instance.kind === VIRTUAL_INSTANCE
? instance.data.name || 'null'
: instance.data.tag +
':' +
(getDisplayNameForFiber(instance.data) || 'null');

const maybeID =
instance.kind === FILTERED_FIBER_INSTANCE ? '<no id>' : instance.id;

const parentDisplayName =
parentInstance === null
? ''
: parentInstance.kind === VIRTUAL_INSTANCE
? parentInstance.data.name || 'null'
: parentInstance.data.tag +
':' +
(getDisplayNameForFiber(parentInstance.data) || 'null');

const maybeParentID =
parentInstance === null ||
parentInstance.kind === FILTERED_FIBER_INSTANCE
? '<no id>'
: parentInstance.id;

console.groupCollapsed(
`[renderer] %c${name} %c${displayName} (${maybeID}) %c${
Expand All @@ -1170,7 +1178,7 @@ export function attach(
console.log(new Error().stack.split('\n').slice(1).join('\n'));
console.groupEnd();
}
};
}

// eslint-disable-next-line no-unused-vars
function debugTree(instance: DevToolsInstance, indent: number = 0) {
Expand Down Expand Up @@ -1570,9 +1578,6 @@ 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 untrackFiber(nearestInstance: DevToolsInstance, fiber: Fiber) {
if (__DEBUG__) {
debug('untrackFiber()', fiber, null);
}
// TODO: Consider using a WeakMap instead. The only thing where that doesn't work
// is React Native Paper which tracks tags but that support is eventually going away
// and can use the old findFiberByHostInstance strategy.
Expand Down Expand Up @@ -2246,7 +2251,7 @@ export function attach(
const id = fiberInstance.id;

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

const isProfilingSupported = fiber.hasOwnProperty('treeBaseDuration');
Expand Down Expand Up @@ -2423,7 +2428,7 @@ export function attach(
function recordUnmount(fiberInstance: FiberInstance): void {
const fiber = fiberInstance.data;
if (__DEBUG__) {
debug('recordUnmount()', fiber, null);
debug('recordUnmount()', fiberInstance, reconcilingParent);
}

if (trackedPathMatchInstance === fiberInstance) {
Expand Down Expand Up @@ -2758,15 +2763,14 @@ export function attach(
fiber: Fiber,
traceNearestHostComponentUpdate: boolean,
): void {
if (__DEBUG__) {
debug('mountFiberRecursively()', fiber, reconcilingParent);
}

const shouldIncludeInTree = !shouldFilterFiber(fiber);
let newInstance = null;
if (shouldIncludeInTree) {
newInstance = recordMount(fiber, reconcilingParent);
insertChild(newInstance);
if (__DEBUG__) {
debug('mountFiberRecursively()', newInstance, reconcilingParent);
}
} else if (
reconcilingParent !== null &&
reconcilingParent.kind === VIRTUAL_INSTANCE
Expand All @@ -2786,6 +2790,9 @@ export function attach(

newInstance = createFilteredFiberInstance(fiber);
insertChild(newInstance);
if (__DEBUG__) {
debug('mountFiberRecursively()', newInstance, reconcilingParent);
}
}

// If we have the tree selection from previous reload, try to match this Fiber.
Expand Down Expand Up @@ -2899,9 +2906,7 @@ export function attach(
// when we switch from primary to fallback, or deleting a subtree.
function unmountInstanceRecursively(instance: DevToolsInstance) {
if (__DEBUG__) {
if (instance.kind === FIBER_INSTANCE) {
debug('unmountInstanceRecursively()', instance.data, null);
}
debug('unmountInstanceRecursively()', instance, reconcilingParent);
}

const stashedParent = reconcilingParent;
Expand Down Expand Up @@ -3035,13 +3040,10 @@ export function attach(
parentInstance: FiberInstance | VirtualInstance,
) {
if (__DEBUG__) {
if (
parentInstance.firstChild !== null &&
parentInstance.firstChild.kind === FIBER_INSTANCE
) {
if (parentInstance.firstChild !== null) {
debug(
'recordResetChildren()',
parentInstance.firstChild.data,
parentInstance.firstChild,
parentInstance,
);
}
Expand Down Expand Up @@ -3418,7 +3420,9 @@ export function attach(
traceNearestHostComponentUpdate: boolean,
): boolean {
if (__DEBUG__) {
debug('updateFiberRecursively()', nextFiber, reconcilingParent);
if (fiberInstance !== null) {
debug('updateFiberRecursively()', fiberInstance, reconcilingParent);
}
}

if (traceUpdatesEnabled) {
Expand Down

0 comments on commit 7768190

Please sign in to comment.