Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @fantom_flags keepLayoutWhenAppendingUnchangedChildren:*
* @fantom_flags updateRuntimeShadowNodeReferencesOnCommit:*
* @flow strict-local
* @format
*/

import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';

import type {HostInstance} from 'react-native';

import * as Fantom from '@react-native/fantom';
import * as React from 'react';
import {Text, View} from 'react-native';

// React rebuilds the children of an updated node by cloning it without
// children and appending them one by one. With
// `keepLayoutWhenAppendingUnchangedChildren`, the clone stays clean when the
// appended children lay out as before. These tests run with the flag off and
// on, render a sequence of updates, and check that the layout matches a fresh
// render of the last update.

// `null` for a ref whose element is not rendered in the last update.
function rectOf(ref: {
current: HostInstance | null,
}): ?[number, number, number, number] {
if (ref.current == null) {
return null;
}
const rect = ref.current.getBoundingClientRect();
return [rect.x, rect.y, rect.width, rect.height];
}

type Refs = ReadonlyArray<React.RefObject<HostInstance | null>>;

function createRefs(count: number): Refs {
return Array.from({length: count}, () => React.createRef<HostInstance>());
}

function expectSameLayoutAsFreshRender(
render: (step: number, refs: Refs) => React.MixedElement,
stepCount: number,
refCount: number,
): void {
const refs = createRefs(refCount);
const root = Fantom.createRoot();
for (let step = 0; step < stepCount; step++) {
Fantom.runTask(() => {
root.render(render(step, refs));
});
}

const freshRefs = createRefs(refCount);
const freshRoot = Fantom.createRoot();
Fantom.runTask(() => {
freshRoot.render(render(stepCount - 1, freshRefs));
});

expect(refs.map(rectOf)).toEqual(freshRefs.map(rectOf));

root.destroy();
freshRoot.destroy();
}

// A row of `count` boxes inside two levels of containers, so that a change to
// one box rebuilds the children of every container above it.
function Boxes({
count,
colorIndex,
wideIndex,
hidden,
refs,
}: {
count: number,
colorIndex: number,
wideIndex: number,
hidden?: boolean,
refs: Refs,
}): React.MixedElement {
return (
<View ref={refs[0]} style={{padding: 2}}>
<View
ref={refs[1]}
style={{
flexDirection: 'row',
display: hidden === true ? 'none' : 'flex',
}}>
{Array.from({length: count}, (_, i) => (
<View
key={i}
ref={refs[2 + i]}
style={{
width: i === wideIndex ? 20 : 10,
height: 10,
backgroundColor: i === colorIndex ? 'red' : 'blue',
}}
/>
))}
</View>
<View ref={refs[2 + count]} style={{height: 5}} />
</View>
);
}

