Skip to content

Commit

Permalink
Attach RCTBundleManager to NativeModules
Browse files Browse the repository at this point in the history
Summary:
After this diff, every NativeModule that has `synthesize bundleManager = _bundleManager`, will get access to an RCTBundleManager, that it can use to read from/write to the bridge's bundle URL.

Changelog: [iOS][Added] - Attach RCTBundleManager to NativeModules

Reviewed By: PeteTheHeat

Differential Revision: D28086319

fbshipit-source-id: 6e4cd815d300e9036957ec8c743e947d2cb3f365
  • Loading branch information
RSNara authored and facebook-github-bot committed May 5, 2021
1 parent 4a1bafe commit 329f58e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
10 changes: 7 additions & 3 deletions React/Base/RCTModuleData.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@class RCTBridge;
@class RCTModuleRegistry;
@class RCTViewRegistry;
@class RCTBundleManager;

typedef id<RCTBridgeModule> (^RCTBridgeModuleProvider)(void);

Expand All @@ -23,18 +24,21 @@ typedef id<RCTBridgeModule> (^RCTBridgeModuleProvider)(void);
- (instancetype)initWithModuleClass:(Class)moduleClass
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED;
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager;

- (instancetype)initWithModuleClass:(Class)moduleClass
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED NS_DESIGNATED_INITIALIZER;
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED NS_DESIGNATED_INITIALIZER;
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager NS_DESIGNATED_INITIALIZER;

/**
* Calls `constantsToExport` on the module and stores the result. Note that
Expand Down
27 changes: 26 additions & 1 deletion React/Base/RCTModuleData.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ @implementation RCTModuleData {
BOOL _setupComplete;
RCTModuleRegistry *_moduleRegistry;
RCTViewRegistry *_viewRegistry_DEPRECATED;
RCTBundleManager *_bundleManager;
}

@synthesize methods = _methods;
Expand Down Expand Up @@ -103,28 +104,32 @@ - (instancetype)initWithModuleClass:(Class)moduleClass
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager
{
return [self initWithModuleClass:moduleClass
moduleProvider:^id<RCTBridgeModule> {
return [moduleClass new];
}
bridge:bridge
moduleRegistry:moduleRegistry
viewRegistry_DEPRECATED:viewRegistry_DEPRECATED];
viewRegistry_DEPRECATED:viewRegistry_DEPRECATED
bundleManager:bundleManager];
}

- (instancetype)initWithModuleClass:(Class)moduleClass
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager
{
if (self = [super init]) {
_bridge = bridge;
_moduleClass = moduleClass;
_moduleProvider = [moduleProvider copy];
_moduleRegistry = moduleRegistry;
_viewRegistry_DEPRECATED = viewRegistry_DEPRECATED;
_bundleManager = bundleManager;
[self setUp];
}
return self;
Expand All @@ -134,13 +139,15 @@ - (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager
{
if (self = [super init]) {
_bridge = bridge;
_instance = instance;
_moduleClass = [instance class];
_moduleRegistry = moduleRegistry;
_viewRegistry_DEPRECATED = viewRegistry_DEPRECATED;
_bundleManager = bundleManager;
[self setUp];
}
return self;
Expand Down Expand Up @@ -203,6 +210,7 @@ - (void)setUpInstanceAndBridge:(int32_t)requestId
[self setBridgeForInstance];
[self setModuleRegistryForInstance];
[self setViewRegistryForInstance];
[self setBundleManagerForInstance];
}

[self setUpMethodQueue];
Expand Down Expand Up @@ -284,6 +292,23 @@ - (void)setViewRegistryForInstance
}
}

- (void)setBundleManagerForInstance
{
if ([_instance respondsToSelector:@selector(bundleManager)] && _instance.bundleManager != _bundleManager) {
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setBundleManagerForInstance]", nil);
@try {
[(id)_instance setValue:_bundleManager forKey:@"bundleManager"];
} @catch (NSException *exception) {
RCTLogError(
@"%@ has no setter or ivar for its module registry, which is not "
"permitted. You must either @synthesize the bundleManager property, "
"or provide your own setter method.",
self.name);
}
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
}
}

- (void)finishSetupForInstance
{
if (!_setupComplete && _instance) {
Expand Down
12 changes: 9 additions & 3 deletions React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ @implementation RCTCxxBridge {

RCTModuleRegistry *_objCModuleRegistry;
RCTViewRegistry *_viewRegistry_DEPRECATED;
RCTBundleManager *_bundleManager;
}

@synthesize bridgeDescription = _bridgeDescription;
Expand Down Expand Up @@ -300,6 +301,8 @@ - (instancetype)initWithParentBridge:(RCTBridge *)bridge
_moduleDataByID = [NSMutableArray new];
_objCModuleRegistry = [RCTModuleRegistry new];
[_objCModuleRegistry setBridge:self];
_bundleManager = [RCTBundleManager new];
[_bundleManager setBridge:self];
_viewRegistry_DEPRECATED = [RCTViewRegistry new];
[_viewRegistry_DEPRECATED setBridge:self];

Expand Down Expand Up @@ -778,7 +781,8 @@ - (void)updateModuleWithInstance:(id<RCTBridgeModule>)instance
moduleData = [[RCTModuleData alloc] initWithModuleClass:moduleClass
bridge:self
moduleRegistry:_objCModuleRegistry
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED];
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED
bundleManager:_bundleManager];
BridgeNativeModulePerfLogger::moduleDataCreateEnd([moduleName UTF8String], moduleDataId);

_moduleDataByName[moduleName] = moduleData;
Expand Down Expand Up @@ -853,7 +857,8 @@ - (void)registerExtraModules
RCTModuleData *moduleData = [[RCTModuleData alloc] initWithModuleInstance:module
bridge:self
moduleRegistry:_objCModuleRegistry
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED];
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED
bundleManager:_bundleManager];
BridgeNativeModulePerfLogger::moduleDataCreateEnd([moduleName UTF8String], moduleDataId);

_moduleDataByName[moduleName] = moduleData;
Expand Down Expand Up @@ -907,7 +912,8 @@ - (void)registerExtraLazyModules
moduleData = [[RCTModuleData alloc] initWithModuleClass:moduleClass
bridge:self
moduleRegistry:_objCModuleRegistry
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED];
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED
bundleManager:_bundleManager];
BridgeNativeModulePerfLogger::moduleDataCreateEnd([moduleName UTF8String], moduleDataId);

_moduleDataByName[moduleName] = moduleData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ - (TurboModuleHolder *)_getOrCreateTurboModuleHolder:(const char *)moduleName
RCTModuleData *data = [[RCTModuleData alloc] initWithModuleInstance:(id<RCTBridgeModule>)module
bridge:_bridge
moduleRegistry:_moduleRegistry
viewRegistry_DEPRECATED:nil];
viewRegistry_DEPRECATED:nil
bundleManager:nil];
[_bridge registerModuleForFrameUpdates:(id<RCTBridgeModule>)module withModuleData:data];
}

Expand Down

0 comments on commit 329f58e

Please sign in to comment.