Skip to content

Commit afe2bf8

Browse files
authored
Merge synchronous props queued for an unmounted view with newer updates
Summary: When a synchronous (native-driven animated) props update arrives for a view that doesn't exist yet, `FabricUIManager` queues it behind the pending mount items. The view can mount and get a newer synchronous update applied directly before the queued one runs, for example when a view command focuses the new view and fires an event that drives native animated props. The queued update then puts the older props back on the view and stores them as its synchronous mount props override, which also beats the next Fabric commit, so the view keeps the stale values: text that should change color on focus stays in its unfocused color. Synchronous props for a view that doesn't exist yet now merge into one pending map per view (`PendingSynchronousProps`): a newer value wins, and a newer `null` still resets the prop. Each pending update queues a mount item behind the pending mount items, and only the newest one applies the merged props, so they also wait for mount items queued in between, such as the one that creates the view. An update applied directly while props are pending applies them merged with its own and clears them. `synchronouslyUpdateViewOnUIThread` and the batched path (`synchronouslyUpdateViewBatch`) both go through it; the batched path still looks each view up once unless props are pending for it. Changelog: [Android][Fixed] - Fix a stale queued animated props update overwriting a newer one when a view mounts and updates in the same frame Reviewed By: zeyap Differential Revision: D132802978 Pull Request resolved: #60487
1 parent 1bb94ce commit afe2bf8

5 files changed

Lines changed: 284 additions & 11 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.facebook.react.fabric.interop.UIBlockViewResolver;
6161
import com.facebook.react.fabric.mounting.MountItemDispatcher;
6262
import com.facebook.react.fabric.mounting.MountingManager;
63+
import com.facebook.react.fabric.mounting.PendingSynchronousProps;
6364
import com.facebook.react.fabric.mounting.SurfaceMountingManager;
6465
import com.facebook.react.fabric.mounting.mountitems.BatchMountItem;
6566
import com.facebook.react.fabric.mounting.mountitems.BatchedAnimatedPropsMountItem;
@@ -229,6 +230,9 @@ public class FabricUIManager
229230
// from C++ to exceed 9,999 and it should be obvious what's going on when analyzing performance.
230231
private int mCurrentSynchronousCommitNumber = 10000;
231232

