GeezerVison is showing github.com — Flatten ReactSharedInternals (#28783) · react/react-native@d50323e
File treeExpand file treeCollapse file tree Open diff view settings react-devtools-shared/src react-native-renderer/src react-server-dom-turbopack/src react-server-dom-webpack/src react-suspense-test-utils/src
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-cache/src/ReactCacheOld.js +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,12 +44,11 @@ const Pending = 0; | 44 | 44 | const Resolved = 1; | 45 | 45 | const Rejected = 2; | 46 | 46 |
| 47 | | -const ReactCurrentDispatcher = | 48 | | - React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED | 49 | | - .ReactCurrentDispatcher; | | 47 | +const SharedInternals = | | 48 | + React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; | 50 | 49 |
| 51 | 50 | function readContext(Context: ReactContext<mixed>) { | 52 | | - const dispatcher = ReactCurrentDispatcher.current; | | 51 | + const dispatcher = SharedInternals.H; | 53 | 52 | if (dispatcher === null) { | 54 | 53 | // This wasn't being minified but we're going to retire this package anyway. | 55 | 54 | // eslint-disable-next-line react-internal/prod-error-codes | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/ReactDebugHooks.js +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,7 @@ import { | 37 | 37 | } from 'shared/ReactSymbols'; | 38 | 38 | import hasOwnProperty from 'shared/hasOwnProperty'; | 39 | 39 |
| 40 | | -type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher; | | 40 | +type CurrentDispatcherRef = typeof ReactSharedInternals; | 41 | 41 |
| 42 | 42 | // Used to track hooks called during a render | 43 | 43 |
| @@ -1075,11 +1075,11 @@ export function inspectHooks<Props>( | 1075 | 1075 | // DevTools will pass the current renderer's injected dispatcher. | 1076 | 1076 | // Other apps might compile debug hooks as part of their app though. | 1077 | 1077 | if (currentDispatcher == null) { | 1078 | | - currentDispatcher = ReactSharedInternals.ReactCurrentDispatcher; | | 1078 | + currentDispatcher = ReactSharedInternals; | 1079 | 1079 | } | 1080 | 1080 |
| 1081 | | - const previousDispatcher = currentDispatcher.current; | 1082 | | - currentDispatcher.current = DispatcherProxy; | | 1081 | + const previousDispatcher = currentDispatcher.H; | | 1082 | + currentDispatcher.H = DispatcherProxy; | 1083 | 1083 |
| 1084 | 1084 | let readHookLog; | 1085 | 1085 | let ancestorStackError; | @@ -1093,7 +1093,7 @@ export function inspectHooks<Props>( | 1093 | 1093 | readHookLog = hookLog; | 1094 | 1094 | hookLog = []; | 1095 | 1095 | // $FlowFixMe[incompatible-use] found when upgrading Flow | 1096 | | - currentDispatcher.current = previousDispatcher; | | 1096 | + currentDispatcher.H = previousDispatcher; | 1097 | 1097 | } | 1098 | 1098 | const rootStack = ErrorStackParser.parse(ancestorStackError); | 1099 | 1099 | return buildTree(rootStack, readHookLog); | @@ -1129,9 +1129,9 @@ function inspectHooksOfForwardRef<Props, Ref>( | 1129 | 1129 | ref: Ref, | 1130 | 1130 | currentDispatcher: CurrentDispatcherRef, | 1131 | 1131 | ): HooksTree { | 1132 | | - const previousDispatcher = currentDispatcher.current; | | 1132 | + const previousDispatcher = currentDispatcher.H; | 1133 | 1133 | let readHookLog; | 1134 | | - currentDispatcher.current = DispatcherProxy; | | 1134 | + currentDispatcher.H = DispatcherProxy; | 1135 | 1135 | let ancestorStackError; | 1136 | 1136 | try { | 1137 | 1137 | ancestorStackError = new Error(); | @@ -1141,7 +1141,7 @@ function inspectHooksOfForwardRef<Props, Ref>( | 1141 | 1141 | } finally { | 1142 | 1142 | readHookLog = hookLog; | 1143 | 1143 | hookLog = []; | 1144 | | - currentDispatcher.current = previousDispatcher; | | 1144 | + currentDispatcher.H = previousDispatcher; | 1145 | 1145 | } | 1146 | 1146 | const rootStack = ErrorStackParser.parse(ancestorStackError); | 1147 | 1147 | return buildTree(rootStack, readHookLog); | @@ -1169,7 +1169,7 @@ export function inspectHooksOfFiber( | 1169 | 1169 | // DevTools will pass the current renderer's injected dispatcher. | 1170 | 1170 | // Other apps might compile debug hooks as part of their app though. | 1171 | 1171 | if (currentDispatcher == null) { | 1172 | | - currentDispatcher = ReactSharedInternals.ReactCurrentDispatcher; | | 1172 | + currentDispatcher = ReactSharedInternals; | 1173 | 1173 | } | 1174 | 1174 |
| 1175 | 1175 | if ( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js -33Lines changed: 0 additions & 33 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -453,39 +453,6 @@ describe('ReactHooksInspection', () => { | 453 | 453 | `); | 454 | 454 | }); | 455 | 455 |
| 456 | | - it('should support an injected dispatcher', () => { | 457 | | - const initial = { | 458 | | - useState() { | 459 | | - throw new Error("Should've been proxied"); | 460 | | - }, | 461 | | - }; | 462 | | - let current = initial; | 463 | | - let getterCalls = 0; | 464 | | - const setterCalls = []; | 465 | | - const FakeDispatcherRef = { | 466 | | - get current() { | 467 | | - getterCalls++; | 468 | | - return current; | 469 | | - }, | 470 | | - set current(value) { | 471 | | - setterCalls.push(value); | 472 | | - current = value; | 473 | | - }, | 474 | | - }; | 475 | | - | 476 | | - function Foo(props) { | 477 | | - const [state] = FakeDispatcherRef.current.useState('hello world'); | 478 | | - return <div>{state}</div>; | 479 | | - } | 480 | | - | 481 | | - ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef); | 482 | | - | 483 | | - expect(getterCalls).toBe(2); | 484 | | - expect(setterCalls).toHaveLength(2); | 485 | | - expect(setterCalls[0]).not.toBe(initial); | 486 | | - expect(setterCalls[1]).toBe(initial); | 487 | | - }); | 488 | | - | 489 | 456 | it('should inspect use() calls for Promise and Context', async () => { | 490 | 457 | const MyContext = React.createContext('hi'); | 491 | 458 | const promise = Promise.resolve('world'); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js -55Lines changed: 0 additions & 55 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2345,61 +2345,6 @@ describe('ReactHooksInspectionIntegration', () => { | 2345 | 2345 | `); | 2346 | 2346 | }); | 2347 | 2347 |
| 2348 | | - it('should support an injected dispatcher', async () => { | 2349 | | - function Foo(props) { | 2350 | | - const [state] = React.useState('hello world'); | 2351 | | - return <div>{state}</div>; | 2352 | | - } | 2353 | | - | 2354 | | - const initial = {}; | 2355 | | - let current = initial; | 2356 | | - let getterCalls = 0; | 2357 | | - const setterCalls = []; | 2358 | | - const FakeDispatcherRef = { | 2359 | | - get current() { | 2360 | | - getterCalls++; | 2361 | | - return current; | 2362 | | - }, | 2363 | | - set current(value) { | 2364 | | - setterCalls.push(value); | 2365 | | - current = value; | 2366 | | - }, | 2367 | | - }; | 2368 | | - | 2369 | | - let renderer; | 2370 | | - await act(() => { | 2371 | | - renderer = ReactTestRenderer.create(<Foo />, { | 2372 | | - unstable_isConcurrent: true, | 2373 | | - }); | 2374 | | - }); | 2375 | | - const childFiber = renderer.root._currentFiber(); | 2376 | | - | 2377 | | - let didCatch = false; | 2378 | | - | 2379 | | - try { | 2380 | | - ReactDebugTools.inspectHooksOfFiber(childFiber, FakeDispatcherRef); | 2381 | | - } catch (error) { | 2382 | | - expect(error.message).toBe('Error rendering inspected component'); | 2383 | | - expect(error.cause).toBeInstanceOf(Error); | 2384 | | - expect(error.cause.message).toBe( | 2385 | | - 'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' + | 2386 | | - ' one of the following reasons:\n' + | 2387 | | - '1. You might have mismatching versions of React and the renderer (such as React DOM)\n' + | 2388 | | - '2. You might be breaking the Rules of Hooks\n' + | 2389 | | - '3. You might have more than one copy of React in the same app\n' + | 2390 | | - 'See https://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem.', | 2391 | | - ); | 2392 | | - didCatch = true; | 2393 | | - } | 2394 | | - // avoid false positive if no error was thrown at all | 2395 | | - expect(didCatch).toBe(true); | 2396 | | - | 2397 | | - expect(getterCalls).toBe(1); | 2398 | | - expect(setterCalls).toHaveLength(2); | 2399 | | - expect(setterCalls[0]).not.toBe(initial); | 2400 | | - expect(setterCalls[1]).toBe(initial); | 2401 | | - }); | 2402 | | - | 2403 | 2348 | // This test case is based on an open source bug report: | 2404 | 2349 | // https://github.com/facebookincubator/redux-react-hook/issues/34#issuecomment-466693787 | 2405 | 2350 | it('should properly advance the current hook for useContext', async () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/DevToolsComponentStackFrame.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -86,8 +86,8 @@ export function describeNativeComponentFrame( | 86 | 86 | // Note that unlike the code this was forked from (in ReactComponentStackFrame) | 87 | 87 | // DevTools should override the dispatcher even when DevTools is compiled in production mode, | 88 | 88 | // because the app itself may be in development mode and log errors/warnings. | 89 | | - const previousDispatcher = currentDispatcherRef.current; | 90 | | - currentDispatcherRef.current = null; | | 89 | + const previousDispatcher = currentDispatcherRef.H; | | 90 | + currentDispatcherRef.H = null; | 91 | 91 | disableLogs(); | 92 | 92 |
| 93 | 93 | // NOTE: keep in sync with the implementation in ReactComponentStackFrame | @@ -270,7 +270,7 @@ export function describeNativeComponentFrame( | 270 | 270 |
| 271 | 271 | Error.prepareStackTrace = previousPrepareStackTrace; | 272 | 272 |
| 273 | | - currentDispatcherRef.current = previousDispatcher; | | 273 | + currentDispatcherRef.H = previousDispatcher; | 274 | 274 | reenableLogs(); | 275 | 275 | } | 276 | 276 | // Fallback to just using the name if we couldn't make it throw. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/console.js +7-9Lines changed: 7 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | import type {Fiber} from 'react-reconciler/src/ReactInternalTypes'; | 11 | 11 | import type { | | 12 | + LegacyDispatcherRef, | 12 | 13 | CurrentDispatcherRef, | 13 | 14 | ReactRenderer, | 14 | 15 | WorkTagMap, | 15 | 16 | ConsolePatchSettings, | 16 | 17 | } from './types'; | 17 | 18 | import {format, formatWithStyles} from './utils'; | 18 | 19 |
| 19 | | -import {getInternalReactConstants} from './renderer'; | | 20 | +import {getInternalReactConstants, getDispatcherRef} from './renderer'; | 20 | 21 | import {getStackByFiberInDevAndProd} from './DevToolsFiberComponentStack'; | 21 | 22 | import {consoleManagedByDevToolsDuringStrictMode} from 'react-devtools-feature-flags'; | 22 | 23 | import {castBool, castBrowserTheme} from '../utils'; | @@ -75,7 +76,7 @@ type OnErrorOrWarning = ( | 75 | 76 | const injectedRenderers: Map< | 76 | 77 | ReactRenderer, | 77 | 78 | { | 78 | | - currentDispatcherRef: CurrentDispatcherRef, | | 79 | + currentDispatcherRef: LegacyDispatcherRef | CurrentDispatcherRef, | 79 | 80 | getCurrentFiber: () => Fiber | null, | 80 | 81 | onErrorOrWarning: ?OnErrorOrWarning, | 81 | 82 | workTagMap: WorkTagMap, | @@ -215,12 +216,9 @@ export function patch({ | 215 | 216 | // Search for the first renderer that has a current Fiber. | 216 | 217 | // We don't handle the edge case of stacks for more than one (e.g. interleaved renderers?) | 217 | 218 | // eslint-disable-next-line no-for-of-loops/no-for-of-loops | 218 | | - for (const { | 219 | | - currentDispatcherRef, | 220 | | - getCurrentFiber, | 221 | | - onErrorOrWarning, | 222 | | - workTagMap, | 223 | | - } of injectedRenderers.values()) { | | 219 | + for (const renderer of injectedRenderers.values()) { | | 220 | + const currentDispatcherRef = getDispatcherRef(renderer); | | 221 | + const {getCurrentFiber, onErrorOrWarning, workTagMap} = renderer; | 224 | 222 | const current: ?Fiber = getCurrentFiber(); | 225 | 223 | if (current != null) { | 226 | 224 | try { | @@ -241,7 +239,7 @@ export function patch({ | 241 | 239 | const componentStack = getStackByFiberInDevAndProd( | 242 | 240 | workTagMap, | 243 | 241 | current, | 244 | | - currentDispatcherRef, | | 242 | + (currentDispatcherRef: any), | 245 | 243 | ); | 246 | 244 | if (componentStack !== '') { | 247 | 245 | if (isStrictModeOverride(args, method)) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/renderer.js +30-6Lines changed: 30 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -119,6 +119,8 @@ import type { | 119 | 119 | RendererInterface, | 120 | 120 | SerializedElement, | 121 | 121 | WorkTagMap, | | 122 | + CurrentDispatcherRef, | | 123 | + LegacyDispatcherRef, | 122 | 124 | } from './types'; | 123 | 125 | import type { | 124 | 126 | ComponentFilter, | @@ -140,6 +142,31 @@ type ReactPriorityLevelsType = { | 140 | 142 | NoPriority: number, | 141 | 143 | }; | 142 | 144 |
| | 145 | +export function getDispatcherRef(renderer: { | | 146 | + +currentDispatcherRef?: LegacyDispatcherRef | CurrentDispatcherRef, | | 147 | + ... | | 148 | +}): void | CurrentDispatcherRef { | | 149 | + if (renderer.currentDispatcherRef === undefined) { | | 150 | + return undefined; | | 151 | + } | | 152 | + const injectedRef = renderer.currentDispatcherRef; | | 153 | + if ( | | 154 | + typeof injectedRef.H === 'undefined' && | | 155 | + typeof injectedRef.current !== 'undefined' | | 156 | + ) { | | 157 | + // We got a legacy dispatcher injected, let's create a wrapper proxy to translate. | | 158 | + return { | | 159 | + get H() { | | 160 | + return (injectedRef: any).current; | | 161 | + }, | | 162 | + set H(value) { | | 163 | + (injectedRef: any).current = value; | | 164 | + }, | | 165 | + }; | | 166 | + } | | 167 | + return (injectedRef: any); | | 168 | +} | | 169 | + | 143 | 170 | function getFiberFlags(fiber: Fiber): number { | 144 | 171 | // The name of this field changed from "effectTag" to "flags" | 145 | 172 | return fiber.flags !== undefined ? fiber.flags : (fiber: any).effectTag; | @@ -694,7 +721,7 @@ export function attach( | 694 | 721 | getDisplayNameForFiber, | 695 | 722 | getIsProfiling: () => isProfiling, | 696 | 723 | getLaneLabelMap, | 697 | | - currentDispatcherRef: renderer.currentDispatcherRef, | | 724 | + currentDispatcherRef: getDispatcherRef(renderer), | 698 | 725 | workTagMap: ReactTypeOfWork, | 699 | 726 | reactVersion: version, | 700 | 727 | }); | @@ -3344,10 +3371,7 @@ export function attach( | 3344 | 3371 | } | 3345 | 3372 |
| 3346 | 3373 | try { | 3347 | | - hooks = inspectHooksOfFiber( | 3348 | | - fiber, | 3349 | | - (renderer.currentDispatcherRef: any), | 3350 | | - ); | | 3374 | + hooks = inspectHooksOfFiber(fiber, getDispatcherRef(renderer)); | 3351 | 3375 | } finally { | 3352 | 3376 | // Restore original console functionality. | 3353 | 3377 | for (const method in originalConsoleMethods) { | @@ -4571,7 +4595,7 @@ export function attach( | 4571 | 4595 | function getComponentStackForFiber(fiber: Fiber): string | null { | 4572 | 4596 | let componentStack = fiberToComponentStackMap.get(fiber); | 4573 | 4597 | if (componentStack == null) { | 4574 | | - const dispatcherRef = renderer.currentDispatcherRef; | | 4598 | + const dispatcherRef = getDispatcherRef(renderer); | 4575 | 4599 | if (dispatcherRef == null) { | 4576 | 4600 | return null; | 4577 | 4601 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/types.js +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,7 +87,12 @@ export type NativeType = Object; | 87 | 87 | export type RendererID = number; | 88 | 88 |
| 89 | 89 | type Dispatcher = any; | 90 | | -export type CurrentDispatcherRef = {current: null | Dispatcher}; | | 90 | +export type LegacyDispatcherRef = {current: null | Dispatcher}; | | 91 | +type SharedInternalsSubset = { | | 92 | + H: null | Dispatcher, | | 93 | + ... | | 94 | +}; | | 95 | +export type CurrentDispatcherRef = SharedInternalsSubset; | 91 | 96 |
| 92 | 97 | export type GetDisplayNameForFiberID = ( | 93 | 98 | id: number, | @@ -155,7 +160,7 @@ export type ReactRenderer = { | 155 | 160 | scheduleUpdate?: ?(fiber: Object) => void, | 156 | 161 | setSuspenseHandler?: ?(shouldSuspend: (fiber: Object) => boolean) => void, | 157 | 162 | // Only injected by React v16.8+ in order to support hooks inspection. | 158 | | - currentDispatcherRef?: CurrentDispatcherRef, | | 163 | + currentDispatcherRef?: LegacyDispatcherRef | CurrentDispatcherRef, | 159 | 164 | // Only injected by React v16.9+ in DEV mode. | 160 | 165 | // Enables DevTools to append owners-only component stack to error messages. | 161 | 166 | getCurrentFiber?: () => Fiber | null, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/devtools/cache.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,11 +59,11 @@ const Pending = 0; | 59 | 59 | const Resolved = 1; | 60 | 60 | const Rejected = 2; | 61 | 61 |
| 62 | | -const ReactCurrentDispatcher = (React: any) | 63 | | - .__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher; | | 62 | +const ReactSharedInternals = (React: any) | | 63 | + .__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; | 64 | 64 |
| 65 | 65 | function readContext(Context: ReactContext<null>) { | 66 | | - const dispatcher = ReactCurrentDispatcher.current; | | 66 | + const dispatcher = ReactSharedInternals.H; | 67 | 67 | if (dispatcher === null) { | 68 | 68 | throw new Error( | 69 | 69 | 'react-cache: read and preload may only be called from within a ' + | |
|