Skip to content

Commit

Permalink
feat: use proper APIs for newest RN versions on new arch (#2767)
Browse files Browse the repository at this point in the history
## Description

PR introducing newest APIs on new arch for the library to be prepared
for newest RN versions and bridgeless mode.
  • Loading branch information
WoLewicki authored Feb 20, 2024
1 parent b4ebd18 commit b1808d1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
6 changes: 4 additions & 2 deletions FabricExample/ios/FabricExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -679,7 +680,8 @@
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down
2 changes: 1 addition & 1 deletion FabricExample/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ SPEC CHECKSUMS:
ReactCommon: 45b5d4f784e869c44a6f5a8fad5b114ca8f78c53
RNGestureHandler: 3ece35af01c47c38ae384e08610905388d4ed591
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: e64aa65de36c0832d04e8c7bd614396c77a80047
Yoga: 13c8ef87792450193e117976337b8527b49e8c03

PODFILE CHECKSUM: 792f7d0ed591c328474645afc856de4fd1732c31

Expand Down
5 changes: 5 additions & 0 deletions apple/RNGestureHandlerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

@interface RNGestureHandlerManager : NSObject

#ifdef RCT_NEW_ARCH_ENABLED
- (nonnull instancetype)initWithModuleRegistry:(nonnull RCTModuleRegistry *)moduleRegistry
viewRegistry:(nonnull RCTViewRegistry *)viewRegistry;
#else
- (nonnull instancetype)initWithUIManager:(nonnull RCTUIManager *)uiManager
eventDispatcher:(nonnull id<RCTEventDispatcherProtocol>)eventDispatcher;
#endif // RCT_NEW_ARCH_ENABLED

- (void)createGestureHandler:(nonnull NSString *)handlerName
tag:(nonnull NSNumber *)handlerTag
Expand Down
38 changes: 32 additions & 6 deletions apple/RNGestureHandlerManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,48 @@ @interface RNGestureHandlerManager () <RNGestureHandlerEventEmitter, RNRootViewG

@implementation RNGestureHandlerManager {
RNGestureHandlerRegistry *_registry;
RCTUIManager *_uiManager;
NSHashTable<RNRootViewGestureRecognizer *> *_rootViewGestureRecognizers;
NSMutableDictionary<NSNumber *, NSNumber *> *_attachRetryCounter;
#ifdef RCT_NEW_ARCH_ENABLED
RCTModuleRegistry *_moduleRegistry;
RCTViewRegistry *_viewRegistry;
#else
RCTUIManager *_uiManager;
#endif // RCT_NEW_ARCH_ENABLED
id<RCTEventDispatcherProtocol> _eventDispatcher;
id _reanimatedModule;
}

#ifdef RCT_NEW_ARCH_ENABLED
- (instancetype)initWithModuleRegistry:(RCTModuleRegistry *)moduleRegistry viewRegistry:(RCTViewRegistry *)viewRegistry
{
if ((self = [super init])) {
_moduleRegistry = moduleRegistry;
_viewRegistry = viewRegistry;
_eventDispatcher = [_moduleRegistry moduleForName:"EventDispatcher"];
[self initCommonProps];
}
return self;
}
#else
- (instancetype)initWithUIManager:(RCTUIManager *)uiManager
eventDispatcher:(id<RCTEventDispatcherProtocol>)eventDispatcher
{
if ((self = [super init])) {
_uiManager = uiManager;
_eventDispatcher = eventDispatcher;
_registry = [RNGestureHandlerRegistry new];
_rootViewGestureRecognizers = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
_attachRetryCounter = [[NSMutableDictionary alloc] init];
_reanimatedModule = nil;
[self initCommonProps];
}
return self;
}
#endif // RCT_NEW_ARCH_ENABLED

- (void)initCommonProps
{
_registry = [RNGestureHandlerRegistry new];
_rootViewGestureRecognizers = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
_attachRetryCounter = [[NSMutableDictionary alloc] init];
}

- (void)createGestureHandler:(NSString *)handlerName tag:(NSNumber *)handlerTag config:(NSDictionary *)config
{
Expand Down Expand Up @@ -115,7 +137,11 @@ - (void)attachGestureHandler:(nonnull NSNumber *)handlerTag
toViewWithTag:(nonnull NSNumber *)viewTag
withActionType:(RNGestureHandlerActionType)actionType
{
#ifdef RCT_NEW_ARCH_ENABLED
RNGHUIView *view = [_viewRegistry viewForReactTag:viewTag];
#else
RNGHUIView *view = [_uiManager viewForReactTag:viewTag];
#endif // RCT_NEW_ARCH_ENABLED

#ifdef RCT_NEW_ARCH_ENABLED
if (view == nil || view.superview == nil) {
Expand Down Expand Up @@ -350,7 +376,7 @@ - (void)sendEventForReanimated:(RNGestureHandlerStateChange *)event
#ifdef RCT_NEW_ARCH_ENABLED
// Send event directly to Reanimated
if (_reanimatedModule == nil) {
_reanimatedModule = [_uiManager.bridge moduleForName:@"ReanimatedModule"];
_reanimatedModule = [_moduleRegistry moduleForName:"ReanimatedModule"];
}

[_reanimatedModule eventDispatcherWillDispatchEvent:event];
Expand Down
7 changes: 4 additions & 3 deletions apple/RNGestureHandlerModule.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#import <React/RCTEventEmitter.h>
#import <React/RCTUIManager.h>

#ifdef RN_FABRIC_ENABLED
#ifdef RCT_NEW_ARCH_ENABLED
#import <React/RCTInitializing.h>
#import <rngesturehandler_codegen/rngesturehandler_codegen.h>
#else
#import <React/RCTBridgeModule.h>
#endif

@interface RNGestureHandlerModule : RCTEventEmitter
#ifdef RN_FABRIC_ENABLED
<NativeRNGestureHandlerModuleSpec>
#ifdef RCT_NEW_ARCH_ENABLED
<NativeRNGestureHandlerModuleSpec, RCTInitializing>
#else
<RCTBridgeModule>
#endif
Expand Down
17 changes: 14 additions & 3 deletions apple/RNGestureHandlerModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ @implementation RNGestureHandlerModule {
NSMutableArray<GestureHandlerOperation> *_operations;
}

#ifdef RCT_NEW_ARCH_ENABLED
@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
#endif // RCT_NEW_ARCH_ENABLED

RCT_EXPORT_MODULE()

+ (BOOL)requiresMainQueueSetup
Expand Down Expand Up @@ -104,6 +108,14 @@ void decorateRuntime(jsi::Runtime &runtime)
}
#endif // RCT_NEW_ARCH_ENABLED

#ifdef RCT_NEW_ARCH_ENABLED
- (void)initialize
{
_manager = [[RNGestureHandlerManager alloc] initWithModuleRegistry:self.moduleRegistry
viewRegistry:_viewRegistry_DEPRECATED];
_operations = [NSMutableArray new];
}
#else
- (void)setBridge:(RCTBridge *)bridge
{
[super setBridge:bridge];
Expand All @@ -112,10 +124,9 @@ - (void)setBridge:(RCTBridge *)bridge
eventDispatcher:bridge.eventDispatcher];
_operations = [NSMutableArray new];

#ifndef RCT_NEW_ARCH_ENABLED
[bridge.uiManager.observerCoordinator addObserver:self];
#endif // RCT_NEW_ARCH_ENABLED
}
#endif // RCT_NEW_ARCH_ENABLED

#ifdef RCT_NEW_ARCH_ENABLED
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install)
Expand Down Expand Up @@ -295,7 +306,7 @@ - (NSDictionary *)constantsToExport
};
}

#if RN_FABRIC_ENABLED
#if RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
Expand Down

0 comments on commit b1808d1

Please sign in to comment.