233+
@ThreadConfined(UI)
234+
private final PendingSynchronousProps mPendingSynchronousProps;
235+
232236
private final MountingManager.MountItemExecutor mMountItemExecutor =
233237
new MountingManager.MountItemExecutor() {
234238
@Override
@@ -258,6 +262,7 @@ public FabricUIManager(
258262
mMountingManager = new MountingManager(viewManagerRegistry, mMountItemExecutor);
259263
mMountItemDispatcher =
260264
new MountItemDispatcher(mMountingManager, new MountItemDispatchListener());
265+
mPendingSynchronousProps = new PendingSynchronousProps(mMountItemDispatcher);
261266
mEventDispatcher = new FabricEventDispatcher(reactContext, new FabricEventEmitter(this));
262267
mBatchEventDispatchedListener = batchEventDispatchedListener;
263268
mReactApplicationContext.addLifecycleEventListener(this);
@@ -830,12 +835,10 @@ public void synchronouslyUpdateViewOnUIThread(final int reactTag, final Readable
830835
// android.view.View.updateDisplayListIfDirty(View.java:20466)
831836
// 3. A view is deleted while its parent is being drawn, causing a crash.
832837

833-
MountItem synchronousMountItem = new SynchronousMountItem(reactTag, props);
834-
835838
// If the reactTag exists, we assume that it might at the end of the next
836839
// batch of MountItems. Otherwise, we try to execute immediately.
837840
if (!mMountingManager.getViewExists(reactTag)) {
838-
mMountItemDispatcher.addMountItem(synchronousMountItem);
841+
mPendingSynchronousProps.add(reactTag, props);
839842
return;
840843
}
841844

@@ -850,7 +853,8 @@ public void synchronouslyUpdateViewOnUIThread(final int reactTag, final Readable
850853
(IS_DEVELOPMENT_ENVIRONMENT ? props.toHashMap().toString() : "<hidden>"));
851854
}
852855

853-
synchronousMountItem.execute(mMountingManager);
856+
new SynchronousMountItem(reactTag, mPendingSynchronousProps.takeMergedWith(reactTag, props))
857+
.execute(mMountingManager);
854858

855859
ReactMarker.logFabricMarker(
856860
ReactMarkerConstants.FABRIC_UPDATE_UI_MAIN_THREAD_END, null, commitNumber);
@@ -878,7 +882,8 @@ private void synchronouslyUpdateViewBatch(
878882
rawPropsBuffer.length);
879883
}
880884

881-
new BatchedAnimatedPropsMountItem(intBuffer, doubleBuffer, rawPropsBuffer, mMountItemDispatcher)
885+
new BatchedAnimatedPropsMountItem(
886+
intBuffer, doubleBuffer, rawPropsBuffer, mPendingSynchronousProps)
882887
.execute(mMountingManager);
883888

884889
ReactMarker.logFabricMarker(
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.fabric.mounting
9+
10+
import android.view.View
11+
import androidx.annotation.UiThread
12+
import androidx.collection.SparseArrayCompat
13+
import com.facebook.react.bridge.JavaOnlyMap
14+
import com.facebook.react.bridge.ReadableMap
15+
import com.facebook.react.fabric.mounting.mountitems.MountItem
16+
import com.facebook.react.fabric.mounting.mountitems.SynchronousMountItem
17+
18+
/**
19+
* Synchronous props updates for views that don't exist yet. Updates for the same view merge into
20+
* one map: a newer value wins, and a newer null resets the prop. Applying the merged map once gives
21+
* the same result as applying the updates in order.
22+
*/
23+
internal class PendingSynchronousProps(private val mountItemDispatcher: MountItemDispatcher) {
24+
private val pendingByTag = SparseArrayCompat<Pending>()
25+
26+
/** Returns true if props are pending for the view. */
27+
@UiThread fun hasPending(reactTag: Int): Boolean = pendingByTag.containsKey(reactTag)
28+
29+
/**
30+
* Keeps [props] until the view mounts, and queues a mount item behind the pending mount items
31+
* that applies everything pending for the view. Only the item queued for the newest update
32+
* applies them, so they also wait for mount items queued after an earlier update, such as the one
33+
* that creates the view.
34+
*/
35+
@UiThread
36+
fun add(reactTag: Int, props: ReadableMap) {
37+
val applyItem = ApplyPendingPropsMountItem(reactTag)
38+
val pending = pendingByTag[reactTag]
39+
if (pending == null) {
40+
pendingByTag.put(reactTag, Pending(JavaOnlyMap.deepClone(props), applyItem))
41+
} else {
42+
pending.props.merge(JavaOnlyMap.deepClone(props))
43+
pending.applyItem = applyItem
44+
}
45+
mountItemDispatcher.addMountItem(applyItem)
46+
}
47+
48+
/** Returns the props pending for the view with [props] merged over them, and clears them. */
49+
@UiThread
50+
fun takeMergedWith(reactTag: Int, props: ReadableMap): ReadableMap {
51+
val pending = pendingByTag[reactTag] ?: return props
52+
pendingByTag.remove(reactTag)
53+
pending.props.merge(JavaOnlyMap.deepClone(props))
54+
return pending.props
55+
}
56+
57+
private class Pending(val props: JavaOnlyMap, var applyItem: MountItem)
58+
59+
private inner class ApplyPendingPropsMountItem(private val reactTag: Int) : MountItem {
60+
override fun execute(mountingManager: MountingManager) {
61+
// Nothing to do if an update applied directly already took the props, or if a newer update
62+
// queued a later item. If the view still doesn't exist, the props are dropped.
63+
val pending = pendingByTag[reactTag]
64+
if (pending == null || pending.applyItem !== this) {
65+
return
66+
}
67+
pendingByTag.remove(reactTag)
68+
SynchronousMountItem(reactTag, pending.props).execute(mountingManager)
69+
}
70+
71+
override fun getSurfaceId(): Int = View.NO_ID
72+
73+
override fun toString(): String = "SYNC UPDATE PENDING PROPS [$reactTag]"
74+
}
75+
}

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/BatchedAnimatedPropsMountItem.kt‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ import com.facebook.react.bridge.JavaOnlyArray
1212
import com.facebook.react.bridge.JavaOnlyMap
1313
import com.facebook.react.bridge.ReadableMap
1414
import com.facebook.react.fabric.FabricUIManager.IS_DEVELOPMENT_ENVIRONMENT
15-
import com.facebook.react.fabric.mounting.MountItemDispatcher
1615
import com.facebook.react.fabric.mounting.MountingManager
16+
import com.facebook.react.fabric.mounting.PendingSynchronousProps
1717
import com.facebook.react.uimanager.ViewProps
1818

1919
internal class BatchedAnimatedPropsMountItem(
2020
private val intBuffer: IntArray,
2121
private val doubleBuffer: DoubleArray,
2222
private val rawPropsBuffer: Array<Any?>,
23-
private val mountItemDispatcher: MountItemDispatcher,
23+
private val pendingProps: PendingSynchronousProps,
2424
) : MountItem {
2525

2626
override fun execute(mountingManager: MountingManager) {
2727
decode { viewTag, props ->
2828
try {
29-
if (!mountingManager.updateAnimatedPropsSynchronously(viewTag, props)) {
30-
mountItemDispatcher.addMountItem(SynchronousMountItem(viewTag, props))
29+
val effectiveProps =
30+
if (pendingProps.hasPending(viewTag) && mountingManager.getViewExists(viewTag)) {
31+
pendingProps.takeMergedWith(viewTag, props)
32+
} else {
33+
props
34+
}
35+
if (!mountingManager.updateAnimatedPropsSynchronously(viewTag, effectiveProps)) {
36+
pendingProps.add(viewTag, props)
3137
}
3238
} catch (ex: Exception) {
3339
// Match SynchronousMountItem's handling of view-manager failures.

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/BatchedAnimatedPropsMountItemTest.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.facebook.react.bridge.JavaOnlyMap
1111
import com.facebook.react.bridge.ReadableMap
1212
import com.facebook.react.fabric.mounting.MountItemDispatcher
1313
import com.facebook.react.fabric.mounting.MountingManager
14+
import com.facebook.react.fabric.mounting.PendingSynchronousProps
1415
import com.facebook.react.fabric.mounting.mountitems.BatchedAnimatedPropsMountItem
1516
import org.assertj.core.api.Assertions.assertThat
1617
import org.junit.Test
@@ -287,11 +288,12 @@ class BatchedAnimatedPropsMountItemTest {
287288
),
288289
doubleArrayOf(0.25, 0.75),
289290
emptyArray(),
290-
dispatcher,
291+
PendingSynchronousProps(dispatcher),
291292
)
292293
.execute(manager)
293294
verify(dispatcher).addMountItem(org.mockito.kotlin.any())
294295
verify(manager).updateAnimatedPropsSynchronously(12, second)
296+
verify(manager, org.mockito.kotlin.never()).getViewExists(org.mockito.kotlin.any())
295297
}
296298

297299
@Test
@@ -313,7 +315,7 @@ class BatchedAnimatedPropsMountItemTest {
313315
),
314316
doubleArrayOf(0.25, 0.75),
315317
emptyArray(),
316-
dispatcher,
318+
PendingSynchronousProps(dispatcher),
317319
)
318320
.execute(manager)
319321
verify(manager).updateAnimatedPropsSynchronously(12, JavaOnlyMap.of("opacity", 0.75))
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
@file:Suppress("DEPRECATION")
9+
10+
package com.facebook.react.fabric
11+
12+
import android.view.View
13+
import com.facebook.react.ReactRootView
14+
import com.facebook.react.bridge.JavaOnlyArray
15+
import com.facebook.react.bridge.JavaOnlyMap
16+
import com.facebook.react.bridge.ReactTestHelper
17+
import com.facebook.react.fabric.mounting.MountItemDispatcher
18+
import com.facebook.react.fabric.mounting.MountingManager
19+
import com.facebook.react.fabric.mounting.SurfaceMountingManager
20+
import com.facebook.react.fabric.mounting.mountitems.MountItem
21+
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
22+
import com.facebook.react.uimanager.ThemedReactContext
23+
import com.facebook.react.uimanager.ViewManager
24+
import com.facebook.react.uimanager.ViewManagerRegistry
25+
import com.facebook.react.views.view.ReactViewManager
26+
import com.facebook.testutils.fakes.FakeBatchEventDispatchedListener
27+
import com.facebook.testutils.shadows.ShadowNativeLoader
28+
import com.facebook.testutils.shadows.ShadowNativeMap
29+
import com.facebook.testutils.shadows.ShadowPerformanceTracer
30+
import com.facebook.testutils.shadows.ShadowReadableNativeArray
31+
import com.facebook.testutils.shadows.ShadowReadableNativeMap
32+
import com.facebook.testutils.shadows.ShadowSoLoader
33+
import com.facebook.testutils.shadows.ShadowWritableNativeArray
34+
import com.facebook.testutils.shadows.ShadowWritableNativeMap
35+
import org.assertj.core.api.Assertions.assertThat
36+
import org.junit.Before
37+
import org.junit.Test
38+
import org.junit.runner.RunWith
39+
import org.robolectric.RobolectricTestRunner
40+
import org.robolectric.annotation.Config
41+
import org.robolectric.util.ReflectionHelpers
42+
43+
/**
44+
* Synchronous props updates for a view that doesn't exist yet wait for the next batch of mount
45+
* items. The view can mount and get a newer update applied directly before that batch runs.
46+
*/
47+
@RunWith(RobolectricTestRunner::class)
48+
@Config(
49+
shadows =
50+
[
51+
ShadowSoLoader::class,
52+
ShadowNativeLoader::class,
53+
ShadowNativeMap::class,
54+
ShadowWritableNativeMap::class,
55+
ShadowReadableNativeMap::class,
56+
ShadowWritableNativeArray::class,
57+
ShadowReadableNativeArray::class,
58+
ShadowPerformanceTracer::class,
59+
],
60+
)
61+
class FabricUIManagerSynchronousPropsTest {
62+
private val surfaceId = 1
63+
private val tag = 42
64+
private lateinit var themedReactContext: ThemedReactContext
65+
private lateinit var underTest: FabricUIManager
66+
67+
private val mountingManager: MountingManager
68+
get() = ReflectionHelpers.getField(underTest, "mMountingManager")
69+
70+
private val dispatcher: MountItemDispatcher
71+
get() = ReflectionHelpers.getField(underTest, "mMountItemDispatcher")
72+
73+
@Before
74+
fun setUp() {
75+
ReactNativeFeatureFlagsForTests.setUp()
76+
val reactContext = ReactTestHelper.createTestReactApplicationContext()
77+
themedReactContext = ThemedReactContext(reactContext, reactContext, null, -1)
78+
underTest =
79+
FabricUIManager(
80+
reactContext,
81+
ViewManagerRegistry(listOf<ViewManager<*, *>>(ReactViewManager())),
82+
FakeBatchEventDispatchedListener(),
83+
)
84+
}
85+
86+
private fun mountView(): SurfaceMountingManager {
87+
mountingManager.startSurface(surfaceId, themedReactContext, ReactRootView(themedReactContext))
88+
val smm = mountingManager.getSurfaceManagerEnforced(surfaceId, "test")
89+
smm.preallocateView("RCTView", tag, JavaOnlyMap.of(), null, true)
90+
smm.addViewAt(surfaceId, tag, 0)
91+
return smm
92+
}
93+
94+
private fun updateViewBatch(opacity: Double) {
95+
val method =
96+
FabricUIManager::class
97+
.java
98+
.getDeclaredMethod(
99+
"synchronouslyUpdateViewBatch",
100+
IntArray::class.java,
101+
DoubleArray::class.java,
102+
Array<Any>::class.java,
103+
)
104+
method.isAccessible = true
105+
method.invoke(
106+
underTest,
107+
intArrayOf(CMD_START_OF_VIEW, tag, CMD_OPACITY, CMD_END_OF_VIEW),
108+
doubleArrayOf(opacity),
109+
arrayOf<Any?>(),
110+
)
111+
}
112+
113+
@Test
114+
fun queuedUpdate_doesNotOverwriteNewerDirectUpdate() {
115+
underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.3))
116+
val smm = mountView()
117+
underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.6))
118+
119+
dispatcher.tryDispatchMountItems()
120+
121+
assertThat(smm.getView(tag).alpha).isEqualTo(0.6f)
122+
// The synchronous mount props override keeps the newer value over a Fabric commit.
123+
smm.updateProps(tag, JavaOnlyMap.of("opacity", 1.0))
124+
assertThat(smm.getView(tag).alpha).isEqualTo(0.6f)
125+
}
126+
127+
@Test
128+
fun queuedBatchedUpdate_doesNotOverwriteNewerDirectUpdate() {
129+
updateViewBatch(opacity = 0.3)
130+
val smm = mountView()
131+
updateViewBatch(opacity = 0.6)
132+
133+
dispatcher.tryDispatchMountItems()
134+
135+
assertThat(smm.getView(tag).alpha).isEqualTo(0.6f)
136+
smm.updateProps(tag, JavaOnlyMap.of("opacity", 1.0))
137+
assertThat(smm.getView(tag).alpha).isEqualTo(0.6f)
138+
}
139+
140+
@Test
141+
fun queuedUpdates_waitForTheMountItemThatCreatesTheView() {
142+
underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.3))
143+
lateinit var smm: SurfaceMountingManager
144+
dispatcher.addMountItem(
145+
object : MountItem {
146+
override fun execute(mountingManager: MountingManager) {
147+
smm = mountView()
148+
}
149+
150+
override fun getSurfaceId(): Int = View.NO_ID
151+
}
152+
)
153+
underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.6))
154+
155+
dispatcher.tryDispatchMountItems()
156+
157+
assertThat(smm.getView(tag).alpha).isEqualTo(0.6f)
158+
}
159+
160+
@Test
161+
fun queuedUpdates_mergeInOrderAndKeepNullResets() {
162+
underTest.synchronouslyUpdateViewOnUIThread(
163+
tag,
164+
JavaOnlyMap.of("opacity", 0.3, "transform", JavaOnlyArray.of(JavaOnlyMap.of("scale", 2.0))),
165+
)
166+
underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", null))
167+
val smm = mountView()
168+
169+
dispatcher.tryDispatchMountItems()
170+
171+
val view = smm.getView(tag)
172+
assertThat(view.alpha).isEqualTo(1f)
173+
assertThat(view.scaleX).isEqualTo(2f)
174+
// The reset also cleared the stored opacity, so a Fabric commit applies its own value.
175+
smm.updateProps(tag, JavaOnlyMap.of("opacity", 0.5))
176+
assertThat(view.alpha).isEqualTo(0.5f)
177+
}
178+
179+
private companion object {
180+
// Keep in sync with BatchedAnimatedPropsMountItem.
181+
const val CMD_START_OF_VIEW = 1
182+
const val CMD_END_OF_VIEW = 4
183+
const val CMD_OPACITY = 10
184+
}
185+
}

0 commit comments

Comments
 (0)