forked from vnil/react-native-simple-compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (25 loc) · 851 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { NativeModules, NativeEventEmitter, Dimensions } from "react-native";
const { RNSimpleCompass } = NativeModules;
let listener;
//Monkey patching
let _start = RNSimpleCompass.start;
RNSimpleCompass.start = (update_rate, callback) => {
if (listener) {
RNSimpleCompass.stop();
}
const compassEventEmitter = new NativeEventEmitter(RNSimpleCompass);
listener = compassEventEmitter.addListener("HeadingUpdated", course => {
const correctedCourse = { ...course };
const { height, width } = Dimensions.get("window");
if (width > height) correctedCourse.heading += 90;
callback(correctedCourse);
});
_start(update_rate === null ? 0 : update_rate);
};
let _stop = RNSimpleCompass.stop;
RNSimpleCompass.stop = () => {
listener && listener.remove();
listener = null;
_stop();
};
export default RNSimpleCompass;