-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Extracted definition and access to public instances to a separate module in Fabric #26321
Merged
sammy-SC
merged 1 commit into
facebook:main
from
rubennorte:extract-react-fabric-public-instance-handling-to-module
Mar 13, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,13 @@ | |
* @flow | ||
*/ | ||
|
||
import type {ElementRef} from 'react'; | ||
import type { | ||
HostComponent, | ||
MeasureInWindowOnSuccessCallback, | ||
MeasureLayoutOnSuccessCallback, | ||
MeasureOnSuccessCallback, | ||
INativeMethods, | ||
ViewConfig, | ||
TouchedViewDataAtPoint, | ||
} from './ReactNativeTypes'; | ||
|
||
import {warnForStyleProps} from './NativeMethodsMixinUtils'; | ||
import type {TouchedViewDataAtPoint, ViewConfig} from './ReactNativeTypes'; | ||
import { | ||
createPublicInstance, | ||
type ReactFabricHostComponent, | ||
} from './ReactFabricPublicInstance'; | ||
import {create, diff} from './ReactNativeAttributePayload'; | ||
|
||
import {dispatchEvent} from './ReactFabricEventEmitter'; | ||
|
||
import { | ||
DefaultEventPriority, | ||
DiscreteEventPriority, | ||
|
@@ -31,7 +22,6 @@ import { | |
// Modules provided by RN: | ||
import { | ||
ReactNativeViewConfigRegistry, | ||
TextInputState, | ||
deepFreezeAndThrowOnMutationInDev, | ||
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'; | ||
|
||
|
@@ -46,14 +36,9 @@ const { | |
appendChildToSet: appendChildNodeToSet, | ||
completeRoot, | ||
registerEventHandler, | ||
measure: fabricMeasure, | ||
measureInWindow: fabricMeasureInWindow, | ||
measureLayout: fabricMeasureLayout, | ||
unstable_DefaultEventPriority: FabricDefaultPriority, | ||
unstable_DiscreteEventPriority: FabricDiscretePriority, | ||
unstable_getCurrentEventPriority: fabricGetCurrentEventPriority, | ||
setNativeProps, | ||
getBoundingClientRect: fabricGetBoundingClientRect, | ||
} = nativeFabricUIManager; | ||
|
||
const {get: getViewConfigForType} = ReactNativeViewConfigRegistry; | ||
|
@@ -68,9 +53,15 @@ type Node = Object; | |
export type Type = string; | ||
export type Props = Object; | ||
export type Instance = { | ||
// Reference to the shadow node. | ||
node: Node, | ||
canonical: ReactFabricHostComponent, | ||
... | ||
nativeTag: number, | ||
viewConfig: ViewConfig, | ||
currentProps: Props, | ||
// Reference to the React handle (the fiber) | ||
internalInstanceHandle: Object, | ||
// Exposed through refs. | ||
publicInstance: ReactFabricHostComponent, | ||
}; | ||
export type TextInstance = {node: Node, ...}; | ||
export type HydratableInstance = Instance | TextInstance; | ||
|
@@ -104,137 +95,6 @@ if (registerEventHandler) { | |
registerEventHandler(dispatchEvent); | ||
} | ||
|
||
const noop = () => {}; | ||
|
||
/** | ||
* This is used for refs on host components. | ||
*/ | ||
class ReactFabricHostComponent implements INativeMethods { | ||
_nativeTag: number; | ||
viewConfig: ViewConfig; | ||
currentProps: Props; | ||
_internalInstanceHandle: Object; | ||
|
||
constructor( | ||
tag: number, | ||
viewConfig: ViewConfig, | ||
props: Props, | ||
internalInstanceHandle: Object, | ||
) { | ||
this._nativeTag = tag; | ||
this.viewConfig = viewConfig; | ||
this.currentProps = props; | ||
this._internalInstanceHandle = internalInstanceHandle; | ||
} | ||
|
||
blur() { | ||
TextInputState.blurTextInput(this); | ||
} | ||
|
||
focus() { | ||
TextInputState.focusTextInput(this); | ||
} | ||
|
||
measure(callback: MeasureOnSuccessCallback) { | ||
const node = getShadowNodeFromInternalInstanceHandle( | ||
this._internalInstanceHandle, | ||
); | ||
if (node != null) { | ||
fabricMeasure(node, callback); | ||
} | ||
} | ||
|
||
measureInWindow(callback: MeasureInWindowOnSuccessCallback) { | ||
const node = getShadowNodeFromInternalInstanceHandle( | ||
this._internalInstanceHandle, | ||
); | ||
if (node != null) { | ||
fabricMeasureInWindow(node, callback); | ||
} | ||
} | ||
|
||
measureLayout( | ||
relativeToNativeNode: number | ElementRef<HostComponent<mixed>>, | ||
onSuccess: MeasureLayoutOnSuccessCallback, | ||
onFail?: () => void /* currently unused */, | ||
) { | ||
if ( | ||
typeof relativeToNativeNode === 'number' || | ||
!(relativeToNativeNode instanceof ReactFabricHostComponent) | ||
) { | ||
if (__DEV__) { | ||
console.error( | ||
'Warning: ref.measureLayout must be called with a ref to a native component.', | ||
); | ||
} | ||
|
||
return; | ||
} | ||
|
||
const toStateNode = getShadowNodeFromInternalInstanceHandle( | ||
this._internalInstanceHandle, | ||
); | ||
const fromStateNode = getShadowNodeFromInternalInstanceHandle( | ||
relativeToNativeNode._internalInstanceHandle, | ||
); | ||
|
||
if (toStateNode != null && fromStateNode != null) { | ||
fabricMeasureLayout( | ||
toStateNode, | ||
fromStateNode, | ||
onFail != null ? onFail : noop, | ||
onSuccess != null ? onSuccess : noop, | ||
); | ||
} | ||
} | ||
|
||
unstable_getBoundingClientRect(): DOMRect { | ||
const node = getShadowNodeFromInternalInstanceHandle( | ||
this._internalInstanceHandle, | ||
); | ||
if (node != null) { | ||
const rect = fabricGetBoundingClientRect(node); | ||
|
||
if (rect) { | ||
return new DOMRect(rect[0], rect[1], rect[2], rect[3]); | ||
} | ||
} | ||
|
||
// Empty rect if any of the above failed | ||
return new DOMRect(0, 0, 0, 0); | ||
} | ||
|
||
setNativeProps(nativeProps: Object) { | ||
if (__DEV__) { | ||
warnForStyleProps(nativeProps, this.viewConfig.validAttributes); | ||
} | ||
const updatePayload = create(nativeProps, this.viewConfig.validAttributes); | ||
|
||
const node = getShadowNodeFromInternalInstanceHandle( | ||
this._internalInstanceHandle, | ||
); | ||
if (node != null && updatePayload != null) { | ||
setNativeProps(node, updatePayload); | ||
} | ||
} | ||
} | ||
|
||
type ParamOf<Fn> = $Call<<T>((arg: T) => mixed) => T, Fn>; | ||
type ShadowNode = ParamOf<(typeof nativeFabricUIManager)['measure']>; | ||
|
||
export function getShadowNodeFromInternalInstanceHandle( | ||
internalInstanceHandle: mixed, | ||
): ?ShadowNode { | ||
return ( | ||
// $FlowExpectedError[incompatible-return] internalInstanceHandle is opaque but we need to make an exception here. | ||
internalInstanceHandle && | ||
// $FlowExpectedError[incompatible-return] | ||
internalInstanceHandle.stateNode && | ||
// $FlowExpectedError[incompatible-use] | ||
internalInstanceHandle.stateNode.node | ||
); | ||
} | ||
|
||
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMutation'; | ||
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoHydration'; | ||
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoScopes'; | ||
|
@@ -280,16 +140,19 @@ export function createInstance( | |
internalInstanceHandle, // internalInstanceHandle | ||
); | ||
|
||
const component = new ReactFabricHostComponent( | ||
const component = createPublicInstance( | ||
tag, | ||
viewConfig, | ||
props, | ||
internalInstanceHandle, | ||
); | ||
|
||
return { | ||
node: node, | ||
canonical: component, | ||
nativeTag: tag, | ||
viewConfig, | ||
currentProps: props, | ||
internalInstanceHandle, | ||
publicInstance: component, | ||
Comment on lines
+151
to
+155
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. Where is |
||
}; | ||
} | ||
|
||
|
@@ -359,12 +222,15 @@ export function getChildHostContext( | |
} | ||
|
||
export function getPublicInstance(instance: Instance): null | PublicInstance { | ||
if (instance.canonical) { | ||
return instance.canonical; | ||
if (instance.publicInstance != null) { | ||
return instance.publicInstance; | ||
} | ||
|
||
// For compatibility with Paper | ||
// For compatibility with the legacy renderer, in case it's used with Fabric | ||
// in the same app. | ||
// $FlowExpectedError[prop-missing] | ||
if (instance._nativeTag != null) { | ||
// $FlowExpectedError[incompatible-return] | ||
return instance; | ||
} | ||
|
||
|
@@ -383,12 +249,12 @@ export function prepareUpdate( | |
newProps: Props, | ||
hostContext: HostContext, | ||
): null | Object { | ||
const viewConfig = instance.canonical.viewConfig; | ||
const viewConfig = instance.viewConfig; | ||
const updatePayload = diff(oldProps, newProps, viewConfig.validAttributes); | ||
// TODO: If the event handlers have changed, we need to update the current props | ||
// in the commit phase but there is no host config hook to do it yet. | ||
// So instead we hack it by updating it in the render phase. | ||
instance.canonical.currentProps = newProps; | ||
instance.currentProps = newProps; | ||
return updatePayload; | ||
} | ||
|
||
|
@@ -467,7 +333,11 @@ export function cloneInstance( | |
} | ||
return { | ||
node: clone, | ||
canonical: instance.canonical, | ||
nativeTag: instance.nativeTag, | ||
viewConfig: instance.viewConfig, | ||
currentProps: instance.currentProps, | ||
internalInstanceHandle: instance.internalInstanceHandle, | ||
publicInstance: instance.publicInstance, | ||
}; | ||
} | ||
|
||
|
@@ -477,15 +347,19 @@ export function cloneHiddenInstance( | |
props: Props, | ||
internalInstanceHandle: Object, | ||
): Instance { | ||
const viewConfig = instance.canonical.viewConfig; | ||
const viewConfig = instance.viewConfig; | ||
const node = instance.node; | ||
const updatePayload = create( | ||
{style: {display: 'none'}}, | ||
viewConfig.validAttributes, | ||
); | ||
return { | ||
node: cloneNodeWithNewProps(node, updatePayload), | ||
canonical: instance.canonical, | ||
nativeTag: instance.nativeTag, | ||
viewConfig: instance.viewConfig, | ||
currentProps: instance.currentProps, | ||
internalInstanceHandle: instance.internalInstanceHandle, | ||
publicInstance: instance.publicInstance, | ||
}; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could we structure this as the following instead, to create a single shared object between instances?
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 was just implementing that when I saw that comment. Making sure I'm updating all the references using this PR as well, hehe. Thanks!