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,212 @@
/**
* 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 useSharedAnimatedBackend:true enableAnimatedPropsInLayoutReads:true updateRuntimeShadowNodeReferencesOnCommitThread:*
* @flow strict-local
* @format
*/

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

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

import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags';
import * as Fantom from '@react-native/fantom';
import nullthrows from 'nullthrows';
import * as React from 'react';
import {createRef, useLayoutEffect, useState} from 'react';
import {Animated, Easing, View, useAnimatedValue} from 'react-native';

// Deferred start outputs the initial value on the first animation frame and
// re-anchors timing on the second, which delays the animation by one frame.
const DEFERRED_START_MS =
ReactNativeFeatureFlags.animatedDeferStartOfTimingAnimations() ? 16 : 0;

type Layout = {pageY: number, height: number};

function measure(element: HostInstance): Layout {
let layout: ?Layout;
element.measure((x, y, width, height, pageX, pageY) => {
layout = {pageY, height};
});
return nullthrows(layout);
}

test('layout reads include the transform of a running native-driven animation', () => {
let _translateY;
const childRef = createRef<HostInstance>();

function MyApp() {
const translateY = useAnimatedValue(0);
_translateY = translateY;
return (
<Animated.View style={{transform: [{translateY}]}}>
<View ref={childRef} style={{width: 100, height: 50}} />
</Animated.View>
);
}

const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(<MyApp />);
});

const childElement = nullthrows(childRef.current);

expect(measure(childElement).pageY).toBe(0);

let animation;
Fantom.runTask(() => {
animation = Animated.timing(_translateY, {
toValue: 100,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
});
animation.start();
});

Fantom.unstable_produceFramesForDuration(500 + DEFERRED_START_MS);

// Nothing was committed to the shadow tree since the animation started.
expect(measure(childElement).pageY).toBeCloseTo(50, 0.001);
expect(measure(childElement).height).toBe(50);
expect(childElement.getBoundingClientRect().y).toBeCloseTo(50, 0.001);

Fantom.runTask(() => {
animation?.stop();
});
Fantom.runWorkLoop();
});

test('layout reads include the animated props applied after a re-render', () => {
let _translateY;
let _setHeight;
const childRef = createRef<HostInstance>();

function MyApp() {
const translateY = useAnimatedValue(0);
const [height, setHeight] = useState(50);
_translateY = translateY;
_setHeight = setHeight;
return (
<Animated.View style={{transform: [{translateY}]}}>
<View ref={childRef} style={{width: 100, height}} />
</Animated.View>
);
}

const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(<MyApp />);
});

const childElement = nullthrows(childRef.current);

let animation;
Fantom.runTask(() => {
animation = Animated.timing(_translateY, {
toValue: 100,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
});
animation.start();
});

Fantom.unstable_produceFramesForDuration(500 + DEFERRED_START_MS);

Fantom.runTask(() => {
_setHeight(80);
});

expect(measure(childElement).pageY).toBeCloseTo(50, 0.001);
expect(measure(childElement).height).toBe(80);

Fantom.unstable_produceFramesForDuration(250);

expect(measure(childElement).pageY).toBeCloseTo(75, 0.001);

Fantom.runTask(() => {
animation?.stop();
});
Fantom.runWorkLoop();
});

test('layout reads after a commit in the same task include the commit and the animated props', () => {
let _translateY;
const childRef = createRef<HostInstance>();

function MyApp(props: {
height: number,
onRender?: () => void,
onLayoutEffect?: () => void,
}) {
const {height, onRender, onLayoutEffect} = props;
const translateY = useAnimatedValue(0);
_translateY = translateY;
onRender?.();
useLayoutEffect(() => {
onLayoutEffect?.();
});
return (
<Animated.View style={{transform: [{translateY}]}}>
<View ref={childRef} style={{width: 100, height}} />
</Animated.View>
);
}

const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(<MyApp height={50} />);
});

const childElement = nullthrows(childRef.current);

let animation;
Fantom.runTask(() => {
animation = Animated.timing(_translateY, {
toValue: 100,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
});
animation.start();
});

Fantom.unstable_produceFramesForDuration(500 + DEFERRED_START_MS);

let layoutInRender: ?Layout;
let layoutInLayoutEffect: ?Layout;
Fantom.runTask(() => {
root.render(
<MyApp
height={80}
onRender={() => {
layoutInRender = measure(childElement);
}}
onLayoutEffect={() => {
layoutInLayoutEffect = measure(childElement);
}}
/>,
);
});

// Render and the layout effect run in the same task, so the read in render
// captures the revision before the commit.
expect(nullthrows(layoutInRender).height).toBe(50);
expect(nullthrows(layoutInRender).pageY).toBeCloseTo(50, 0.001);
expect(nullthrows(layoutInLayoutEffect).height).toBe(80);
expect(nullthrows(layoutInLayoutEffect).pageY).toBeCloseTo(50, 0.001);

