Permalink
Browse files

Make Cxx modules lazy on iOS

Reviewed By: mhorowitz

Differential Revision: D4716040

fbshipit-source-id: 08cc1317e15f0b3bd1df6d76c483a560f5c43d53
  • Loading branch information...
javache authored and facebook-github-bot committed Mar 17, 2017
1 parent 99d73c8 commit ea069b69de427832226c4fe6e32803b4c7c31d2c
Showing with 33 additions and 30 deletions.
  1. +1 −1 React/CxxBridge/RCTCxxBridge.mm
  2. +8 −7 React/CxxModule/RCTCxxModule.h
  3. +24 −22 React/CxxModule/RCTCxxModule.mm
@@ -505,7 +505,7 @@ - (BOOL)moduleIsInitialized:(Class)moduleClass
modules.emplace_back(std::make_unique<CxxNativeModule>(
_reactInstance,
[moduleData.name UTF8String],
[moduleData] { return [(RCTCxxModule *)(moduleData.instance) move]; },
[moduleData] { return [(RCTCxxModule *)(moduleData.instance) createModule]; },
std::make_shared<DispatchMessageQueueThread>(moduleData)));
} else {
modules.emplace_back(std::make_unique<RCTNativeModule>(self, moduleData));
@@ -14,14 +14,15 @@
#import <React/RCTBridgeModule.h>
#import <cxxreact/CxxModule.h>
/**
* Subclass RCTCxxModule to use cross-platform CxxModule on iOS.
*
* Subclasses must implement the createModule method to lazily produce the module. When running under the Cxx bridge
* modules will be accessed directly, under the Objective-C bridge method access is wrapped through RCTCxxMethod.
*/
@interface RCTCxxModule : NSObject <RCTBridgeModule>
- (instancetype)initWithCxxModule:(std::unique_ptr<facebook::xplat::module::CxxModule>)module;
- (NSArray *)methodsToExport;
- (NSDictionary *)constantsToExport;
// Extracts the module from its objc wrapper
- (std::unique_ptr<facebook::xplat::module::CxxModule>)move;
// To be implemented by subclasses
- (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule;
@end
@@ -23,37 +23,36 @@ @implementation RCTCxxModule
std::unique_ptr<facebook::xplat::module::CxxModule> _module;
}
- (instancetype)init
+ (NSString *)moduleName
{
return nil;
return @"";
}
- (instancetype)initWithCxxModule:(std::unique_ptr<facebook::xplat::module::CxxModule>)module
- (void)lazyInit
{
RCTAssert([RCTBridgeModuleNameForClass([self class]) isEqualToString:@(module->getName().c_str())],
@"CxxModule class name %@ does not match runtime name %s",
RCTBridgeModuleNameForClass([self class]), module->getName().c_str());
if ((self = [super init])) {
_module = std::move(module);
if (!_module) {
_module = [self createModule];
if (_module) {
RCTAssert([RCTBridgeModuleNameForClass([self class]) isEqualToString:@(_module->getName().c_str())],
@"CxxModule class name %@ does not match runtime name %s",
RCTBridgeModuleNameForClass([self class]), _module->getName().c_str());
}
}
return self;
}
- (std::unique_ptr<facebook::xplat::module::CxxModule>)move
- (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule
{
return std::move(_module);
RCTAssert(NO, @"Subclass %@ must override createModule", [self class]);
return nullptr;
}
+ (NSString *)moduleName
- (NSArray<id<RCTBridgeMethod>> *)methodsToExport;
{
return @"";
}
- (NSArray *)methodsToExport
{
CHECK(_module) << "Can't call methodsToExport on moved module";
[self lazyInit];
if (!_module) {
return nil;
}
NSMutableArray *moduleMethods = [NSMutableArray new];
for (const auto &method : _module->getMethods()) {
@@ -62,9 +61,12 @@ - (NSArray *)methodsToExport
return moduleMethods;
}
- (NSDictionary *)constantsToExport
- (NSDictionary<NSString *, id> *)constantsToExport;
{
CHECK(_module) << "Can't call constantsToExport on moved module";
[self lazyInit];
if (!_module) {
return nil;
}
NSMutableDictionary *moduleConstants = [NSMutableDictionary new];
for (const auto &c : _module->getConstants()) {

0 comments on commit ea069b6

Please sign in to comment.