From 4c5182c1cc8bafb15490adf602c87cb5bf289ffd Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 27 Apr 2021 15:00:51 -0700 Subject: [PATCH] RCTNetworking: Use RCTModuleRegistry to load handlers Summary: ## Context A React Native application can configure its RCTNetworking by initializing it with id objects. Therefore, RCTNetworking supports this initializer: ``` - (instancetype)initWithHandlersProvider:(NSArray> * (^)(void))getHandlers ``` Right now, all id are NativeModules. So, they need to be loaded using the Bridge/TurboModuleManager. ## Problem The method [that constructs RCTNetworking](https://www.internalfb.com/code/fbsource/[6530647879a5e6d5edcfad029b39879c87e97bb3]/fbobjc/Apps/Wilde/FBReactModule2/FBReactModuleAPI/FBReactModuleAPI/FBReactModule.mm?lines=1471) is shared between bridge mode and bridgeless mode. So, the shared constructor needs to know what infra to use to load the request handlers: the TurboModuleManager, when called from a bridgeless context; the bridge, when called from a bridge context. There's no easy way to let this shared constructor know what context it's being called from. We could fork the constructor, but that's not very clean. ## Changes In this refactor, RCTNetworking gives its _handlersProvider its RCTModuleRegistry. If the module was instantiated in bridgeless mode, RCTModuleRegistry will use the TurboModuleManager. If the module was instantiated in bridge mode, RCTModuleRegistry will use the bridge. Using RCTModuleRegistry allows the _handlersProvider to load id from correct infra, in both contexts. Changelog: [iOS][Changed] - Give RCTNetworking handler provider block RCTModuleRegistry Reviewed By: PeteTheHeat Differential Revision: D28013000 fbshipit-source-id: 956d660771ab18f5e7f24fcc28792f9a217146e7 --- Libraries/Network/RCTNetworking.h | 2 +- Libraries/Network/RCTNetworking.mm | 6 +++--- packages/rn-tester/RNTester/AppDelegate.mm | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Libraries/Network/RCTNetworking.h b/Libraries/Network/RCTNetworking.h index 18344302ea8034..2068f32b803038 100644 --- a/Libraries/Network/RCTNetworking.h +++ b/Libraries/Network/RCTNetworking.h @@ -31,7 +31,7 @@ * Allows RCTNetworking instances to be initialized with handlers. * The handlers will be requested via the bridge's moduleForName method when required. */ -- (instancetype)initWithHandlersProvider:(NSArray> * (^)(void))getHandlers; +- (instancetype)initWithHandlersProvider:(NSArray> * (^)(RCTModuleRegistry *))getHandlers; /** * Does a handler exist for the specified request? diff --git a/Libraries/Network/RCTNetworking.mm b/Libraries/Network/RCTNetworking.mm index 0f9c3d61c0b104..22ca45b0fa3ff4 100644 --- a/Libraries/Network/RCTNetworking.mm +++ b/Libraries/Network/RCTNetworking.mm @@ -148,7 +148,7 @@ @implementation RCTNetworking NSMutableDictionary *_tasksByRequestID; std::mutex _handlersLock; NSArray> *_handlers; - NSArray> * (^_handlersProvider)(void); + NSArray> * (^_handlersProvider)(RCTModuleRegistry *); NSMutableArray> *_requestHandlers; NSMutableArray> *_responseHandlers; } @@ -167,7 +167,7 @@ - (instancetype)init return [super initWithDisabledObservation]; } -- (instancetype)initWithHandlersProvider:(NSArray> * (^)(void))getHandlers +- (instancetype)initWithHandlersProvider:(NSArray> * (^)(RCTModuleRegistry *moduleRegistry))getHandlers { if (self = [super initWithDisabledObservation]) { _handlersProvider = getHandlers; @@ -209,7 +209,7 @@ - (void)invalidate if (!_handlers) { if (_handlersProvider) { - _handlers = _handlersProvider(); + _handlers = _handlersProvider(self.moduleRegistry); } else { _handlers = [self.bridge modulesConformingToProtocol:@protocol(RCTURLRequestHandler)]; } diff --git a/packages/rn-tester/RNTester/AppDelegate.mm b/packages/rn-tester/RNTester/AppDelegate.mm index 03cf01c9302e41..c54f94fae65bef 100644 --- a/packages/rn-tester/RNTester/AppDelegate.mm +++ b/packages/rn-tester/RNTester/AppDelegate.mm @@ -215,7 +215,7 @@ - (Class)getModuleClassFromName:(const char *)name return @ [[RCTGIFImageDecoder new]]; }]; } else if (moduleClass == RCTNetworking.class) { - return [[moduleClass alloc] initWithHandlersProvider:^NSArray> * { + return [[moduleClass alloc] initWithHandlersProvider:^NSArray> *(RCTModuleRegistry * moduleRegistry) { return @[ [RCTHTTPRequestHandler new], [RCTDataRequestHandler new],