Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async componentDidMount() {
if (!(await Accelerometer.isAvailableAsync())) {
this.setState({
error:
'Cannot start demo!' +
'\nEnable device orientation in Settings > Safari > Motion & Orientation Access' +
'\nalso ensure that you are hosting with https as DeviceMotion is now a secure API on iOS Safari.',
isSetup: false,
});
return;
}
Accelerometer.addListener(({ x, y }) => {
this.state.items.forEach((_, index) => {
// All that matters is that the values are the same on iOS, Android, Web, ect...
const { perspective } = this.props;
const nIndex = index + 1;
Animated.spring(this.state.items[index].position, {
toValue: {
x: (Number(x.toFixed(1)) * perspective * nIndex) / COUNT,
y: (-y.toFixed(1) * perspective * nIndex) / COUNT,
},
friction: 7,
}).start();
});
});
_subscribe = () => {
this._subscription = Accelerometer.addListener(accelerometerData => {
this.setState({ accelerometerData });
});
};
private subscribe() {
this.subscription = Accelerometer.addListener(accelerometerData => {
this.setState({ accelerometerData });
});
}
useEffect(() => {
if (availability) {
Accelerometer.isAvailableAsync().then(setAvailable);
}
if (options.interval !== undefined) {
Accelerometer.setUpdateInterval(options.interval);
}
return Accelerometer.addListener(setData).remove;
}, []);
Accelerometer.isAvailableAsync().then(motionAvailable => {
if (
!motionAvailable ||
!this.mounted ||
this.subscription ||
AppState.currentState !== "active"
)
return;
Accelerometer.setUpdateInterval(1000);
this.subscription = Accelerometer.addListener(acc => {
this.acceleration = acc;
});
});
}
function startAccelerometer (opts = {}) {
const {interval = 'normal', success, fail, complete} = opts
accCase.interval = interval
try {
accCase.listener = Accelerometer.addListener(accCase.callback)
success && success()
complete && complete()
} catch (error) {
fail && fail()
complete && complete()
const res = {errMsg: 'stopAccelerometer failed'}
return Promise.reject(res)
}
Accelerometer.setUpdateInterval(intervalMap[interval])
return Promise.resolve({errMsg: 'ok'})
}