@samitouri / QOS-React / commits / 3dfd5d9efb

refactor[RendererInterface]: expose onErrorOrWarning and getComponentStack (#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

Ruslan Lesiutin committed Sep 10, 2024 at 15:59 UTC 3dfd5d9efb8fb56c4f602915c0e38a84b0e956db
4 files changed +19 -6
packages/react-devtools-shared/src/backend/console.js
+5 -6
@@ -7,7 +7,11 @@
7 * @flow
8 */
9
10 -import type {ConsolePatchSettings} from './types';
10 +import type {
11 + ConsolePatchSettings,
12 + OnErrorOrWarning,
13 + GetComponentStack,
14 +} from './types';
15
16 import {
17 formatConsoleArguments,
@@ -75,11 +79,6 @@ function restorePotentiallyModifiedArgs(args: Array<any>): Array<any> {
79 }
80 }
81
78 -type OnErrorOrWarning = (type: 'error' | 'warn', args: Array<any>) => void;
79 -type GetComponentStack = (
80 - topFrame: Error,
81 -) => null | {enableOwnerStacks: boolean, componentStack: string};
82 -
82 const injectedRenderers: Array<{
83 onErrorOrWarning: ?OnErrorOrWarning,
84 getComponentStack: ?GetComponentStack,
packages/react-devtools-shared/src/backend/fiber/renderer.js
+2
@@ -5789,8 +5789,10 @@ export function attach(
5789 inspectElement,
5790 logElementToConsole,
5791 patchConsoleForStrictMode,
5792 + getComponentStack,
5793 getElementAttributeByPath,
5794 getElementSourceFunctionById,
5795 + onErrorOrWarning,
5796 overrideError,
5797 overrideSuspense,
5798 overrideValueAtPath,
packages/react-devtools-shared/src/backend/flight/renderer.js
+2
@@ -162,6 +162,7 @@ export function attach(
162 getBestMatchForTrackedPath() {
163 return null;
164 },
165 + getComponentStack,
166 getDisplayNameForElementID() {
167 return null;
168 },
@@ -207,6 +208,7 @@ export function attach(
208 patchConsoleForStrictMode() {},
209 getElementAttributeByPath() {},
210 getElementSourceFunctionById() {},
211 + onErrorOrWarning,
212 overrideError() {},
213 overrideSuspense() {},
214 overrideValueAtPath() {},
packages/react-devtools-shared/src/backend/types.js
+10
@@ -350,6 +350,14 @@ export type InstanceAndStyle = {
350
351 type Type = 'props' | 'hooks' | 'state' | 'context';
352
353 +export type OnErrorOrWarning = (
354 + type: 'error' | 'warn',
355 + args: Array<any>,
356 +) => void;
357 +export type GetComponentStack = (
358 + topFrame: Error,
359 +) => null | {enableOwnerStacks: boolean, componentStack: string};
360 +
361 export type RendererInterface = {
362 cleanup: () => void,
363 clearErrorsAndWarnings: () => void,
@@ -364,6 +372,7 @@ export type RendererInterface = {
372 findHostInstancesForElementID: FindHostInstancesForElementID,
373 flushInitialOperations: () => void,
374 getBestMatchForTrackedPath: () => PathMatch | null,
375 + getComponentStack?: GetComponentStack,
376 getNearestMountedDOMNode: (component: Element) => Element | null,
377 getElementIDForHostInstance: GetElementIDForHostInstance,
378 getDisplayNameForElementID: GetDisplayNameForElementID,
@@ -386,6 +395,7 @@ export type RendererInterface = {
395 forceFullData: boolean,
396 ) => InspectedElementPayload,
397 logElementToConsole: (id: number) => void,
398 + onErrorOrWarning?: OnErrorOrWarning,
399 overrideError: (id: number, forceError: boolean) => void,
400 overrideSuspense: (id: number, forceFallback: boolean) => void,
401 overrideValueAtPath: (