@@ -38,6 +38,8 @@ import {getParentHydrationBoundary} from './ReactFiberConfigDOM';
3838
3939import { enableScopeAPI } from 'shared/ReactFeatureFlags' ;
4040
41+ import { enableInternalInstanceMap } from 'shared/ReactFeatureFlags' ;
42+
4143const randomKey = Math . random ( ) . toString ( 36 ) . slice ( 2 ) ;
4244const internalInstanceKey = '__reactFiber$' + randomKey ;
4345const internalPropsKey = '__reactProps$' + randomKey ;
@@ -49,7 +51,32 @@ const internalRootNodeResourcesKey = '__reactResources$' + randomKey;
4951const internalHoistableMarker = '__reactMarker$' + randomKey ;
5052const internalScrollTimer = '__reactScroll$' + randomKey ;
5153
54+ type InstanceUnion =
55+ | Instance
56+ | TextInstance
57+ | SuspenseInstance
58+ | ActivityInstance
59+ | ReactScopeInstance
60+ | Container ;
61+
62+ const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map ;
63+ const internalInstanceMap :
64+ | WeakMap < InstanceUnion , Fiber >
65+ | Map < InstanceUnion , Fiber > = new PossiblyWeakMap ( ) ;
66+ const internalPropsMap :
67+ | WeakMap < InstanceUnion , Props >
68+ | Map < InstanceUnion , Props > = new PossiblyWeakMap ( ) ;
69+
5270export function detachDeletedInstance ( node : Instance ) : void {
71+ if ( enableInternalInstanceMap ) {
72+ internalInstanceMap . delete ( node ) ;
73+ internalPropsMap . delete ( node ) ;
74+ delete ( node : any ) [ internalEventHandlersKey ] ;
75+ delete ( node : any ) [ internalEventHandlerListenersKey ] ;
76+ delete ( node : any ) [ internalEventHandlesSetKey ] ;
77+ delete ( node : any ) [ internalRootNodeResourcesKey ] ;
78+ return ;
79+ }
5380 // TODO: This function is only called on host components. I don't think all of
5481 // these fields are relevant.
5582 delete ( node : any ) [ internalInstanceKey ] ;
@@ -68,6 +95,10 @@ export function precacheFiberNode(
6895 | ActivityInstance
6996 | ReactScopeInstance ,
7097) : void {
98+ if ( enableInternalInstanceMap ) {
99+ internalInstanceMap . set ( node , hostInst ) ;
100+ return ;
101+ }
71102 ( node : any ) [ internalInstanceKey ] = hostInst ;
72103}
73104
@@ -95,7 +126,12 @@ export function isContainerMarkedAsRoot(node: Container): boolean {
95126// HostRoot back. To get to the HostRoot, you need to pass a child of it.
96127// The same thing applies to Suspense and Activity boundaries.
97128export function getClosestInstanceFromNode ( targetNode : Node ) : null | Fiber {
98- let targetInst = ( targetNode : any ) [ internalInstanceKey ] ;
129+ let targetInst : void | Fiber ;
130+ if ( enableInternalInstanceMap ) {
131+ targetInst = internalInstanceMap . get ( ( ( targetNode : any ) : InstanceUnion ) ) ;
132+ } else {
133+ targetInst = ( targetNode : any ) [ internalInstanceKey ] ;
134+ }
99135 if ( targetInst ) {
100136 // Don't return HostRoot, SuspenseComponent or ActivityComponent here.
101137 return targetInst ;
@@ -112,9 +148,15 @@ export function getClosestInstanceFromNode(targetNode: Node): null | Fiber {
112148 // itself because the fibers are conceptually between the container
113149 // node and the first child. It isn't surrounding the container node.
114150 // If it's not a container, we check if it's an instance.
115- targetInst =
116- ( parentNode : any ) [ internalContainerInstanceKey ] ||
117- ( parentNode : any ) [ internalInstanceKey ] ;
151+ if ( enableInternalInstanceMap ) {
152+ targetInst =
153+ ( parentNode : any ) [ internalContainerInstanceKey ] ||
154+ internalInstanceMap . get ( ( ( parentNode : any ) : InstanceUnion ) ) ;
155+ } else {
156+ targetInst =
157+ ( parentNode : any ) [ internalContainerInstanceKey ] ||
158+ ( parentNode : any ) [ internalInstanceKey ] ;
159+ }
118160 if ( targetInst ) {
119161 // Since this wasn't the direct target of the event, we might have
120162 // stepped past dehydrated DOM nodes to get here. However they could
@@ -147,8 +189,10 @@ export function getClosestInstanceFromNode(targetNode: Node): null | Fiber {
147189 // have had an internalInstanceKey on it.
148190 // Let's get the fiber associated with the SuspenseComponent
149191 // as the deepest instance.
150- // $FlowFixMe[prop-missing]
151- const targetFiber = hydrationInstance [ internalInstanceKey ] ;
192+ const targetFiber = enableInternalInstanceMap
193+ ? internalInstanceMap . get ( hydrationInstance )
194+ : // $FlowFixMe[prop-missing]
195+ hydrationInstance [ internalInstanceKey ] ;
152196 if ( targetFiber ) {
153197 return targetFiber ;
154198 }
@@ -175,9 +219,16 @@ export function getClosestInstanceFromNode(targetNode: Node): null | Fiber {
175219 * instance, or null if the node was not rendered by this React.
176220 */
177221export function getInstanceFromNode ( node : Node ) : Fiber | null {
178- const inst =
179- ( node : any ) [ internalInstanceKey ] ||
180- ( node : any ) [ internalContainerInstanceKey ] ;
222+ let inst : void | null | Fiber ;
223+ if ( enableInternalInstanceMap ) {
224+ inst =
225+ internalInstanceMap . get ( ( ( node : any ) : InstanceUnion ) ) ||
226+ ( node : any ) [ internalContainerInstanceKey ] ;
227+ } else {
228+ inst =
229+ ( node : any ) [ internalInstanceKey ] ||
230+ ( node : any ) [ internalContainerInstanceKey ] ;
231+ }
181232 if ( inst ) {
182233 const tag = inst . tag ;
183234 if (
@@ -226,16 +277,24 @@ export function getFiberCurrentPropsFromNode(
226277 | TextInstance
227278 | SuspenseInstance
228279 | ActivityInstance ,
229- ) : Props {
280+ ) : Props | null {
281+ if ( enableInternalInstanceMap ) {
282+ return internalPropsMap . get ( node ) || null ;
283+ }
230284 return ( node : any ) [ internalPropsKey ] || null ;
231285}
232286
233287export function updateFiberProps ( node : Instance , props : Props ) : void {
288+ if ( enableInternalInstanceMap ) {
289+ internalPropsMap . set ( node , props ) ;
290+ return ;
291+ }
234292 ( node : any ) [ internalPropsKey ] = props ;
235293}
236294
237295export function getEventListenerSet ( node : EventTarget ) : Set < string > {
238- let elementListenerSet = ( node : any ) [ internalEventHandlersKey ] ;
296+ let elementListenerSet : Set < string > | void ;
297+ elementListenerSet = ( node : any ) [ internalEventHandlersKey ] ;
239298 if ( elementListenerSet === undefined ) {
240299 elementListenerSet = ( node : any ) [ internalEventHandlersKey ] = new Set ( ) ;
241300 }
@@ -246,6 +305,9 @@ export function getFiberFromScopeInstance(
246305 scope: ReactScopeInstance,
247306): null | Fiber {
248307 if ( enableScopeAPI ) {
308+ if ( enableInternalInstanceMap ) {
309+ return internalInstanceMap . get ( ( ( scope : any ) : InstanceUnion ) ) || null ;
310+ }
249311 return ( scope : any ) [ internalInstanceKey ] || null ;
250312 }
251313 return null;
@@ -318,6 +380,12 @@ export function clearScrollEndTimer(node: EventTarget): void {
318380}
319381
320382export function isOwnedInstance(node: Node): boolean {
383+ if ( enableInternalInstanceMap ) {
384+ return ! ! (
385+ ( node : any ) [ internalHoistableMarker ] ||
386+ internalInstanceMap . has ( ( node : any ) )
387+ ) ;
388+ }
321389 return !!(
322390 (node: any)[internalHoistableMarker] || (node: any)[internalInstanceKey]
323391 );
0 commit comments