describe('keepLayoutWhenAppendingUnchangedChildren', () => {
it('lays out a size change that follows paint-only changes', () => {
expectSameLayoutAsFreshRender(
(step, refs) => (
<Boxes
count={4}
colorIndex={step % 4}
wideIndex={step >= 3 ? 2 : -1}
refs={refs}
/>
),
6,
7,
);
});

it('lays out removed, added and reordered children', () => {
const counts = [5, 5, 3, 3, 6, 1];
expectSameLayoutAsFreshRender(
(step, refs) => (
<View ref={refs[0]} style={{padding: 1}}>
<View ref={refs[1]} style={{flexDirection: 'row'}}>
{Array.from({length: counts[step]}, (_, i) => {
const id = step === 4 ? counts[step] - 1 - i : i;
return (
<View
key={id}
ref={refs[2 + i]}
style={{width: 4 + id, height: 4 + (id % 2)}}
/>
);
})}
</View>
</View>
),
counts.length,
8,
);
});

it('lays out children that changed while their parent was hidden', () => {
expectSameLayoutAsFreshRender(
(step, refs) => (
<Boxes
count={step >= 2 ? 3 : 5}
colorIndex={step}
wideIndex={step >= 2 ? 0 : -1}
hidden={step === 1 || step === 2}
refs={refs}
/>
),
4,
8,
);
});

it('lays out an inline view whose children were removed', () => {
expectSameLayoutAsFreshRender(
(step, refs) => (
<Text ref={refs[0]}>
before
<View ref={refs[1]} style={{flexDirection: 'row'}}>
{Array.from({length: step === 0 ? 3 : 1}, (_, i) => (
<View
key={i}
style={{
width: 8,
height: 8,
backgroundColor: step === 2 ? 'red' : 'blue',
}}
/>
))}
</View>
after
</Text>
),
3,
2,
);
});

it('lays out a tree rendered again after the root was emptied', () => {
const refs = createRefs(7);
const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(<Boxes count={4} colorIndex={0} wideIndex={1} refs={refs} />);
});
Fantom.runTask(() => {
// An empty fragment commits the root without children.
root.render(<></>);
});
Fantom.runTask(() => {
root.render(<Boxes count={4} colorIndex={1} wideIndex={2} refs={refs} />);
});

const freshRefs = createRefs(7);
const freshRoot = Fantom.createRoot();
Fantom.runTask(() => {
freshRoot.render(
<Boxes count={4} colorIndex={1} wideIndex={2} refs={freshRefs} />,
);
});

expect(refs.map(rectOf)).toEqual(freshRefs.map(rectOf));

root.destroy();
freshRoot.destroy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<9a675a67a9ce5e7ca443dd319e2af91a>>
* @generated SignedSource<<ba5599ac2b643b1549f82cfc22a9757a>>
*/

/**
Expand Down Expand Up @@ -468,6 +468,12 @@ public object ReactNativeFeatureFlags {
@JvmStatic
public fun fuseboxWebSocketEventsEnabled(): Boolean = accessor.fuseboxWebSocketEventsEnabled()

/**
* Keep a node clean when React appends children that lay out like the ones they replace, as when children are passed into the clone, so paint-only updates skip layout.
*/
@JvmStatic
public fun keepLayoutWhenAppendingUnchangedChildren(): Boolean = accessor.keepLayoutWhenAppendingUnchangedChildren()

/**
* Keep JS timers running on Android while the React host is paused, stopping them only when the React instance is destroyed. Matches iOS, where timers do not depend on host lifecycle.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<6b668cbb1ef60126d7e08b2d853b1262>>
* @generated SignedSource<<07f12fce2ecff5c8c0c11830290c507a>>
*/

/**
Expand Down Expand Up @@ -93,6 +93,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
private var fuseboxFrameRecordingEnabledCache: Boolean? = null
private var fuseboxScreenshotCaptureEnabledCache: Boolean? = null
private var fuseboxWebSocketEventsEnabledCache: Boolean? = null
private var keepLayoutWhenAppendingUnchangedChildrenCache: Boolean? = null
private var keepTimersRunningWhileHostPausedAndroidCache: Boolean? = null
private var optimizedAnimatedPropUpdatesCache: Boolean? = null
private var overrideBySynchronousMountPropsAtMountingAndroidCache: Boolean? = null
Expand Down Expand Up @@ -781,6 +782,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun keepLayoutWhenAppendingUnchangedChildren(): Boolean {
var cached = keepLayoutWhenAppendingUnchangedChildrenCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.keepLayoutWhenAppendingUnchangedChildren()
keepLayoutWhenAppendingUnchangedChildrenCache = cached
}
return cached
}

override fun keepTimersRunningWhileHostPausedAndroid(): Boolean {
var cached = keepTimersRunningWhileHostPausedAndroidCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<0890a0b7979d00172b0a79abd94f6c3e>>
* @generated SignedSource<<26c9819118c9de8461d38131fb3c01a3>>
*/

/**
Expand Down Expand Up @@ -174,6 +174,8 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun fuseboxWebSocketEventsEnabled(): Boolean

@DoNotStrip @JvmStatic public external fun keepLayoutWhenAppendingUnchangedChildren(): Boolean

@DoNotStrip @JvmStatic public external fun keepTimersRunningWhileHostPausedAndroid(): Boolean

@DoNotStrip @JvmStatic public external fun optimizedAnimatedPropUpdates(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<3ca183dbeeb3fdeac98780267df9dff2>>
* @generated SignedSource<<4de5760364de68c35f64abba267aa667>>
*/

/**
Expand Down Expand Up @@ -169,6 +169,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi

override fun fuseboxWebSocketEventsEnabled(): Boolean = true

override fun keepLayoutWhenAppendingUnchangedChildren(): Boolean = false

override fun keepTimersRunningWhileHostPausedAndroid(): Boolean = false

override fun optimizedAnimatedPropUpdates(): Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<ba5e55492e7f521229dd62ac1274924e>>
* @generated SignedSource<<361ac70bf8504f6ae2a0cb36abbcebc8>>
*/

/**
Expand Down Expand Up @@ -97,6 +97,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
private var fuseboxFrameRecordingEnabledCache: Boolean? = null
private var fuseboxScreenshotCaptureEnabledCache: Boolean? = null
private var fuseboxWebSocketEventsEnabledCache: Boolean? = null
private var keepLayoutWhenAppendingUnchangedChildrenCache: Boolean? = null
private var keepTimersRunningWhileHostPausedAndroidCache: Boolean? = null
private var optimizedAnimatedPropUpdatesCache: Boolean? = null
private var overrideBySynchronousMountPropsAtMountingAndroidCache: Boolean? = null
Expand Down Expand Up @@ -858,6 +859,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
return cached
}

override fun keepLayoutWhenAppendingUnchangedChildren(): Boolean {
var cached = keepLayoutWhenAppendingUnchangedChildrenCache
if (cached == null) {
cached = currentProvider.keepLayoutWhenAppendingUnchangedChildren()
accessedFeatureFlags.add("keepLayoutWhenAppendingUnchangedChildren")
keepLayoutWhenAppendingUnchangedChildrenCache = cached
}
return cached
}

override fun keepTimersRunningWhileHostPausedAndroid(): Boolean {
var cached = keepTimersRunningWhileHostPausedAndroidCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<8d21b1b7d2bd5c5fe3a111c0d411f742>>
* @generated SignedSource<<7fdb245cc543d0c56b955d96f86b98cd>>
*/

/**
Expand Down Expand Up @@ -169,6 +169,8 @@ public interface ReactNativeFeatureFlagsProvider {

@DoNotStrip public fun fuseboxWebSocketEventsEnabled(): Boolean

@DoNotStrip public fun keepLayoutWhenAppendingUnchangedChildren(): Boolean

@DoNotStrip public fun keepTimersRunningWhileHostPausedAndroid(): Boolean

@DoNotStrip public fun optimizedAnimatedPropUpdates(): Boolean
Expand Down
Loading
Loading