From f3f652daabc4a1d374190165ac38715ad31a5214 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Mon, 9 Sep 2024 20:34:57 -0700 Subject: [PATCH] Animated: Create Experimental Props Allowlist (#46374) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46374 Creates an experimental `allowlist` parameter that can be supplied to a new `unstable_createAnimatedComponentWithAllowlist` method for use in new experiments to evaluate the impact on product performance and developer experience. When it is provided, only the props and styles in `allowlist` will be inspected for `AnimatedNode` values. The hypothesis for this change is that restricting the search space for `AnimatedProps` to an allowlist will significantly reduce the runtime overhead of using `Animated` components. Changelog: [General][Added] - Created an experimental (unstable) method for allowlisting props when using `Animated` Reviewed By: javache Differential Revision: D62117424 fbshipit-source-id: bdd656be1fdc7454360035627644606cb00d33c0 --- .../Animated/createAnimatedComponent.js | 13 ++++ .../Libraries/Animated/nodes/AnimatedProps.js | 61 +++++++++++++------ .../Libraries/Animated/nodes/AnimatedStyle.js | 57 ++++++++++++----- .../Libraries/Animated/useAnimatedProps.js | 8 +-- .../__snapshots__/public-api-test.js.snap | 30 +++++++-- 5 files changed, 127 insertions(+), 42 deletions(-) diff --git a/packages/react-native/Libraries/Animated/createAnimatedComponent.js b/packages/react-native/Libraries/Animated/createAnimatedComponent.js index 8c9fbea9c57..72fd367049b 100644 --- a/packages/react-native/Libraries/Animated/createAnimatedComponent.js +++ b/packages/react-native/Libraries/Animated/createAnimatedComponent.js @@ -8,6 +8,8 @@ * @format */ +import type {AnimatedPropsAllowlist} from './nodes/AnimatedProps'; + import composeStyles from '../../src/private/styles/composeStyles'; import View from '../Components/View/View'; import useMergeRefs from '../Utilities/useMergeRefs'; @@ -32,12 +34,23 @@ export type AnimatedComponentType< export default function createAnimatedComponent( Component: React.AbstractComponent, +): AnimatedComponentType { + return unstable_createAnimatedComponentWithAllowlist(Component, null); +} + +export function unstable_createAnimatedComponentWithAllowlist< + TProps: {...}, + TInstance, +>( + Component: React.AbstractComponent, + allowlist: ?AnimatedPropsAllowlist, ): AnimatedComponentType { const AnimatedComponent = React.forwardRef, TInstance>( (props, forwardedRef) => { const [reducedProps, callbackRef] = useAnimatedProps( // $FlowFixMe[incompatible-call] props, + allowlist, ); const ref = useMergeRefs(callbackRef, forwardedRef); diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js b/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js index fc579fb41b5..83709c6640a 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js @@ -9,6 +9,7 @@ */ import type {PlatformConfig} from '../AnimatedPlatformConfig'; +import type {AnimatedStyleAllowlist} from './AnimatedStyle'; import {findNodeHandle} from '../../ReactNative/RendererProxy'; import {AnimatedEvent} from '../AnimatedEvent'; @@ -18,9 +19,15 @@ import AnimatedObject from './AnimatedObject'; import AnimatedStyle from './AnimatedStyle'; import invariant from 'invariant'; -function createAnimatedProps(inputProps: { - [string]: mixed, -}): [$ReadOnlyArray, $ReadOnlyArray, {[string]: mixed}] { +export type AnimatedPropsAllowlist = $ReadOnly<{ + style?: ?AnimatedStyleAllowlist, + [string]: true, +}>; + +function createAnimatedProps( + inputProps: {[string]: mixed}, + allowlist: ?AnimatedPropsAllowlist, +): [$ReadOnlyArray, $ReadOnlyArray, {[string]: mixed}] { const nodeKeys: Array = []; const nodes: Array = []; const props: {[string]: mixed} = {}; @@ -30,20 +37,36 @@ function createAnimatedProps(inputProps: { const key = keys[ii]; const value = inputProps[key]; - let node; - if (key === 'style') { - node = AnimatedStyle.from(value); - } else if (value instanceof AnimatedNode) { - node = value; + if (allowlist == null || Object.hasOwn(allowlist, key)) { + let node; + if (key === 'style') { + node = AnimatedStyle.from(value, allowlist?.style); + } else if (value instanceof AnimatedNode) { + node = value; + } else { + node = AnimatedObject.from(value); + } + if (node == null) { + props[key] = value; + } else { + nodeKeys.push(key); + nodes.push(node); + props[key] = node; + } } else { - node = AnimatedObject.from(value); - } - if (node == null) { + if (__DEV__) { + // WARNING: This is a potentially expensive check that we should only + // do in development. Without this check in development, it might be + // difficult to identify which props need to be allowlisted. + if (AnimatedObject.from(inputProps[key]) != null) { + console.error( + `AnimatedProps: ${key} is not allowlisted for animation, but it ` + + 'contains AnimatedNode values; props allowing animation: ', + allowlist, + ); + } + } props[key] = value; - } else { - nodeKeys.push(key); - nodes.push(node); - props[key] = node; } } @@ -57,9 +80,13 @@ export default class AnimatedProps extends AnimatedNode { #nodes: $ReadOnlyArray; #props: {[string]: mixed}; - constructor(inputProps: {[string]: mixed}, callback: () => void) { + constructor( + inputProps: {[string]: mixed}, + callback: () => void, + allowlist?: ?AnimatedPropsAllowlist, + ) { super(); - const [nodeKeys, nodes, props] = createAnimatedProps(inputProps); + const [nodeKeys, nodes, props] = createAnimatedProps(inputProps, allowlist); this.#nodeKeys = nodeKeys; this.#nodes = nodes; this.#props = props; diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js b/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js index 13aedf47c31..9086986d17a 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js @@ -19,8 +19,11 @@ import AnimatedObject from './AnimatedObject'; import AnimatedTransform from './AnimatedTransform'; import AnimatedWithChildren from './AnimatedWithChildren'; +export type AnimatedStyleAllowlist = $ReadOnly<{[string]: true}>; + function createAnimatedStyle( inputStyle: {[string]: mixed}, + allowlist: ?AnimatedStyleAllowlist, keepUnanimatedValues: boolean, ): [$ReadOnlyArray, $ReadOnlyArray, {[string]: mixed}] { const nodeKeys: Array = []; @@ -32,25 +35,43 @@ function createAnimatedStyle( const key = keys[ii]; const value = inputStyle[key]; - let node; - if (value != null && key === 'transform') { - node = ReactNativeFeatureFlags.shouldUseAnimatedObjectForTransform() - ? AnimatedObject.from(value) - : // $FlowFixMe[incompatible-call] - `value` is mixed. - AnimatedTransform.from(value); - } else if (value instanceof AnimatedNode) { - node = value; + if (allowlist == null || Object.hasOwn(allowlist, key)) { + let node; + if (value != null && key === 'transform') { + node = ReactNativeFeatureFlags.shouldUseAnimatedObjectForTransform() + ? AnimatedObject.from(value) + : // $FlowFixMe[incompatible-call] - `value` is mixed. + AnimatedTransform.from(value); + } else if (value instanceof AnimatedNode) { + node = value; + } else { + node = AnimatedObject.from(value); + } + if (node == null) { + if (keepUnanimatedValues) { + style[key] = value; + } + } else { + nodeKeys.push(key); + nodes.push(node); + style[key] = node; + } } else { - node = AnimatedObject.from(value); - } - if (node == null) { + if (__DEV__) { + // WARNING: This is a potentially expensive check that we should only + // do in development. Without this check in development, it might be + // difficult to identify which styles need to be allowlisted. + if (AnimatedObject.from(inputStyle[key]) != null) { + console.error( + `AnimatedStyle: ${key} is not allowlisted for animation, but it ` + + 'contains AnimatedNode values; styles allowing animation: ', + allowlist, + ); + } + } if (keepUnanimatedValues) { style[key] = value; } - } else { - nodeKeys.push(key); - nodes.push(node); - style[key] = node; } } @@ -67,13 +88,17 @@ export default class AnimatedStyle extends AnimatedWithChildren { * Creates an `AnimatedStyle` if `value` contains `AnimatedNode` instances. * Otherwise, returns `null`. */ - static from(inputStyle: any): ?AnimatedStyle { + static from( + inputStyle: any, + allowlist: ?AnimatedStyleAllowlist, + ): ?AnimatedStyle { const flatStyle = flattenStyle(inputStyle); if (flatStyle == null) { return null; } const [nodeKeys, nodes, style] = createAnimatedStyle( flatStyle, + allowlist, Platform.OS !== 'web', ); if (nodes.length === 0) { diff --git a/packages/react-native/Libraries/Animated/useAnimatedProps.js b/packages/react-native/Libraries/Animated/useAnimatedProps.js index df3df01a3c9..acc5c6a4f1d 100644 --- a/packages/react-native/Libraries/Animated/useAnimatedProps.js +++ b/packages/react-native/Libraries/Animated/useAnimatedProps.js @@ -8,8 +8,7 @@ * @format */ -'use strict'; - +import type {AnimatedPropsAllowlist} from './nodes/AnimatedProps'; import type {EventSubscription} from '../EventEmitter/NativeEventEmitter'; import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags'; @@ -43,6 +42,7 @@ type AnimatedValueListeners = Array<{ export default function useAnimatedProps( props: TProps, + allowlist?: ?AnimatedPropsAllowlist, ): [ReducedProps, CallbackRef] { const [, scheduleUpdate] = useReducer(count => count + 1, 0); const onUpdateRef = useRef void>(null); @@ -53,8 +53,8 @@ export default function useAnimatedProps( // same name property name as styles, so we can probably continue doing that. // The ordering of other props *should* not matter. const node = useMemo( - () => new AnimatedProps(props, () => onUpdateRef.current?.()), - [props], + () => new AnimatedProps(props, () => onUpdateRef.current?.(), allowlist), + [allowlist, props], ); const useNativePropsInFabric = ReactNativeFeatureFlags.shouldUseSetNativePropsInFabric(); diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index d1dede2de91..de62a3b54bd 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -730,6 +730,13 @@ declare export default function createAnimatedComponent< >( Component: React.AbstractComponent ): AnimatedComponentType; +declare export function unstable_createAnimatedComponentWithAllowlist< + TProps: { ... }, + TInstance, +>( + Component: React.AbstractComponent, + allowlist: ?AnimatedPropsAllowlist +): AnimatedComponentType; " `; @@ -945,8 +952,16 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A `; exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedProps.js 1`] = ` -"declare export default class AnimatedProps extends AnimatedNode { - constructor(inputProps: { [string]: mixed }, callback: () => void): void; +"export type AnimatedPropsAllowlist = $ReadOnly<{ + style?: ?AnimatedStyleAllowlist, + [string]: true, +}>; +declare export default class AnimatedProps extends AnimatedNode { + constructor( + inputProps: { [string]: mixed }, + callback: () => void, + allowlist?: ?AnimatedPropsAllowlist + ): void; __getValue(): Object; __getAnimatedValue(): Object; __attach(): void; @@ -963,8 +978,12 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A `; exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedStyle.js 1`] = ` -"declare export default class AnimatedStyle extends AnimatedWithChildren { - static from(inputStyle: any): ?AnimatedStyle; +"export type AnimatedStyleAllowlist = $ReadOnly<{ [string]: true }>; +declare export default class AnimatedStyle extends AnimatedWithChildren { + static from( + inputStyle: any, + allowlist: ?AnimatedStyleAllowlist + ): ?AnimatedStyle; constructor( nodeKeys: $ReadOnlyArray, nodes: $ReadOnlyArray, @@ -1159,7 +1178,8 @@ exports[`public API should not change unintentionally Libraries/Animated/useAnim }; type CallbackRef = (T) => mixed; declare export default function useAnimatedProps( - props: TProps + props: TProps, + allowlist?: ?AnimatedPropsAllowlist ): [ReducedProps, CallbackRef]; " `;