From 72c5092e1ea4195e363ae1a08c94b539f5544cd2 Mon Sep 17 00:00:00 2001 From: gladiuscode Date: Sun, 29 Mar 2026 11:57:15 +0200 Subject: [PATCH 1/2] fix(iOS): missing listeners enabled check --- ios/OrientationDirector.mm | 43 ++++++++++++++++++++------- ios/implementation/EventManager.swift | 22 +++++++++++--- src/EventEmitter.ts | 33 ++++++++++---------- src/RNOrientationDirector.ts | 22 +++++++------- src/module.ts | 28 ----------------- 5 files changed, 76 insertions(+), 72 deletions(-) delete mode 100644 src/module.ts diff --git a/ios/OrientationDirector.mm b/ios/OrientationDirector.mm index 8dd30db..74a24df 100644 --- a/ios/OrientationDirector.mm +++ b/ios/OrientationDirector.mm @@ -10,11 +10,12 @@ #import "OrientationDirector/OrientationDirector-Swift.h" #endif +#include + static OrientationDirectorImpl *_director = SharedOrientationDirectorImpl.shared; /////////////////////////////////////////////////////////////////////////////////////// /// EVENT EMITTER SETUP -///https://github.com/react-native-community/RNNewArchitectureLibraries/tree/feat/swift-event-emitter @interface OrientationDirector() @end /// @@ -43,17 +44,33 @@ + (BOOL)requiresMainQueueSetup /////////////////////////////////////////////////////////////////////////////////////// /// EVENT EMITTER SETUP /// --(void)emitOnDeviceOrientationDidChangeWithParams:(NSDictionary*)params { - [self emitOnDeviceOrientationChanged:params]; + +@synthesize enabled; + +-(void)emitDeviceOrientationChangedWithParams:(NSDictionary*)params { + try { + [self emitOnDeviceOrientationChanged:params]; + } catch (std::exception &e) { + // Ignore if no listeners + } } --(void)emitOnInterfaceOrientationDidChangeWithParams:(NSDictionary*)params { - [self emitOnInterfaceOrientationChanged:params]; +-(void)emitInterfaceOrientationChangedWithParams:(NSDictionary*)params { + try { + [self emitOnInterfaceOrientationChanged:params]; + } catch (std::exception &e) { + // Ignore if no listeners + } } -(void)emitOnLockChangedWithParams:(NSDictionary*)params { - [self emitOnLockChanged:params]; + try { + [self emitOnLockChanged:params]; + } catch (std::exception &e) { + // Ignore if no listeners + } } + /// /////////////////////////////////////////////////////////////////////////////////////// @@ -104,6 +121,15 @@ - (void)resetSupportedInterfaceOrientations }); } +- (void)disableOrientationSensors { + [self setEnabled:false]; +} + + +- (void)enableOrientationSensors { + [self setEnabled:true]; +} + /////////////////////////////////////////////////////////////////////////////////////// /// STUBS /// @@ -113,11 +139,6 @@ - (NSNumber *)isAutoRotationEnabled return @(NO); } -- (void)disableOrientationSensors {} - - -- (void)enableOrientationSensors {} - /// /////////////////////////////////////////////////////////////////////////////////////// diff --git a/ios/implementation/EventManager.swift b/ios/implementation/EventManager.swift index 0d32361..cc91731 100644 --- a/ios/implementation/EventManager.swift +++ b/ios/implementation/EventManager.swift @@ -15,23 +15,35 @@ public class EventManager: NSObject { return } + if delegate.enabled == false { + return + } + let params = Dictionary(dictionaryLiteral: ("orientation", value)) - delegate.emitOnDeviceOrientationDidChange(params: params as NSDictionary) + delegate.emitDeviceOrientationChanged(params: params as NSDictionary) } func sendInterfaceOrientationDidChange(value: Int) { guard let delegate = delegate else { return } + + if delegate.enabled == false { + return + } let params = Dictionary(dictionaryLiteral: ("orientation", value)) - delegate.emitOnInterfaceOrientationDidChange(params: params as NSDictionary) + delegate.emitInterfaceOrientationChanged(params: params as NSDictionary) } func sendLockDidChange(value: Bool) { guard let delegate = delegate else { return } + + if delegate.enabled == false { + return + } let params = Dictionary(dictionaryLiteral: ("locked", value)) delegate.emitOnLockChanged(params: params as NSDictionary) @@ -39,7 +51,9 @@ public class EventManager: NSObject { } @objc public protocol OrientationEventEmitterDelegate { + @objc var enabled: Bool { get set } + func emitOnLockChanged(params: NSDictionary) - func emitOnDeviceOrientationDidChange(params: NSDictionary) - func emitOnInterfaceOrientationDidChange(params: NSDictionary) + func emitDeviceOrientationChanged(params: NSDictionary) + func emitInterfaceOrientationChanged(params: NSDictionary) } diff --git a/src/EventEmitter.ts b/src/EventEmitter.ts index f20e05e..e7bbac9 100644 --- a/src/EventEmitter.ts +++ b/src/EventEmitter.ts @@ -1,23 +1,20 @@ -import { Platform, type EventSubscription } from 'react-native'; -import Module from './module'; +import { type EventSubscription } 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; + private static listenerCount = 0; static addDeviceOrientationDidChangeListener( callback: (orientation: OrientationEvent) => void ) { - let listener = Module.onDeviceOrientationChanged(callback); + let listener = + NativeOrientationDirector.onDeviceOrientationChanged(callback); - if (Platform.OS !== 'android') { - return listener; - } - - EventEmitter.androidListenerCount++; - if (EventEmitter.androidListenerCount === 1) { - Module.enableOrientationSensors(); + EventEmitter.listenerCount++; + if (EventEmitter.listenerCount === 1) { + NativeOrientationDirector.enableOrientationSensors(); } return EventEmitter.createDeviceOrientationListenerProxy(listener); @@ -26,11 +23,11 @@ class EventEmitter { static addInterfaceOrientationDidChangeListener( callback: (orientation: OrientationEvent) => void ) { - return Module.onInterfaceOrientationChanged(callback); + return NativeOrientationDirector.onInterfaceOrientationChanged(callback); } static addLockDidChangeListener(callback: (event: LockedEvent) => void) { - return Module.onLockChanged(callback); + return NativeOrientationDirector.onLockChanged(callback); } private static createDeviceOrientationListenerProxy( @@ -48,17 +45,17 @@ class EventEmitter { return new Proxy(listener, handler); function disableOrientationSensorsIfLastListener() { - if (EventEmitter.androidListenerCount === 1) { - EventEmitter.androidListenerCount = 0; - Module.disableOrientationSensors(); + if (EventEmitter.listenerCount === 1) { + EventEmitter.listenerCount = 0; + NativeOrientationDirector.disableOrientationSensors(); return; } - if (EventEmitter.androidListenerCount === 0) { + if (EventEmitter.listenerCount === 0) { return; } - EventEmitter.androidListenerCount--; + EventEmitter.listenerCount--; return; } } diff --git a/src/RNOrientationDirector.ts b/src/RNOrientationDirector.ts index ee0143c..26a8148 100644 --- a/src/RNOrientationDirector.ts +++ b/src/RNOrientationDirector.ts @@ -1,5 +1,5 @@ import { Platform } from 'react-native'; -import Module from './module'; +import NativeOrientationDirector from './NativeOrientationDirector'; import type { HumanReadableOrientationsResource } from './types/HumanReadableOrientationsResource.type'; import { Orientation } from './types/Orientation.enum'; import { AutoRotation } from './types/AutoRotation.enum'; @@ -39,11 +39,11 @@ class RNOrientationDirector { } static getInterfaceOrientation(): Promise { - return Module.getInterfaceOrientation(); + return NativeOrientationDirector.getInterfaceOrientation(); } static getDeviceOrientation(): Promise { - return Module.getDeviceOrientation(); + return NativeOrientationDirector.getDeviceOrientation(); } /** @@ -69,42 +69,42 @@ class RNOrientationDirector { orientationType: OrientationType = OrientationType.interface ) { if (orientationType === OrientationType.interface) { - Module.lockTo(orientation); + NativeOrientationDirector.lockTo(orientation); return; } if (orientation === Orientation.landscapeLeft) { - Module.lockTo(Orientation.landscapeRight); + NativeOrientationDirector.lockTo(Orientation.landscapeRight); return; } if (orientation === Orientation.landscapeRight) { - Module.lockTo(Orientation.landscapeLeft); + NativeOrientationDirector.lockTo(Orientation.landscapeLeft); return; } - Module.lockTo(orientation); + NativeOrientationDirector.lockTo(orientation); } static unlock() { - Module.unlock(); + NativeOrientationDirector.unlock(); } static isLocked() { - return Module.isLocked(); + return NativeOrientationDirector.isLocked(); } static isAutoRotationEnabled() { if (Platform.OS !== 'android') { return AutoRotation.unknown; } - return Module.isAutoRotationEnabled() + return NativeOrientationDirector.isAutoRotationEnabled() ? AutoRotation.enabled : AutoRotation.disabled; } static resetSupportedInterfaceOrientations() { - Module.resetSupportedInterfaceOrientations(); + NativeOrientationDirector.resetSupportedInterfaceOrientations(); } static listenForDeviceOrientationChanges( diff --git a/src/module.ts b/src/module.ts deleted file mode 100644 index 4bed768..0000000 --- a/src/module.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { NativeModules, Platform } from 'react-native'; -import type { Spec } from './NativeOrientationDirector'; - -const LINKING_ERROR = - `The package 'react-native-orientation-director' doesn't seem to be linked. Make sure: \n\n` + - Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + - '- You rebuilt the app after installing the package\n' + - '- You are not using Expo Go\n'; - -// @ts-expect-error -const isTurboModuleEnabled = global.__turboModuleProxy != null; - -const Module = isTurboModuleEnabled - ? require('./NativeOrientationDirector').default - : NativeModules.OrientationDirector; - -const OrientationDirectorModule = Module - ? Module - : new Proxy( - {}, - { - get() { - throw new Error(LINKING_ERROR); - }, - } - ); - -export default OrientationDirectorModule as Spec; From 0c47d0f10bfab6ceafa8fd35330f5de7e8e335c9 Mon Sep 17 00:00:00 2001 From: gladiuscode Date: Wed, 1 Apr 2026 21:49:02 +0200 Subject: [PATCH 2/2] revert(iOS): listeners enabled property setup --- ios/OrientationDirector.mm | 16 +++++----------- ios/implementation/EventManager.swift | 14 -------------- src/EventEmitter.ts | 20 ++++++++++++-------- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/ios/OrientationDirector.mm b/ios/OrientationDirector.mm index 74a24df..5d02375 100644 --- a/ios/OrientationDirector.mm +++ b/ios/OrientationDirector.mm @@ -45,8 +45,6 @@ + (BOOL)requiresMainQueueSetup /// EVENT EMITTER SETUP /// -@synthesize enabled; - -(void)emitDeviceOrientationChangedWithParams:(NSDictionary*)params { try { [self emitOnDeviceOrientationChanged:params]; @@ -121,15 +119,6 @@ - (void)resetSupportedInterfaceOrientations }); } -- (void)disableOrientationSensors { - [self setEnabled:false]; -} - - -- (void)enableOrientationSensors { - [self setEnabled:true]; -} - /////////////////////////////////////////////////////////////////////////////////////// /// STUBS /// @@ -139,6 +128,11 @@ - (NSNumber *)isAutoRotationEnabled return @(NO); } +- (void)disableOrientationSensors {} + + +- (void)enableOrientationSensors {} + /// /////////////////////////////////////////////////////////////////////////////////////// diff --git a/ios/implementation/EventManager.swift b/ios/implementation/EventManager.swift index cc91731..486712a 100644 --- a/ios/implementation/EventManager.swift +++ b/ios/implementation/EventManager.swift @@ -14,10 +14,6 @@ public class EventManager: NSObject { guard let delegate = delegate else { return } - - if delegate.enabled == false { - return - } let params = Dictionary(dictionaryLiteral: ("orientation", value)) delegate.emitDeviceOrientationChanged(params: params as NSDictionary) @@ -27,10 +23,6 @@ public class EventManager: NSObject { guard let delegate = delegate else { return } - - if delegate.enabled == false { - return - } let params = Dictionary(dictionaryLiteral: ("orientation", value)) delegate.emitInterfaceOrientationChanged(params: params as NSDictionary) @@ -40,10 +32,6 @@ public class EventManager: NSObject { guard let delegate = delegate else { return } - - if delegate.enabled == false { - return - } let params = Dictionary(dictionaryLiteral: ("locked", value)) delegate.emitOnLockChanged(params: params as NSDictionary) @@ -51,8 +39,6 @@ public class EventManager: NSObject { } @objc public protocol OrientationEventEmitterDelegate { - @objc var enabled: Bool { get set } - func emitOnLockChanged(params: NSDictionary) func emitDeviceOrientationChanged(params: NSDictionary) func emitInterfaceOrientationChanged(params: NSDictionary) diff --git a/src/EventEmitter.ts b/src/EventEmitter.ts index e7bbac9..a827218 100644 --- a/src/EventEmitter.ts +++ b/src/EventEmitter.ts @@ -1,10 +1,10 @@ -import { type EventSubscription } from 'react-native'; +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 listenerCount = 0; + private static androidListenerCount = 0; static addDeviceOrientationDidChangeListener( callback: (orientation: OrientationEvent) => void @@ -12,8 +12,12 @@ class EventEmitter { let listener = NativeOrientationDirector.onDeviceOrientationChanged(callback); - EventEmitter.listenerCount++; - if (EventEmitter.listenerCount === 1) { + if (Platform.OS !== 'android') { + return listener; + } + + EventEmitter.androidListenerCount++; + if (EventEmitter.androidListenerCount === 1) { NativeOrientationDirector.enableOrientationSensors(); } @@ -45,17 +49,17 @@ class EventEmitter { return new Proxy(listener, handler); function disableOrientationSensorsIfLastListener() { - if (EventEmitter.listenerCount === 1) { - EventEmitter.listenerCount = 0; + if (EventEmitter.androidListenerCount === 1) { + EventEmitter.androidListenerCount = 0; NativeOrientationDirector.disableOrientationSensors(); return; } - if (EventEmitter.listenerCount === 0) { + if (EventEmitter.androidListenerCount === 0) { return; } - EventEmitter.listenerCount--; + EventEmitter.androidListenerCount--; return; } }