Fantom.runTask(() => {
animation?.stop();
});
Fantom.runWorkLoop();
});
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<<3a6a61e2cc2bf01a35ab5c3919462ad5>>
* @generated SignedSource<<898f1742589c8b5a676355c5989482ef>>
*/

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

/**
* When enabled with the shared animated backend, layout reads from JS (measure, getBoundingClientRect, etc.) include the animated props that were applied to views but not committed to the shadow tree yet.
*/
@JvmStatic
public fun enableAnimatedPropsInLayoutReads(): Boolean = accessor.enableAnimatedPropsInLayoutReads()

/**
* Runs multi-source image disk-cache checks asynchronously on Android.
*/
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<<fe927f4acf804e05b74dbe606e39746f>>
* @generated SignedSource<<d96305205bac4cd5278434c64abd3817>>
*/

/**
Expand Down Expand Up @@ -36,6 +36,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
private var enableAccumulatedUpdatesInRawPropsAndroidCache: Boolean? = null
private var enableAliasedTextRoleInheritanceCache: Boolean? = null
private var enableAndroidTextMeasurementOptimizationsCache: Boolean? = null
private var enableAnimatedPropsInLayoutReadsCache: Boolean? = null
private var enableAsyncDiskCacheCheckInMultiSourceImageAndroidCache: Boolean? = null
private var enableBridgelessArchitectureCache: Boolean? = null
private var enableCppPropsIteratorSetterCache: Boolean? = null
Expand Down Expand Up @@ -269,6 +270,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
return cached
}

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

override fun enableAsyncDiskCacheCheckInMultiSourceImageAndroid(): Boolean {
var cached = enableAsyncDiskCacheCheckInMultiSourceImageAndroidCache
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<<b58702a0f594f409c0d20fd497395fde>>
* @generated SignedSource<<0035705a72adfab15f4f11c054879998>>
*/

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

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

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

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

@DoNotStrip @JvmStatic public external fun enableBridgelessArchitecture(): 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<<834b0e9533cbc775a1c12b86140d718d>>
* @generated SignedSource<<cacb9f106fed3baaf13591f8c96941ea>>
*/

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

override fun enableAndroidTextMeasurementOptimizations(): Boolean = true

override fun enableAnimatedPropsInLayoutReads(): Boolean = false

override fun enableAsyncDiskCacheCheckInMultiSourceImageAndroid(): Boolean = false

override fun enableBridgelessArchitecture(): Boolean = true
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<<15c65d4497fd9f36f8902efb2812b3b2>>
* @generated SignedSource<<6d296817f1c53c51d708b18f2bc30eed>>
*/

/**
Expand Down Expand Up @@ -40,6 +40,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
private var enableAccumulatedUpdatesInRawPropsAndroidCache: Boolean? = null
private var enableAliasedTextRoleInheritanceCache: Boolean? = null
private var enableAndroidTextMeasurementOptimizationsCache: Boolean? = null
private var enableAnimatedPropsInLayoutReadsCache: Boolean? = null
private var enableAsyncDiskCacheCheckInMultiSourceImageAndroidCache: Boolean? = null
private var enableBridgelessArchitectureCache: Boolean? = null
private var enableCppPropsIteratorSetterCache: Boolean? = null
Expand Down Expand Up @@ -289,6 +290,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
return cached
}

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

override fun enableAsyncDiskCacheCheckInMultiSourceImageAndroid(): Boolean {
var cached = enableAsyncDiskCacheCheckInMultiSourceImageAndroidCache
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<<d5a27cf5db5bc3d3a0dd8b40d0fbe9a7>>
* @generated SignedSource<<7559bba24988e7f6a9e3339d15d15402>>
*/

/**
Expand All @@ -27,6 +27,8 @@ public open class ReactNativeFeatureFlagsOverrides_RNOSS_Experimental_Android :

override fun disableIdleMountItemFrameCallbackRearmAndroid(): Boolean = true

override fun enableAnimatedPropsInLayoutReads(): Boolean = true

override fun enableFlexboxAutoMinSizeInStrictMode(): Boolean = true

override fun preventShadowTreeCommitExhaustion(): Boolean = true
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<<89f683cb28a7d1ef52c86faa96f1107c>>
* @generated SignedSource<<504ecb63acaeebfcf3c56c2db7e6df41>>
*/

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

@DoNotStrip public fun enableAndroidTextMeasurementOptimizations(): Boolean

@DoNotStrip public fun enableAnimatedPropsInLayoutReads(): Boolean

@DoNotStrip public fun enableAsyncDiskCacheCheckInMultiSourceImageAndroid(): Boolean

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