-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add one listener per sensor config (#4340)
<!-- 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', }, }); ```
- Loading branch information
1 parent
f9ef042
commit eafb345
Showing
7 changed files
with
232 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import NativeReanimatedModule from './NativeReanimated'; | ||
import { | ||
SensorType, | ||
SensorConfig, | ||
SharedValue, | ||
Value3D, | ||
ValueRotation, | ||
ShareableRef, | ||
} from './commonTypes'; | ||
import { makeMutable } from './mutables'; | ||
|
||
function initSensorData( | ||
sensorType: SensorType | ||
): SharedValue<Value3D | ValueRotation> { | ||
if (sensorType === SensorType.ROTATION) { | ||
return makeMutable<Value3D | ValueRotation>({ | ||
qw: 0, | ||
qx: 0, | ||
qy: 0, | ||
qz: 0, | ||
yaw: 0, | ||
pitch: 0, | ||
roll: 0, | ||
interfaceOrientation: 0, | ||
}); | ||
} else { | ||
return makeMutable<Value3D | ValueRotation>({ | ||
x: 0, | ||
y: 0, | ||
z: 0, | ||
interfaceOrientation: 0, | ||
}); | ||
} | ||
} | ||
|
||
export default class Sensor<T> { | ||
public listenersNumber = 0; | ||
private sensorId: number | null = null; | ||
private sensorType: SensorType; | ||
private data: SharedValue<Value3D | ValueRotation>; | ||
private config: SensorConfig; | ||
|
||
constructor(sensorType: SensorType, config: SensorConfig) { | ||
this.sensorType = sensorType; | ||
this.config = config; | ||
this.data = initSensorData(sensorType); | ||
} | ||
|
||
register( | ||
eventHandler: ShareableRef<T> | ((data: Value3D | ValueRotation) => void) | ||
) { | ||
const config = this.config; | ||
const sensorType = this.sensorType; | ||
this.sensorId = NativeReanimatedModule.registerSensor( | ||
sensorType, | ||
config.interval === 'auto' ? -1 : config.interval, | ||
config.iosReferenceFrame, | ||
eventHandler | ||
); | ||
return this.sensorId !== -1; | ||
} | ||
|
||
isRunning() { | ||
return this.sensorId !== -1 && this.sensorId !== null; | ||
} | ||
|
||
isAvailable() { | ||
return this.sensorId !== -1; | ||
} | ||
|
||
getSharedValue() { | ||
return this.data; | ||
} | ||
|
||
unregister() { | ||
if (this.sensorId !== null && this.sensorId !== -1) { | ||
NativeReanimatedModule.unregisterSensor(this.sensorId); | ||
} | ||
this.sensorId = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
SensorType, | ||
SensorConfig, | ||
Value3D, | ||
ValueRotation, | ||
ShareableRef, | ||
SharedValue, | ||
} from './commonTypes'; | ||
import Sensor from './Sensor'; | ||
|
||
export class SensorContainer { | ||
private nativeSensors: Map<number, Sensor<any>> = new Map(); | ||
|
||
getSensorId(sensorType: SensorType, config: SensorConfig) { | ||
return ( | ||
sensorType * 100 + | ||
config.iosReferenceFrame * 10 + | ||
Number(config.adjustToInterfaceOrientation) | ||
); | ||
} | ||
|
||
initializeSensor( | ||
sensorType: SensorType, | ||
config: SensorConfig | ||
): SharedValue<Value3D | ValueRotation> { | ||
const sensorId = this.getSensorId(sensorType, config); | ||
|
||
if (!this.nativeSensors.has(sensorId)) { | ||
const newSensor = new Sensor(sensorType, config); | ||
this.nativeSensors.set(sensorId, newSensor); | ||
} | ||
|
||
const sensor = this.nativeSensors.get(sensorId); | ||
return sensor!.getSharedValue(); | ||
} | ||
|
||
registerSensor<T>( | ||
sensorType: SensorType, | ||
config: SensorConfig, | ||
handler: ShareableRef<T> | ((data: Value3D | ValueRotation) => void) | ||
): number { | ||
const sensorId = this.getSensorId(sensorType, config); | ||
|
||
if (!this.nativeSensors.has(sensorId)) { | ||
return -1; | ||
} | ||
|
||
const sensor = this.nativeSensors.get(sensorId); | ||
if ( | ||
sensor && | ||
sensor.isAvailable() && | ||
(sensor.isRunning() || sensor.register(handler)) | ||
) { | ||
sensor.listenersNumber++; | ||
return sensorId; | ||
} | ||
return -1; | ||
} | ||
|
||
unregisterSensor(sensorId: number) { | ||
if (this.nativeSensors.has(sensorId)) { | ||
const sensor = this.nativeSensors.get(sensorId); | ||
if (sensor && sensor.isRunning()) { | ||
sensor.listenersNumber--; | ||
if (sensor.listenersNumber === 0) { | ||
sensor.unregister(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.