Skip to content

Commit 4c5182c

Browse files
RSNarafacebook-github-bot
authored andcommitted
RCTNetworking: Use RCTModuleRegistry to load handlers
Summary: ## Context A React Native application can configure its RCTNetworking by initializing it with id<RCTURLRequestHandler> objects. Therefore, RCTNetworking supports this initializer: ``` - (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(void))getHandlers ``` Right now, all id<RCTURLRequestHandler> 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<RCTURLRequestHandler> 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
1 parent af6bcfa commit 4c5182c

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

Libraries/Network/RCTNetworking.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Allows RCTNetworking instances to be initialized with handlers.
3232
* The handlers will be requested via the bridge's moduleForName method when required.
3333
*/
34-
- (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(void))getHandlers;
34+
- (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(RCTModuleRegistry *))getHandlers;
3535

3636
/**
3737
* Does a handler exist for the specified request?

Libraries/Network/RCTNetworking.mm

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ @implementation RCTNetworking
148148
NSMutableDictionary<NSNumber *, RCTNetworkTask *> *_tasksByRequestID;
149149
std::mutex _handlersLock;
150150
NSArray<id<RCTURLRequestHandler>> *_handlers;
151-
NSArray<id<RCTURLRequestHandler>> * (^_handlersProvider)(void);
151+
NSArray<id<RCTURLRequestHandler>> * (^_handlersProvider)(RCTModuleRegistry *);
152152
NSMutableArray<id<RCTNetworkingRequestHandler>> *_requestHandlers;
153153
NSMutableArray<id<RCTNetworkingResponseHandler>> *_responseHandlers;
154154
}
@@ -167,7 +167,7 @@ - (instancetype)init
167167
return [super initWithDisabledObservation];
168168
}
169169

170-
- (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(void))getHandlers
170+
- (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(RCTModuleRegistry *moduleRegistry))getHandlers
171171
{
172172
if (self = [super initWithDisabledObservation]) {
173173
_handlersProvider = getHandlers;
@@ -209,7 +209,7 @@ - (void)invalidate
209209

210210
if (!_handlers) {
211211
if (_handlersProvider) {
212-
_handlers = _handlersProvider();
212+
_handlers = _handlersProvider(self.moduleRegistry);
213213
} else {
214214
_handlers = [self.bridge modulesConformingToProtocol:@protocol(RCTURLRequestHandler)];
215215
}

packages/rn-tester/RNTester/AppDelegate.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ - (Class)getModuleClassFromName:(const char *)name
215215
return @ [[RCTGIFImageDecoder new]];
216216
}];
217217
} else if (moduleClass == RCTNetworking.class) {
218-
return [[moduleClass alloc] initWithHandlersProvider:^NSArray<id<RCTURLRequestHandler>> * {
218+
return [[moduleClass alloc] initWithHandlersProvider:^NSArray<id<RCTURLRequestHandler>> *(RCTModuleRegistry * moduleRegistry) {
219219
return @[
220220
[RCTHTTPRequestHandler new],
221221
[RCTDataRequestHandler new],

0 commit comments

Comments
 (0)