|
| 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 | + * @fantom_flags useSharedAnimatedBackend:* |
| 8 | + * @flow strict-local |
| 9 | + * @format |
| 10 | + */ |
| 11 | + |
| 12 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 13 | + |
| 14 | +import * as Fantom from '@react-native/fantom'; |
| 15 | +import * as React from 'react'; |
| 16 | +import {useEffect} from 'react'; |
| 17 | +import {Animated, View, useAnimatedValue} from 'react-native'; |
| 18 | +import {allowStyleProp} from 'react-native/Libraries/Animated/NativeAnimatedAllowlist'; |
| 19 | + |
| 20 | +allowStyleProp('height'); |
| 21 | + |
| 22 | +function MyApp() { |
| 23 | + return ( |
| 24 | + <View |
| 25 | + style={[ |
| 26 | + { |
| 27 | + width: 100, |
| 28 | + height: 100, |
| 29 | + opacity: 1, |
| 30 | + }, |
| 31 | + ]} |
| 32 | + /> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function MyAnimatedApp() { |
| 37 | + return ( |
| 38 | + <Animated.View |
| 39 | + style={[ |
| 40 | + { |
| 41 | + width: 100, |
| 42 | + height: 100, |
| 43 | + opacity: 1, |
| 44 | + }, |
| 45 | + ]} |
| 46 | + /> |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +function MyAnimatedAppWithOpacityAnimation() { |
| 51 | + const opacity = useAnimatedValue(1); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + Animated.timing(opacity, { |
| 55 | + toValue: 0, |
| 56 | + duration: 100, |
| 57 | + useNativeDriver: true, |
| 58 | + }).start(); |
| 59 | + }, [opacity]); |
| 60 | + |
| 61 | + return ( |
| 62 | + <Animated.View |
| 63 | + style={[ |
| 64 | + { |
| 65 | + width: 100, |
| 66 | + height: 100, |
| 67 | + opacity, |
| 68 | + }, |
| 69 | + ]} |
| 70 | + /> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +function MyAnimatedAppWithHeightAnimation() { |
| 75 | + const height = useAnimatedValue(0); |
| 76 | + |
| 77 | + useEffect(() => { |
| 78 | + Animated.timing(height, { |
| 79 | + toValue: 100, |
| 80 | + duration: 100, |
| 81 | + useNativeDriver: true, |
| 82 | + }).start(); |
| 83 | + }, [height]); |
| 84 | + |
| 85 | + return ( |
| 86 | + <Animated.View |
| 87 | + style={[ |
| 88 | + { |
| 89 | + width: 100, |
| 90 | + height, |
| 91 | + }, |
| 92 | + ]} |
| 93 | + /> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +function MyAnimatedAppWithMultipleAnimations() { |
| 98 | + const opacity = useAnimatedValue(1); |
| 99 | + const height = useAnimatedValue(0); |
| 100 | + |
| 101 | + useEffect(() => { |
| 102 | + Animated.parallel([ |
| 103 | + Animated.timing(opacity, { |
| 104 | + toValue: 0.5, |
| 105 | + duration: 100, |
| 106 | + useNativeDriver: true, |
| 107 | + }), |
| 108 | + Animated.timing(height, { |
| 109 | + toValue: 100, |
| 110 | + duration: 100, |
| 111 | + useNativeDriver: true, |
| 112 | + }), |
| 113 | + ]).start(); |
| 114 | + }, [opacity, height]); |
| 115 | + |
| 116 | + return ( |
| 117 | + <Animated.View |
| 118 | + style={[ |
| 119 | + { |
| 120 | + width: 100, |
| 121 | + height, |
| 122 | + opacity, |
| 123 | + }, |
| 124 | + ]} |
| 125 | + /> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +const ARGS = [1, 10, 100]; |
| 130 | + |
| 131 | +let root = Fantom.createRoot(); |
| 132 | +let element: React.MixedElement; |
| 133 | + |
| 134 | +function renderElement() { |
| 135 | + Fantom.runTask(() => root.render(element)); |
| 136 | +} |
| 137 | + |
| 138 | +function renderAndAnimateElement() { |
| 139 | + Fantom.runTask(() => root.render(element)); |
| 140 | + Fantom.unstable_produceFramesForDuration(100); |
| 141 | +} |
| 142 | + |
| 143 | +function getOptions( |
| 144 | + numberOfComponents: number, |
| 145 | + Component: React.ComponentType<{}>, |
| 146 | +): Fantom.BenchmarkTestOptions { |
| 147 | + return { |
| 148 | + beforeAll: () => { |
| 149 | + element = ( |
| 150 | + <> |
| 151 | + {Array.from({length: numberOfComponents}, (_, i) => ( |
| 152 | + <Component key={i} /> |
| 153 | + ))} |
| 154 | + </> |
| 155 | + ); |
| 156 | + }, |
| 157 | + beforeEach: () => { |
| 158 | + Fantom.runTask(() => root.render(<></>)); |
| 159 | + }, |
| 160 | + afterAll: () => { |
| 161 | + // flush pending tasks in message queue |
| 162 | + Fantom.runWorkLoop(); |
| 163 | + }, |
| 164 | + }; |
| 165 | +} |
| 166 | + |
| 167 | +Fantom.unstable_benchmark |
| 168 | + .suite('AnimatedBackend') |
| 169 | + .test.each( |
| 170 | + ARGS, |
| 171 | + n => `render ${n} views`, |
| 172 | + renderElement, |
| 173 | + n => getOptions(n, MyApp), |
| 174 | + ) |
| 175 | + .test.each( |
| 176 | + ARGS, |
| 177 | + n => `render ${n} animated views (without animations)`, |
| 178 | + renderElement, |
| 179 | + n => getOptions(n, MyAnimatedApp), |
| 180 | + ) |
| 181 | + .test.each( |
| 182 | + ARGS, |
| 183 | + n => `render ${n} animated views with opacity animation`, |
| 184 | + renderAndAnimateElement, |
| 185 | + n => getOptions(n, MyAnimatedAppWithOpacityAnimation), |
| 186 | + ) |
| 187 | + .test.each( |
| 188 | + ARGS, |
| 189 | + n => `render ${n} animated views with height animation (layout prop)`, |
| 190 | + renderAndAnimateElement, |
| 191 | + n => getOptions(n, MyAnimatedAppWithHeightAnimation), |
| 192 | + ) |
| 193 | + .test.each( |
| 194 | + ARGS, |
| 195 | + n => `render ${n} animated views with multiple animations`, |
| 196 | + renderAndAnimateElement, |
| 197 | + n => getOptions(n, MyAnimatedAppWithMultipleAnimations), |
| 198 | + ); |
0 commit comments