@@ -14,13 +14,16 @@ import {
1414 DefaultEventPriority ,
1515 DiscreteEventPriority ,
1616} from 'react-reconciler/src/ReactEventPriorities' ;
17+ import { HostText } from 'react-reconciler/src/ReactWorkTags' ;
1718
1819// Modules provided by RN:
1920import {
2021 ReactNativeViewConfigRegistry ,
2122 deepFreezeAndThrowOnMutationInDev ,
2223 createPublicInstance ,
24+ createPublicTextInstance ,
2325 type PublicInstance as ReactNativePublicInstance ,
26+ type PublicTextInstance ,
2427} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface' ;
2528
2629const {
@@ -47,23 +50,33 @@ const {get: getViewConfigForType} = ReactNativeViewConfigRegistry;
4750// This means that they never overlap.
4851let nextReactTag = 2 ;
4952
53+ type InternalInstanceHandle = Object ;
5054type Node = Object ;
5155export type Type = string ;
5256export type Props = Object ;
5357export type Instance = {
5458 // Reference to the shadow node.
5559 node : Node ,
60+ // This object is shared by all the clones of the instance.
61+ // We use it to access their shared public instance (exposed through refs)
62+ // and to access its committed state for events, etc.
5663 canonical : {
5764 nativeTag : number ,
5865 viewConfig : ViewConfig ,
5966 currentProps : Props ,
6067 // Reference to the React handle (the fiber)
61- internalInstanceHandle : Object ,
68+ internalInstanceHandle : InternalInstanceHandle ,
6269 // Exposed through refs.
6370 publicInstance : PublicInstance ,
6471 } ,
6572} ;
66- export type TextInstance = { node : Node , ...} ;
73+ export type TextInstance = {
74+ // Reference to the shadow node.
75+ node : Node ,
76+ // Text instances are never cloned, so we don't need to keep a "canonical"
77+ // reference to make sure all clones of the instance point to the same values.
78+ publicInstance ?: PublicTextInstance ,
79+ } ;
6780export type HydratableInstance = Instance | TextInstance ;
6881export type PublicInstance = ReactNativePublicInstance ;
6982export type Container = number ;
@@ -115,7 +128,7 @@ export function createInstance(
115128 props : Props ,
116129 rootContainerInstance : Container ,
117130 hostContext : HostContext ,
118- internalInstanceHandle : Object ,
131+ internalInstanceHandle : InternalInstanceHandle ,
119132) : Instance {
120133 const tag = nextReactTag ;
121134 nextReactTag += 2 ;
@@ -162,7 +175,7 @@ export function createTextInstance(
162175 text : string ,
163176 rootContainerInstance : Container ,
164177 hostContext : HostContext ,
165- internalInstanceHandle : Object ,
178+ internalInstanceHandle : InternalInstanceHandle ,
166179) : TextInstance {
167180 if ( __DEV__ ) {
168181 if ( ! hostContext . isInAParentText ) {
@@ -239,9 +252,26 @@ export function getPublicInstance(instance: Instance): null | PublicInstance {
239252 return null ;
240253}
241254
255+ function getPublicTextInstance (
256+ textInstance : TextInstance ,
257+ internalInstanceHandle : InternalInstanceHandle ,
258+ ) : PublicTextInstance {
259+ if ( textInstance . publicInstance == null ) {
260+ textInstance . publicInstance = createPublicTextInstance (
261+ internalInstanceHandle ,
262+ ) ;
263+ }
264+ return textInstance . publicInstance ;
265+ }
266+
242267export function getPublicInstanceFromInternalInstanceHandle (
243- internalInstanceHandle : Object ,
244- ) : null | PublicInstance {
268+ internalInstanceHandle : InternalInstanceHandle ,
269+ ) : null | PublicInstance | PublicTextInstance {
270+ if ( internalInstanceHandle . tag === HostText ) {
271+ const textInstance : TextInstance = internalInstanceHandle . stateNode ;
272+ return getPublicTextInstance ( textInstance , internalInstanceHandle ) ;
273+ }
274+
245275 const instance : Instance = internalInstanceHandle . stateNode ;
246276 return getPublicInstance ( instance ) ;
247277}
@@ -321,7 +351,7 @@ export function cloneInstance(
321351 type : string ,
322352 oldProps : Props ,
323353 newProps : Props ,
324- internalInstanceHandle : Object ,
354+ internalInstanceHandle : InternalInstanceHandle ,
325355 keepChildren : boolean ,
326356 recyclableInstance : null | Instance ,
327357) : Instance {
@@ -350,7 +380,7 @@ export function cloneHiddenInstance(
350380 instance : Instance ,
351381 type : string ,
352382 props : Props ,
353- internalInstanceHandle : Object ,
383+ internalInstanceHandle : InternalInstanceHandle ,
354384) : Instance {
355385 const viewConfig = instance . canonical . viewConfig ;
356386 const node = instance . node ;
@@ -367,7 +397,7 @@ export function cloneHiddenInstance(
367397export function cloneHiddenTextInstance (
368398 instance : Instance ,
369399 text : string ,
370- internalInstanceHandle : Object ,
400+ internalInstanceHandle : InternalInstanceHandle ,
371401) : TextInstance {
372402 throw new Error ( 'Not yet implemented.' ) ;
373403}
@@ -399,7 +429,9 @@ export function getInstanceFromNode(node: any): empty {
399429 throw new Error ( 'Not yet implemented.' ) ;
400430}
401431
402- export function beforeActiveInstanceBlur ( internalInstanceHandle : Object ) {
432+ export function beforeActiveInstanceBlur (
433+ internalInstanceHandle : InternalInstanceHandle ,
434+ ) {
403435 // noop
404436}
405437
0 commit comments