Skip to content

Commit 2e38ae3

Browse files
authored
fix(iOS): missing listeners enabled check (#108)
1 parent 8c800df commit 2e38ae3

File tree

5 files changed

+45
-57
lines changed

5 files changed

+45
-57
lines changed

ios/OrientationDirector.mm

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
#import "OrientationDirector/OrientationDirector-Swift.h"
1111
#endif
1212

13+
#include <exception>
14+
1315
static OrientationDirectorImpl *_director = SharedOrientationDirectorImpl.shared;
1416

1517
///////////////////////////////////////////////////////////////////////////////////////
1618
/// EVENT EMITTER SETUP
17-
///https://github.com/react-native-community/RNNewArchitectureLibraries/tree/feat/swift-event-emitter
1819
@interface OrientationDirector() <OrientationEventEmitterDelegate>
1920
@end
2021
///
@@ -43,17 +44,31 @@ + (BOOL)requiresMainQueueSetup
4344
///////////////////////////////////////////////////////////////////////////////////////
4445
/// EVENT EMITTER SETUP
4546
///
46-
-(void)emitOnDeviceOrientationDidChangeWithParams:(NSDictionary*)params {
47-
[self emitOnDeviceOrientationChanged:params];
47+
48+
-(void)emitDeviceOrientationChangedWithParams:(NSDictionary*)params {
49+
try {
50+
[self emitOnDeviceOrientationChanged:params];
51+
} catch (std::exception &e) {
52+
// Ignore if no listeners
53+
}
4854
}
4955

50-
-(void)emitOnInterfaceOrientationDidChangeWithParams:(NSDictionary*)params {
51-
[self emitOnInterfaceOrientationChanged:params];
56+
-(void)emitInterfaceOrientationChangedWithParams:(NSDictionary*)params {
57+
try {
58+
[self emitOnInterfaceOrientationChanged:params];
59+
} catch (std::exception &e) {
60+
// Ignore if no listeners
61+
}
5262
}
5363

5464
-(void)emitOnLockChangedWithParams:(NSDictionary*)params {
55-
[self emitOnLockChanged:params];
65+
try {
66+
[self emitOnLockChanged:params];
67+
} catch (std::exception &e) {
68+
// Ignore if no listeners
69+
}
5670
}
71+
5772
///
5873
///////////////////////////////////////////////////////////////////////////////////////
5974

ios/implementation/EventManager.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class EventManager: NSObject {
1414
guard let delegate = delegate else {
1515
return
1616
}
17-
17+
1818
let params = Dictionary(dictionaryLiteral: ("orientation", value))
19-
delegate.emitOnDeviceOrientationDidChange(params: params as NSDictionary)
19+
delegate.emitDeviceOrientationChanged(params: params as NSDictionary)
2020
}
2121

2222
func sendInterfaceOrientationDidChange(value: Int) {
@@ -25,7 +25,7 @@ public class EventManager: NSObject {
2525
}
2626

2727
let params = Dictionary(dictionaryLiteral: ("orientation", value))
28-
delegate.emitOnInterfaceOrientationDidChange(params: params as NSDictionary)
28+
delegate.emitInterfaceOrientationChanged(params: params as NSDictionary)
2929
}
3030

3131
func sendLockDidChange(value: Bool) {
@@ -40,6 +40,6 @@ public class EventManager: NSObject {
4040

4141
@objc public protocol OrientationEventEmitterDelegate {
4242
func emitOnLockChanged(params: NSDictionary)
43-
func emitOnDeviceOrientationDidChange(params: NSDictionary)
44-
func emitOnInterfaceOrientationDidChange(params: NSDictionary)
43+
func emitDeviceOrientationChanged(params: NSDictionary)
44+
func emitInterfaceOrientationChanged(params: NSDictionary)
4545
}

src/EventEmitter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Platform, type EventSubscription } from 'react-native';
2-
import Module from './module';
1+
import { type EventSubscription, Platform } from 'react-native';
2+
import NativeOrientationDirector from './NativeOrientationDirector';
33
import type { OrientationEvent } from './types/OrientationEvent.interface';
44
import type { LockedEvent } from './types/LockedEvent.interface';
55

@@ -9,15 +9,16 @@ class EventEmitter {
99
static addDeviceOrientationDidChangeListener(
1010
callback: (orientation: OrientationEvent) => void
1111
) {
12-
let listener = Module.onDeviceOrientationChanged(callback);
12+
let listener =
13+
NativeOrientationDirector.onDeviceOrientationChanged(callback);
1314

1415
if (Platform.OS !== 'android') {
1516
return listener;
1617
}
1718

1819
EventEmitter.androidListenerCount++;
1920
if (EventEmitter.androidListenerCount === 1) {
20-
Module.enableOrientationSensors();
21+
NativeOrientationDirector.enableOrientationSensors();
2122
}
2223

2324
return EventEmitter.createDeviceOrientationListenerProxy(listener);
@@ -26,11 +27,11 @@ class EventEmitter {
2627
static addInterfaceOrientationDidChangeListener(
2728
callback: (orientation: OrientationEvent) => void
2829
) {
29-
return Module.onInterfaceOrientationChanged(callback);
30+
return NativeOrientationDirector.onInterfaceOrientationChanged(callback);
3031
}
3132

3233
static addLockDidChangeListener(callback: (event: LockedEvent) => void) {
33-
return Module.onLockChanged(callback);
34+
return NativeOrientationDirector.onLockChanged(callback);
3435
}
3536

3637
private static createDeviceOrientationListenerProxy(
@@ -50,7 +51,7 @@ class EventEmitter {
5051
function disableOrientationSensorsIfLastListener() {
5152
if (EventEmitter.androidListenerCount === 1) {
5253
EventEmitter.androidListenerCount = 0;
53-
Module.disableOrientationSensors();
54+
NativeOrientationDirector.disableOrientationSensors();
5455
return;
5556
}
5657

src/RNOrientationDirector.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Platform } from 'react-native';
2-
import Module from './module';
2+
import NativeOrientationDirector from './NativeOrientationDirector';
33
import type { HumanReadableOrientationsResource } from './types/HumanReadableOrientationsResource.type';
44
import { Orientation } from './types/Orientation.enum';
55
import { AutoRotation } from './types/AutoRotation.enum';
@@ -39,11 +39,11 @@ class RNOrientationDirector {
3939
}
4040

4141
static getInterfaceOrientation(): Promise<Orientation> {
42-
return Module.getInterfaceOrientation();
42+
return NativeOrientationDirector.getInterfaceOrientation();
4343
}
4444

4545
static getDeviceOrientation(): Promise<Orientation> {
46-
return Module.getDeviceOrientation();
46+
return NativeOrientationDirector.getDeviceOrientation();
4747
}
4848

4949
/**
@@ -69,42 +69,42 @@ class RNOrientationDirector {
6969
orientationType: OrientationType = OrientationType.interface
7070
) {
7171
if (orientationType === OrientationType.interface) {
72-
Module.lockTo(orientation);
72+
NativeOrientationDirector.lockTo(orientation);
7373
return;
7474
}
7575

7676
if (orientation === Orientation.landscapeLeft) {
77-
Module.lockTo(Orientation.landscapeRight);
77+
NativeOrientationDirector.lockTo(Orientation.landscapeRight);
7878
return;
7979
}
8080

8181
if (orientation === Orientation.landscapeRight) {
82-
Module.lockTo(Orientation.landscapeLeft);
82+
NativeOrientationDirector.lockTo(Orientation.landscapeLeft);
8383
return;
8484
}
8585

86-
Module.lockTo(orientation);
86+
NativeOrientationDirector.lockTo(orientation);
8787
}
8888

8989
static unlock() {
90-
Module.unlock();
90+
NativeOrientationDirector.unlock();
9191
}
9292

9393
static isLocked() {
94-
return Module.isLocked();
94+
return NativeOrientationDirector.isLocked();
9595
}
9696

9797
static isAutoRotationEnabled() {
9898
if (Platform.OS !== 'android') {
9999
return AutoRotation.unknown;
100100
}
101-
return Module.isAutoRotationEnabled()
101+
return NativeOrientationDirector.isAutoRotationEnabled()
102102
? AutoRotation.enabled
103103
: AutoRotation.disabled;
104104
}
105105

106106
static resetSupportedInterfaceOrientations() {
107-
Module.resetSupportedInterfaceOrientations();
107+
NativeOrientationDirector.resetSupportedInterfaceOrientations();
108108
}
109109

110110
static listenForDeviceOrientationChanges(

src/module.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)