1 | import { SyntheticPlatformEmitter } from '@unimodules/core';
|
2 |
|
3 | import { isSensorEnabledAsync } from './utils/isSensorEnabledAsync.web';
|
4 |
|
5 | const eventName = 'devicemotion';
|
6 |
|
7 | export 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 |
|
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 | };
|