Skip to content

Commit

Permalink
RCTNetworking: Use RCTModuleRegistry to load handlers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RSNara authored and facebook-github-bot committed Apr 27, 2021
1 parent af6bcfa commit 4c5182c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Libraries/Network/RCTNetworking.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<id<RCTURLRequestHandler>> * (^)(void))getHandlers;
- (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(RCTModuleRegistry *))getHandlers;

/**
* Does a handler exist for the specified request?
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Network/RCTNetworking.mm
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ @implementation RCTNetworking
NSMutableDictionary<NSNumber *, RCTNetworkTask *> *_tasksByRequestID;
std::mutex _handlersLock;
NSArray<id<RCTURLRequestHandler>> *_handlers;
NSArray<id<RCTURLRequestHandler>> * (^_handlersProvider)(void);
NSArray<id<RCTURLRequestHandler>> * (^_handlersProvider)(RCTModuleRegistry *);
NSMutableArray<id<RCTNetworkingRequestHandler>> *_requestHandlers;
NSMutableArray<id<RCTNetworkingResponseHandler>> *_responseHandlers;
}
Expand All @@ -167,7 +167,7 @@ - (instancetype)init
return [super initWithDisabledObservation];
}

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

if (!_handlers) {
if (_handlersProvider) {
_handlers = _handlersProvider();
_handlers = _handlersProvider(self.moduleRegistry);
} else {
_handlers = [self.bridge modulesConformingToProtocol:@protocol(RCTURLRequestHandler)];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rn-tester/RNTester/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ - (Class)getModuleClassFromName:(const char *)name
return @ [[RCTGIFImageDecoder new]];
}];
} else if (moduleClass == RCTNetworking.class) {
return [[moduleClass alloc] initWithHandlersProvider:^NSArray<id<RCTURLRequestHandler>> * {
return [[moduleClass alloc] initWithHandlersProvider:^NSArray<id<RCTURLRequestHandler>> *(RCTModuleRegistry * moduleRegistry) {
return @[
[RCTHTTPRequestHandler new],
[RCTDataRequestHandler new],
Expand Down

0 comments on commit 4c5182c

Please sign in to comment.