-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose injectIntoDevTools() to renderers #11463
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import { | |
processChildContext, | ||
} from './ReactFiberContext'; | ||
import {createFiberRoot} from './ReactFiberRoot'; | ||
import * as ReactFiberDevToolsHook from './ReactFiberDevToolsHook'; | ||
import ReactFiberScheduler from './ReactFiberScheduler'; | ||
import {insertUpdateIntoFiber} from './ReactFiberUpdateQueue'; | ||
import ReactFiberInstrumentation from './ReactFiberInstrumentation'; | ||
|
@@ -211,6 +212,23 @@ type HydrationHostConfig<T, P, I, TI, C, CX, PL> = { | |
): void, | ||
}; | ||
|
||
// 0 is PROD, 1 is DEV. | ||
// Might add PROFILE later. | ||
type BundleType = 0 | 1; | ||
|
||
type DevToolsConfig<I, TI> = {| | ||
bundleType: BundleType, | ||
version: string, | ||
rendererPackageName: string, | ||
// Note: this actually *does* depend on Fiber internal fields. | ||
// Used by "inspect clicked DOM element" in React DevTools. | ||
findFiberByHostInstance?: (instance: I | TI) => Fiber, | ||
// Used by RN in-app inspector. | ||
// This API is unfortunately RN-specific. | ||
// TODO: Change it to accept Fiber instead and type it properly. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
getInspectorDataForViewTag?: (tag: number) => Object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remind me where it is used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It used to be part of the devtools config I think. https://github.com/facebook/react-devtools/blob/ef493abba0d08c0243bdb1f2183edc797d5d366c/packages/react-devtools-core/src/backend.js#L13-L18 I think the usage was so that an app could indicate whether it should take over the devtools connection or not, but not really sure. Or maybe to return false while still starting up (according to this comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not required. I added it for RN so that multiple apps on simulator don't steal devtools from each other. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it's part of the options to This is not related to the |
||
|}; | ||
|
||
export type Reconciler<C, I, TI> = { | ||
createContainer(containerInfo: C, hydrate: boolean): OpaqueRoot, | ||
updateContainer( | ||
|
@@ -223,6 +241,7 @@ export type Reconciler<C, I, TI> = { | |
unbatchedUpdates<A>(fn: () => A): A, | ||
flushSync<A>(fn: () => A): A, | ||
deferredUpdates<A>(fn: () => A): A, | ||
injectIntoDevTools(devToolsConfig: DevToolsConfig<I, TI>): boolean, | ||
|
||
// Used to extract the return value from the initial render. Legacy API. | ||
getPublicRootInstance( | ||
|
@@ -327,6 +346,14 @@ export default function<T, P, I, TI, PI, C, CC, CX, PL>( | |
scheduleWork(current, expirationTime); | ||
} | ||
|
||
function findHostInstance(fiber: Fiber): PI | null { | ||
const hostFiber = findCurrentHostFiber(fiber); | ||
if (hostFiber === null) { | ||
return null; | ||
} | ||
return hostFiber.stateNode; | ||
} | ||
|
||
return { | ||
createContainer(containerInfo: C, hydrate: boolean): OpaqueRoot { | ||
return createFiberRoot(containerInfo, hydrate); | ||
|
@@ -386,13 +413,7 @@ export default function<T, P, I, TI, PI, C, CC, CX, PL>( | |
} | ||
}, | ||
|
||
findHostInstance(fiber: Fiber): PI | null { | ||
const hostFiber = findCurrentHostFiber(fiber); | ||
if (hostFiber === null) { | ||
return null; | ||
} | ||
return hostFiber.stateNode; | ||
}, | ||
findHostInstance, | ||
|
||
findHostInstanceWithNoPortals(fiber: Fiber): PI | null { | ||
const hostFiber = findCurrentHostFiberWithNoPortals(fiber); | ||
|
@@ -401,5 +422,22 @@ export default function<T, P, I, TI, PI, C, CC, CX, PL>( | |
} | ||
return hostFiber.stateNode; | ||
}, | ||
|
||
injectIntoDevTools(devToolsConfig: DevToolsConfig<I, TI>): boolean { | ||
const {findFiberByHostInstance} = devToolsConfig; | ||
return ReactFiberDevToolsHook.injectInternals({ | ||
...devToolsConfig, | ||
findHostInstanceByFiber(fiber: Fiber): I | TI | null { | ||
return findHostInstance(fiber); | ||
}, | ||
findFiberByHostInstance(instance: I | TI): Fiber | null { | ||
if (!findFiberByHostInstance) { | ||
// Might not be implemented by the renderer. | ||
return null; | ||
} | ||
return findFiberByHostInstance(instance); | ||
}, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the wrapper functions necessary? Couldn't we just do... injectIntoDevTools(devToolsConfig: DevToolsConfig<I, TI>): boolean {
const {findFiberByHostInstance} = devToolsConfig;
const findFiberByHostInstanceImpl = findFiberByHostInstance
? findFiberByHostInstance
: () => null;
return ReactFiberDevToolsHook.injectInternals({
...devToolsConfig,
findHostInstanceByFiber: findHostInstance,
findFiberByHostInstance: findFiberByHostInstanceImpl,
});
}, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would work too. I don't feel strongly as they're only called once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried using your approach, but it seems like Flow fails to catch some issues for some reason now. I'd prefer keeping it explicit so it's better typed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boo, Flow. Okay. No biggie. |
||
}, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the devtools integration used to work without this method 🤔
With this being required I think we’d also need
ReactFiberTreeReflection.findCurrentHostFiber
andReactFiberTreeReflection.findCurrentHostFiberWithNoPortals
also exposed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need them. This one doesn't have to be the current one. So as long as you save the fiber on some field on the instance (or use a weakmap), you can get one back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still does. The implementation (below) is:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's also true. (Though it's nice to implement if you can I guess.)