Skip to content

Commit

Permalink
refactor[RendererInterface]: expose onErrorOrWarning and getComponent…
Browse files Browse the repository at this point in the history
…Stack (#30931)

Make `onErrorOrWarning` and `getComponentStack` part of
`rendererInterface`. By doing this, they will be available from the
global hook `rendererInterfaces` Map. This makes them available to be
used by Hook, which soon will be the only one who is doing console
patching.

This is also a pre-requisite for removing `registerRenderer`:

https://github.com/facebook/react/blob/d160aa0fbb1bd2d00ea8c771c551c9cb5b47f1e9/packages/react-devtools-shared/src/backend/console.js#L113-L121
  • Loading branch information
hoxyq authored Sep 10, 2024
1 parent 63cefa2 commit 3dfd5d9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/react-devtools-shared/src/backend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* @flow
*/

import type {ConsolePatchSettings} from './types';
import type {
ConsolePatchSettings,
OnErrorOrWarning,
GetComponentStack,
} from './types';

import {
formatConsoleArguments,
Expand Down Expand Up @@ -75,11 +79,6 @@ function restorePotentiallyModifiedArgs(args: Array<any>): Array<any> {
}
}

type OnErrorOrWarning = (type: 'error' | 'warn', args: Array<any>) => void;
type GetComponentStack = (
topFrame: Error,
) => null | {enableOwnerStacks: boolean, componentStack: string};

const injectedRenderers: Array<{
onErrorOrWarning: ?OnErrorOrWarning,
getComponentStack: ?GetComponentStack,
Expand Down
2 changes: 2 additions & 0 deletions packages/react-devtools-shared/src/backend/fiber/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5789,8 +5789,10 @@ export function attach(
inspectElement,
logElementToConsole,
patchConsoleForStrictMode,
getComponentStack,
getElementAttributeByPath,
getElementSourceFunctionById,
onErrorOrWarning,
overrideError,
overrideSuspense,
overrideValueAtPath,
Expand Down
2 changes: 2 additions & 0 deletions packages/react-devtools-shared/src/backend/flight/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export function attach(
getBestMatchForTrackedPath() {
return null;
},
getComponentStack,
getDisplayNameForElementID() {
return null;
},
Expand Down Expand Up @@ -207,6 +208,7 @@ export function attach(
patchConsoleForStrictMode() {},
getElementAttributeByPath() {},
getElementSourceFunctionById() {},
onErrorOrWarning,
overrideError() {},
overrideSuspense() {},
overrideValueAtPath() {},
Expand Down
10 changes: 10 additions & 0 deletions packages/react-devtools-shared/src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ export type InstanceAndStyle = {

type Type = 'props' | 'hooks' | 'state' | 'context';

export type OnErrorOrWarning = (
type: 'error' | 'warn',
args: Array<any>,
) => void;
export type GetComponentStack = (
topFrame: Error,
) => null | {enableOwnerStacks: boolean, componentStack: string};

export type RendererInterface = {
cleanup: () => void,
clearErrorsAndWarnings: () => void,
Expand All @@ -364,6 +372,7 @@ export type RendererInterface = {
findHostInstancesForElementID: FindHostInstancesForElementID,
flushInitialOperations: () => void,
getBestMatchForTrackedPath: () => PathMatch | null,
getComponentStack?: GetComponentStack,
getNearestMountedDOMNode: (component: Element) => Element | null,
getElementIDForHostInstance: GetElementIDForHostInstance,
getDisplayNameForElementID: GetDisplayNameForElementID,
Expand All @@ -386,6 +395,7 @@ export type RendererInterface = {
forceFullData: boolean,
) => InspectedElementPayload,
logElementToConsole: (id: number) => void,
onErrorOrWarning?: OnErrorOrWarning,
overrideError: (id: number, forceError: boolean) => void,
overrideSuspense: (id: number, forceFallback: boolean) => void,
overrideValueAtPath: (
Expand Down

0 comments on commit 3dfd5d9

Please sign in to comment.