Commit ae41fec
[General] Listen for Reanimated events directly, without
## Description
Shouldn't need
software-mansion/react-native-reanimated#8655 to
work properly
Instead of using `createAnimatedComponent`, which does a lot more stuff
other than setting up events, we can set up events ourselves using
Reanimated's `NativeEventsManager` directly.
The implementation in the PR should be independent of the Reanimated
version being used, as long as it's a relatively modern one.
## Test plan
<details>
<summary>Tested on this stress-test</summary>
```jsx
import React, { Profiler, useEffect, useRef } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { GestureDetector, usePanGesture } from 'react-native-gesture-handler';
// import { PerfMonitor } from 'react-native-gesture-handler/src/v3/PerfMonitor';
import Animated, {
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated';
const DATA = new Array(500).fill(null).map((_, i) => `Item ${i + 1}`);
function Item() {
const translateX = useSharedValue(0);
const style = useAnimatedStyle(() => {
return {
transform: [{ translateX: translateX.value }],
};
});
const pan = usePanGesture({
// disableReanimated: true,
onUpdate: (event) => {
'worklet';
console.log('pan onUpdate', event.handlerData.changeX);
},
onStart: () => {
'worklet';
console.log('pan onStart', _WORKLET);
},
onEnd: () => {
'worklet';
console.log('pan onEnd');
},
onBegin: () => {
'worklet';
console.log('pan onBegin');
},
onFinalize: () => {
'worklet';
console.log('pan onFinalize');
},
onTouchesDown: () => {
'worklet';
console.log('pan onTouchesDown');
},
});
return (
<View style={{ height: 80, padding: 16, backgroundColor: 'gray' }}>
<GestureDetector gesture={pan}>
{/* <View collapsable={false} style={{opacity: 0.5}}> */}
<Animated.View
style={[
{ backgroundColor: 'red', height: '100%', aspectRatio: 1 },
style,
]}
/>
{/* </View> */}
</GestureDetector>
</View>
);
}
function Benchmark() {
return (
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={{ flexGrow: 1, gap: 8 }}>
{DATA.map((_, index) => (
<Item key={index} />
))}
</ScrollView>
);
}
const TIMES = 35;
export default function EmptyExample() {
const times = useRef<number[]>([]).current;
const [visible, setVisible] = React.useState(false);
useEffect(() => {
if (!visible && times.length < TIMES) {
setTimeout(() => {
setVisible(true);
}, 24);
}
if (times.length === TIMES) {
// calculate average, but remove highest and lowest
const sortedTimes = [...times].sort((a, b) => a - b);
sortedTimes.shift();
sortedTimes.shift();
sortedTimes.pop();
sortedTimes.pop();
const avgTime =
sortedTimes.reduce((sum, time) => sum + time, 0) / sortedTimes.length;
console.log(`Average render time: ${avgTime} ms`);
// console.log(JSON.stringify(PerfMonitor.getMeasures(), null, 2));
// PerfMonitor.clear();
}
}, [visible]);
return (
<View style={styles.container}>
{visible && (
<Profiler
id="v3"
onRender={(_id, _phase, actualDuration) => {
times.push(actualDuration);
setTimeout(() => {
setVisible(false);
}, 24);
}}>
<Benchmark />
</Profiler>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
```
</details>
Before: 614ms, after: 576ms
---------
Co-authored-by: Michał Bert <63123542+m-bert@users.noreply.github.com>createAnimatedComponent when possible (#3837)1 parent 6a6dc33 commit ae41fec
File tree
7 files changed
+117
-6
lines changed- packages/react-native-gesture-handler/src
- handlers/gestures
- v3/detectors
- VirtualDetector
7 files changed
+117
-6
lines changedLines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
32 | 45 | | |
33 | 46 | | |
34 | 47 | | |
| |||
38 | 51 | | |
39 | 52 | | |
40 | 53 | | |
| 54 | + | |
41 | 55 | | |
42 | 56 | | |
43 | 57 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
Lines changed: 95 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
Lines changed: 0 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
| |||
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | 25 | | |
30 | 26 | | |
31 | 27 | | |
| |||
0 commit comments