Make Cxx modules lazy on iOS

Reviewed By: mhorowitz

Differential Revision: D4716040

fbshipit-source-id: 08cc1317e15f0b3bd1df6d76c483a560f5c43d53
This commit is contained in:
Pieter De Baets 2017-03-17 06:55:43 -07:00 committed by Facebook Github Bot
parent 99d73c8927
commit ea069b69de
3 changed files with 38 additions and 35 deletions

View File

@ -505,7 +505,7 @@ struct RCTInstanceCallback : public InstanceCallback {
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));

View File

@ -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

View File

@ -23,37 +23,36 @@ using namespace facebook::react;
std::unique_ptr<facebook::xplat::module::CxxModule> _module;
}
- (instancetype)init
{
return nil;
}
- (instancetype)initWithCxxModule:(std::unique_ptr<facebook::xplat::module::CxxModule>)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());
if ((self = [super init])) {
_module = std::move(module);
}
return self;
}
- (std::unique_ptr<facebook::xplat::module::CxxModule>)move
{
return std::move(_module);
}
+ (NSString *)moduleName
{
return @"";
}
- (NSArray *)methodsToExport
- (void)lazyInit
{
CHECK(_module) << "Can't call methodsToExport on moved 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());
}
}
}
- (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule
{
RCTAssert(NO, @"Subclass %@ must override createModule", [self class]);
return nullptr;
}
- (NSArray<id<RCTBridgeMethod>> *)methodsToExport;
{
[self lazyInit];
if (!_module) {
return nil;
}
NSMutableArray *moduleMethods = [NSMutableArray new];
for (const auto &method : _module->getMethods()) {
@ -62,9 +61,12 @@ using namespace facebook::react;
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()) {