1import { SyntheticPlatformEmitter } from 'expo-modules-core';
2
3import {
4  assertSensorEventEnabledAsync,
5  getPermissionsAsync,
6  isSensorEnabledAsync,
7  requestPermissionsAsync,
8} from './utils/isSensorEnabledAsync.web';
9
10const eventName = 'devicemotion';
11
12export default {
13  get name(): string {
14    return 'ExponentDeviceMotion';
15  },
16  /**
17   * Gravity on the planet this module supports (currently just Earth) represented as m/s^2.
18   */
19  get Gravity(): number {
20    return 9.80665;
21  },
22  async isAvailableAsync(): Promise<boolean> {
23    if (typeof DeviceMotionEvent === 'undefined') {
24      return false;
25    }
26    return await isSensorEnabledAsync(eventName);
27  },
28  _handleMotion(motion) {
29    // TODO: Bacon: Can rotation be calculated?
30    SyntheticPlatformEmitter.emit('deviceMotionDidUpdate', {
31      acceleration: motion.acceleration,
32      accelerationIncludingGravity: motion.accelerationIncludingGravity,
33      interval: motion.interval,
34      rotationRate: motion.rotationRate,
35      orientation: window.orientation,
36    });
37  },
38  getPermissionsAsync,
39  requestPermissionsAsync,
40  startObserving() {
41    assertSensorEventEnabledAsync(eventName);
42
43    window.addEventListener(eventName, this._handleMotion);
44  },
45  stopObserving() {
46    window.removeEventListener(eventName, this._handleMotion);
47  },
48};
49