UNPKG

1.03 kBPlain TextView Raw
1import { SyntheticPlatformEmitter } from '@unimodules/core';
2
3import { isSensorEnabledAsync } from './utils/isSensorEnabledAsync.web';
4
5const eventName = 'devicemotion';
6
7export default {
8 get name(): string {
9 return 'ExponentDeviceMotion';
10 },
11 get Gravity(): number {
12 return 9.81;
13 },
14 async isAvailableAsync(): Promise<boolean> {
15 if (typeof DeviceMotionEvent === 'undefined') {
16 return false;
17 }
18 return await isSensorEnabledAsync(eventName);
19 },
20 _handleMotion(motion) {
21 // TODO: Bacon: Can rotation be calculated?
22 SyntheticPlatformEmitter.emit('deviceMotionDidUpdate', {
23 acceleration: motion.acceleration,
24 accelerationIncludingGravity: motion.accelerationIncludingGravity,
25 interval: motion.interval,
26 rotationRate: motion.rotationRate,
27 orientation: window.orientation,
28 });
29 },
30 startObserving() {
31 window.addEventListener(eventName, this._handleMotion);
32 },
33 stopObserving() {
34 window.removeEventListener(eventName, this._handleMotion);
35 },
36};