Skip to content

Commit

Permalink
iOS: allow getting an instance of a js-bound module via the bridge
Browse files Browse the repository at this point in the history
Reviewed By: sebmarkbage

Differential Revision: D6982785

fbshipit-source-id: 7bbcc5416e1d1a3a577328349a7c18af5c0f8577
  • Loading branch information
fkgozali authored and facebook-github-bot committed Feb 14, 2018
1 parent 1aeb925 commit 5f48bd8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions React/Base/RCTBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
*/
- (BOOL)moduleIsInitialized:(Class)moduleClass;

/**
* Retrieve an extra module that gets bound to the JS context, if any.
*/
- (id)jsBoundExtraModuleForClass:(Class)moduleClass;

/**
* All registered bridge module classes.
*/
Expand Down
5 changes: 5 additions & 0 deletions React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ - (BOOL)moduleIsInitialized:(Class)moduleClass
return [self.batchedBridge moduleIsInitialized:moduleClass];
}

- (id)jsBoundExtraModuleForClass:(Class)moduleClass
{
return [self.batchedBridge jsBoundExtraModuleForClass:moduleClass];
}

- (void)reload
{
#if RCT_ENABLE_INSPECTOR
Expand Down
12 changes: 12 additions & 0 deletions React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,18 @@ - (BOOL)moduleIsInitialized:(Class)moduleClass
return _moduleDataByName[RCTBridgeModuleNameForClass(moduleClass)].hasInstance;
}

- (id)jsBoundExtraModuleForClass:(Class)moduleClass
{
if ([self.delegate conformsToProtocol:@protocol(RCTCxxBridgeDelegate)]) {
id<RCTCxxBridgeDelegate> cxxDelegate = (id<RCTCxxBridgeDelegate>) self.delegate;
if ([cxxDelegate respondsToSelector:@selector(jsBoundExtraModuleForClass:)]) {
return [cxxDelegate jsBoundExtraModuleForClass:moduleClass];
}
}

return nil;
}

- (std::shared_ptr<ModuleRegistry>)_buildModuleRegistry
{
if (!self.valid) {
Expand Down
5 changes: 5 additions & 0 deletions React/CxxBridge/RCTCxxBridgeDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ class JSExecutorFactory;
*/
- (void)installExtraJSBinding:(JSGlobalContextRef)jsContextRef;

/**
* Experimental: Get the instance of the extra module/class which gets bound via `installExtraJSBinding:`
*/
- (id)jsBoundExtraModuleForClass:(Class)moduleClass;

@end
6 changes: 6 additions & 0 deletions React/Fabric/RCTFabricUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@
childSet:(NSArray<RCTShadowView *> *)childSet;

@end

@interface RCTBridge (RCTFabricUIManager)

@property (nonatomic, readonly) RCTFabricUIManager *fabricUIManager;

@end
9 changes: 9 additions & 0 deletions React/Fabric/RCTFabricUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ - (void)completeRoot:(nonnull NSNumber *)rootTag
}

@end

@implementation RCTBridge (RCTFabricUIManager)

- (RCTFabricUIManager *)fabricUIManager
{
return [self jsBoundExtraModuleForClass:[RCTFabricUIManager class]];
}

@end

0 comments on commit 5f48bd8

Please sign in to comment.