@@ -15,7 +15,6 @@ import androidx.annotation.AnyThread
1515import androidx.annotation.UiThread
1616import androidx.collection.SparseArrayCompat
1717import com.facebook.common.logging.FLog
18- import com.facebook.infer.annotation.Assertions
1918import com.facebook.infer.annotation.ThreadConfined
2019import com.facebook.react.bridge.GuardedRunnable
2120import com.facebook.react.bridge.ReactNoCrashSoftException
@@ -112,30 +111,15 @@ internal constructor(
112111 @ThreadConfined(ThreadConfined .UI )
113112 private val tagToSynchronousMountProps = SparseArrayCompat <MutableMap <String , Any >>()
114113
114+ @AnyThread
115115 public fun attachRootView (rootView : View , themedReactContext : ThemedReactContext ): Unit {
116116 this .context = themedReactContext
117- addRootView(rootView)
118- }
119117
120- public fun getViewExists (tag : Int ): Boolean {
121- // If Surface stopped, check if tag *was* associated with this Surface, even though it's been
122- // deleted. This helps distinguish between scenarios where an invalid tag is referenced, vs
123- // race conditions where an imperative method is called on a tag during/just after StopSurface.
124- if (tagSetForStoppedSurface?.containsKey(tag) == true ) {
125- return true
126- }
127- return tagToViewState.containsKey(tag)
128- }
129-
130- @UiThread
131- @ThreadConfined(ThreadConfined .UI )
132- internal fun scheduleMountItemOnViewAttach (item : MountItem ): Unit {
133- onViewAttachMountItems.add(item)
134- }
135-
136- @AnyThread
137- private fun addRootView (rootView : View ) {
138118 if (isStopped) {
119+ ReactSoftExceptionLogger .logSoftException(
120+ TAG ,
121+ IllegalStateException (" Trying to attach root view to a stopped surface" ),
122+ )
139123 return
140124 }
141125
@@ -196,6 +180,22 @@ internal constructor(
196180 }
197181 }
198182
183+ public fun getViewExists (tag : Int ): Boolean {
184+ // If Surface stopped, check if tag *was* associated with this Surface, even though it's been
185+ // deleted. This helps distinguish between scenarios where an invalid tag is referenced, vs
186+ // race conditions where an imperative method is called on a tag during/just after StopSurface.
187+ if (tagSetForStoppedSurface?.containsKey(tag) == true ) {
188+ return true
189+ }
190+ return tagToViewState.containsKey(tag)
191+ }
192+
193+ @UiThread
194+ @ThreadConfined(ThreadConfined .UI )
195+ internal fun scheduleMountItemOnViewAttach (item : MountItem ): Unit {
196+ onViewAttachMountItems.add(item)
197+ }
198+
199199 @UiThread
200200 @ThreadConfined(ThreadConfined .UI )
201201 private fun executeMountItemsOnViewAttach () {
@@ -370,7 +370,7 @@ internal constructor(
370370 ReactSoftExceptionLogger .logSoftException(
371371 TAG ,
372372 IllegalViewOperationException (
373- ( " removeViewAt tried to remove a React View that was actually reused. This indicates a bug in the Differ (specifically instruction ordering). [$tag ]" )
373+ " removeViewAt tried to remove a React View that was actually reused. This indicates a bug in the Differ (specifically instruction ordering). [$tag ]"
374374 ),
375375 )
376376 return
@@ -428,7 +428,7 @@ internal constructor(
428428 if (tagActualIndex == - 1 ) {
429429 FLog .e(
430430 TAG ,
431- ( " removeViewAt: [$tag ] -> [$parentTag ] @$index : view already removed from parent! Children in parent: ${ parentChildrenCount} " ) ,
431+ " removeViewAt: [$tag ] -> [$parentTag ] @$index : view already removed from parent! Children in parent: $parentChildrenCount " ,
432432 )
433433 return
434434 }
@@ -446,7 +446,7 @@ internal constructor(
446446 ReactSoftExceptionLogger .logSoftException(
447447 TAG ,
448448 IllegalStateException (
449- ( " Tried to remove view [$tag ] of parent [$parentTag ] at index $index , but got view tag $actualTag - actual index of view: $tagActualIndex " )
449+ " Tried to remove view [$tag ] of parent [$parentTag ] at index $index , but got view tag $actualTag - actual index of view: $tagActualIndex "
450450 ),
451451 )
452452 actualIndex = tagActualIndex
@@ -562,7 +562,13 @@ internal constructor(
562562 val viewManager = viewManagerRegistry?.get(componentName) as ViewManager <View , * >
563563 // View Managers are responsible for dealing with inital state and props.
564564 viewState.view =
565- viewManager.createView(reactTag, context!! , propMap, stateWrapper, jsResponderHandler)
565+ viewManager.createView(
566+ reactTag,
567+ checkNotNull(context),
568+ propMap,
569+ stateWrapper,
570+ jsResponderHandler,
571+ )
566572 viewState.viewManager = viewManager
567573 }
568574 } finally {
@@ -573,13 +579,13 @@ internal constructor(
573579 public fun storeSynchronousMountPropsOverride (reactTag : Int , props : ReadableMap ): Unit {
574580 if (ReactNativeFeatureFlags .overrideBySynchronousMountPropsAtMountingAndroid()) {
575581 val propsMap = getMapFromPropsReadableMap(props)
576- if (reactTag in tagToSynchronousMountProps) {
577- val mergedPropsMap = checkNotNull(tagToSynchronousMountProps[reactTag])
578- mergedPropsMap.putAll(propsMap)
579- tagToSynchronousMountProps[reactTag] = mergedPropsMap
582+ var synchronousMountProps = tagToSynchronousMountProps[reactTag]
583+ if (synchronousMountProps != null ) {
584+ synchronousMountProps.putAll(propsMap)
580585 } else {
581- tagToSynchronousMountProps[reactTag] = propsMap
586+ synchronousMountProps = propsMap
582587 }
588+ tagToSynchronousMountProps[reactTag] = synchronousMountProps
583589 }
584590 }
585591
@@ -610,8 +616,7 @@ internal constructor(
610616 ) {
611617 val modifiedProps = WritableNativeMap ()
612618 modifiedProps.merge(props)
613- val directPropsMap =
614- Assertions .assertNotNull<Map <String , Any >>(tagToSynchronousMountProps[reactTag])
619+ val directPropsMap = checkNotNull(tagToSynchronousMountProps[reactTag])
615620 overridePropsReadableMap(directPropsMap, modifiedProps)
616621 viewState.currentProps = ReactStylesDiffMap (modifiedProps)
617622 } else {
@@ -655,7 +660,7 @@ internal constructor(
655660 val viewState =
656661 getNullableViewState(reactTag)
657662 ? : throw RetryableMountingLayerException (
658- " Unable to find viewState for tag: [ $reactTag ] for commandId: $commandId "
663+ " Unable to find viewState for tag $reactTag for commandId $commandId "
659664 )
660665
661666 // It's not uncommon for JS to send events as/after a component is being removed from the
@@ -684,7 +689,7 @@ internal constructor(
684689 val viewState =
685690 getNullableViewState(reactTag)
686691 ? : throw RetryableMountingLayerException (
687- " Unable to find viewState for tag: $reactTag for commandId: $commandId "
692+ " Unable to find viewState for tag $reactTag for commandId $commandId "
688693 )
689694
690695 // It's not uncommon for JS to send events as/after a component is being removed from the
@@ -783,9 +788,7 @@ internal constructor(
783788 if (parentViewState == null ) {
784789 ReactSoftExceptionLogger .logSoftException(
785790 ReactSoftExceptionLogger .Categories .SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE ,
786- ReactNoCrashSoftException (
787- " Unable to find viewState for tag: $parentTag for updateLayout"
788- ),
791+ ReactNoCrashSoftException (" Unable to find viewState for tag $parentTag for updateLayout" ),
789792 )
790793 } else if (parentViewState.viewManager != null ) {
791794 parentViewManager = parentViewState.viewManager as IViewGroupManager <* >
@@ -966,7 +969,7 @@ internal constructor(
966969 // For non-root views we notify viewmanager with [ViewManager#onDropInstance]
967970 val viewManager = viewState.viewManager
968971 if (! viewState.isRoot && viewManager != null ) {
969- viewManager.onDropViewInstance(viewState.view!! )
972+ viewManager.onDropViewInstance(checkNotNull( viewState.view) )
970973 }
971974 }
972975
@@ -989,7 +992,7 @@ internal constructor(
989992 if (viewState == null ) {
990993 ReactSoftExceptionLogger .logSoftException(
991994 ReactSoftExceptionLogger .Categories .SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE ,
992- ReactNoCrashSoftException (" Unable to find viewState for tag: $reactTag for deleteView" ),
995+ ReactNoCrashSoftException (" Unable to find viewState for tag $reactTag for deleteView" ),
993996 )
994997 return
995998 }
@@ -1043,21 +1046,19 @@ internal constructor(
10431046 @UiThread
10441047 public fun getView (reactTag : Int ): View {
10451048 val state = getNullableViewState(reactTag)
1046- val view =
1047- state?.view
1048- ? : throw IllegalViewOperationException (
1049- " Trying to resolve view with tag $reactTag which doesn't exist"
1050- )
1051- return view
1049+ return state?.view
1050+ ? : throw IllegalViewOperationException (
1051+ " Unable to find view for tag $reactTag . Surface $surfaceId stopped: $isStopped , rootViewAttached: $isRootViewAttached "
1052+ )
10521053 }
10531054
1054- private fun getViewState (tag : Int ): ViewState =
1055- tagToViewState[tag ]
1055+ private fun getViewState (reactTag : Int ): ViewState =
1056+ tagToViewState[reactTag ]
10561057 ? : throw RetryableMountingLayerException (
1057- " Unable to find viewState for tag $tag . Surface stopped: $isStopped "
1058+ " Unable to find viewState for tag $reactTag . Surface stopped: $isStopped "
10581059 )
10591060
1060- private fun getNullableViewState (tag : Int ): ViewState ? = tagToViewState[tag ]
1061+ private fun getNullableViewState (reactTag : Int ): ViewState ? = tagToViewState[reactTag ]
10611062
10621063 public fun printSurfaceState (): Unit {
10631064 FLog .e(TAG , " Views created for surface $surfaceId :" )
0 commit comments