-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventEmitter.ts
More file actions
68 lines (55 loc) · 1.97 KB
/
EventEmitter.ts
File metadata and controls
68 lines (55 loc) · 1.97 KB
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
import { type EventSubscription, Platform } from 'react-native';
import NativeOrientationDirector from './NativeOrientationDirector';
import type { OrientationEvent } from './types/OrientationEvent.interface';
import type { LockedEvent } from './types/LockedEvent.interface';
class EventEmitter {
private static androidListenerCount = 0;
static addDeviceOrientationDidChangeListener(
callback: (orientation: OrientationEvent) => void
) {
let listener =
NativeOrientationDirector.onDeviceOrientationChanged(callback);
if (Platform.OS !== 'android') {
return listener;
}
EventEmitter.androidListenerCount++;
if (EventEmitter.androidListenerCount === 1) {
NativeOrientationDirector.enableOrientationSensors();
}
return EventEmitter.createDeviceOrientationListenerProxy(listener);
}
static addInterfaceOrientationDidChangeListener(
callback: (orientation: OrientationEvent) => void
) {
return NativeOrientationDirector.onInterfaceOrientationChanged(callback);
}
static addLockDidChangeListener(callback: (event: LockedEvent) => void) {
return NativeOrientationDirector.onLockChanged(callback);
}
private static createDeviceOrientationListenerProxy(
listener: EventSubscription
) {
const handler: ProxyHandler<EventSubscription> = {
get(target, propertyKey, receiver) {
if (propertyKey === 'remove') {
disableOrientationSensorsIfLastListener();
}
return Reflect.get(target, propertyKey, receiver);
},
};
return new Proxy(listener, handler);
function disableOrientationSensorsIfLastListener() {
if (EventEmitter.androidListenerCount === 1) {
EventEmitter.androidListenerCount = 0;
NativeOrientationDirector.disableOrientationSensors();
return;
}
if (EventEmitter.androidListenerCount === 0) {
return;
}
EventEmitter.androidListenerCount--;
return;
}
}
}
export default EventEmitter;