Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add one listener per sensor config #4340

Merged
merged 15 commits into from
Apr 24, 2023

Conversation

mstach60161
Copy link
Contributor

@mstach60161 mstach60161 commented Apr 6, 2023

Summary

This PR is to optimise using sensors.

Before:

Each useAnimatedSensor call creates a new native listener that update shared value assigned to it.

Now:

We identify sensors listeners by sensor type and config properties and the same configurations use the same shared value.

Test plan

  • Example app
import Animated, {
  useAnimatedStyle,
  useAnimatedSensor,
  SensorType,
} from 'react-native-reanimated';
import { StyleSheet, View } from 'react-native';

import React from 'react';

export default function AnimatedSensorExample() {

  const sensorObject = (color: string, sensorType: any) => {
    const gravity = useAnimatedSensor(sensorType, { interval: 16 });
    console.log(gravity.sensor.value);

    const animatedStyle = useAnimatedStyle(() => {
      //console.log(gravity.sensor.value);
      return {
        top: -gravity.sensor.value.y * 300,
        left: gravity.sensor.value.x * 200,
      };
    });


    return (
      <View style={styles.container}>
        <Animated.View style={[{...styles.box, backgroundColor: color}, animatedStyle]} />
      </View>
    )
  };

  return (
    <View style={styles.container}>
      {sensorObject('red', SensorType.GRAVITY)}
      {sensorObject('blue', SensorType.ACCELEROMETER)}
      {sensorObject('green', SensorType.GRAVITY)}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 150,
    height: 150,
    backgroundColor: 'navy',
  },
});

@mstach60161 mstach60161 marked this pull request as ready for review April 7, 2023 13:42
Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine, but we need just polish some details. I want to ask you about two another things:

  • Add the example to the PR description
  • Resolve conflict with the main branch

src/reanimated2/globals.d.ts Outdated Show resolved Hide resolved
src/reanimated2/globals.d.ts Outdated Show resolved Hide resolved
src/reanimated2/core.ts Outdated Show resolved Hide resolved
src/reanimated2/hook/useAnimatedSensor.ts Outdated Show resolved Hide resolved
src/reanimated2/SensorContainer.ts Outdated Show resolved Hide resolved
src/reanimated2/SensorContainer.ts Outdated Show resolved Hide resolved
src/reanimated2/SensorContainer.ts Outdated Show resolved Hide resolved
src/reanimated2/SensorContainer.ts Outdated Show resolved Hide resolved
src/reanimated2/Sensor.ts Outdated Show resolved Hide resolved
@mstach60161 mstach60161 added this pull request to the merge queue Apr 24, 2023
Merged via the queue into main with commit eafb345 Apr 24, 2023
1 check passed
@mstach60161 mstach60161 deleted the @mstach60161/one-listener-per-sensor branch April 24, 2023 11:52
fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

This PR is to optimise using sensors.

Before:

Each `useAnimatedSensor` call creates a new native listener that update
shared value assigned to it.

Now:

We identify sensors listeners by sensor type and config properties and
the same configurations use the same shared value.
 

## Test plan

- Example app
```
import Animated, {
  useAnimatedStyle,
  useAnimatedSensor,
  SensorType,
} from 'react-native-reanimated';
import { StyleSheet, View } from 'react-native';

import React from 'react';

export default function AnimatedSensorExample() {

  const sensorObject = (color: string, sensorType: any) => {
    const gravity = useAnimatedSensor(sensorType, { interval: 16 });
    console.log(gravity.sensor.value);

    const animatedStyle = useAnimatedStyle(() => {
      //console.log(gravity.sensor.value);
      return {
        top: -gravity.sensor.value.y * 300,
        left: gravity.sensor.value.x * 200,
      };
    });


    return (
      <View style={styles.container}>
        <Animated.View style={[{...styles.box, backgroundColor: color}, animatedStyle]} />
      </View>
    )
  };

  return (
    <View style={styles.container}>
      {sensorObject('red', SensorType.GRAVITY)}
      {sensorObject('blue', SensorType.ACCELEROMETER)}
      {sensorObject('green', SensorType.GRAVITY)}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 150,
    height: 150,
    backgroundColor: 'navy',
  },
});

```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants