-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (77 loc) · 2.79 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {NativeModules, DeviceEventEmitter} from 'react-native';
const {OrientationHandler} = NativeModules;
const orientationDidChangeEvent = 'orientationDidChange';
const specificOrientationDidChangeEvent = 'specificOrientationDidChange';
const META = '__listener_id';
let listeners = {};
let id = 0;
if (!OrientationHandler) {
throw new Error(`[OrientationHandler]: NativeModule: OrientationHandler is null.
To fix this issue try these steps:
• Run \`react-native link react-native-orientation-handler\` in the project root.
• Rebuild and restart the app.
• Run the packager with \`--clearCache\` flag.
• If you are using CocoaPods on iOS, run \`pod install\` in the \`ios\` directory and then rebuild and re-run the app.
If none of these fix the issue, please open an issue on the Github repository: https://github.com/YatoStudio/react-native-orientation-handler/issues
`);
}
const getKey = listener => {
if (!listener.hasOwnProperty(META)) {
if (!Object.isExtensible(listener)) {
return 'F';
}
Object.defineProperty(listener, META, {
value: 'L' + ++id,
});
}
return listener[META];
};
const Orientation = {
lockToPortrait: OrientationHandler.lockToPortrait,
lockToLandscape: OrientationHandler.lockToLandscape,
lockToLandscapeRight: OrientationHandler.lockToLandscapeRight,
lockToLandscapeLeft: OrientationHandler.lockToLandscapeLeft,
unlockAllOrientations: OrientationHandler.unlockAllOrientations,
getInitialOrientation: OrientationHandler.initialOrientation,
getOrientation: cb => {
OrientationHandler.getOrientation((error, orientation) => {
cb(error, orientation);
});
},
getSpecificOrientation: cb => {
OrientationHandler.getSpecificOrientation((error, orientation) => {
cb(error, orientation);
});
},
addOrientationListener: cb => {
const key = getKey(cb);
listeners[key] = DeviceEventEmitter.addListener(orientationDidChangeEvent,
(body) => {
cb(body.orientation);
});
},
removeOrientationListener: cb => {
const key = getKey(cb);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
},
addSpecificOrientationListener: cb => {
const key = getKey(cb);
listeners[key] = DeviceEventEmitter.addListener(specificOrientationDidChangeEvent,
(body) => {
cb(body.specificOrientation);
});
},
removeSpecificOrientationListener: cb => {
const key = getKey(cb);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
}
};
export default Orientation;