Extract module registry creation to helper
Reviewed By: mhorowitz Differential Revision: D4721817 fbshipit-source-id: 2fa17ca5317a57d429aa75d6c1865932af27e02f
This commit is contained in:
parent
ea069b69de
commit
ce270220e4
|
@ -42,7 +42,6 @@
|
|||
|
||||
#import "NSDataBigString.h"
|
||||
#import "RCTMessageThread.h"
|
||||
#import "RCTNativeModule.h"
|
||||
#import "RCTObjcExecutor.h"
|
||||
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
|
@ -95,35 +94,6 @@ static bool isRAMBundle(NSData *script) {
|
|||
|
||||
static std::atomic_bool cxxBridgeEnabled(false);
|
||||
|
||||
namespace {
|
||||
|
||||
// RCTNativeModule arranges for native methods to be invoked on a queue which
|
||||
// is not the JS thread. C++ modules don't use RCTNativeModule, so this little
|
||||
// adapter does the work.
|
||||
|
||||
class DispatchMessageQueueThread : public MessageQueueThread {
|
||||
public:
|
||||
DispatchMessageQueueThread(RCTModuleData *moduleData)
|
||||
: moduleData_(moduleData) {}
|
||||
|
||||
void runOnQueue(std::function<void()>&& func) override {
|
||||
dispatch_async(moduleData_.methodQueue, [func=std::move(func)] {
|
||||
func();
|
||||
});
|
||||
}
|
||||
void runOnQueueSync(std::function<void()>&& func) override {
|
||||
LOG(FATAL) << "Unsupported operation";
|
||||
}
|
||||
void quitSynchronous() override {
|
||||
LOG(FATAL) << "Unsupported operation";
|
||||
}
|
||||
|
||||
private:
|
||||
RCTModuleData *moduleData_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@interface RCTCxxBridge ()
|
||||
|
||||
@property (nonatomic, weak, readonly) RCTBridge *parentBridge;
|
||||
|
@ -484,35 +454,16 @@ struct RCTInstanceCallback : public InstanceCallback {
|
|||
return _moduleDataByName[RCTBridgeModuleNameForClass(moduleClass)].hasInstance;
|
||||
}
|
||||
|
||||
- (std::shared_ptr<ModuleRegistry>)_createModuleRegistry
|
||||
- (std::shared_ptr<ModuleRegistry>)_buildModuleRegistry
|
||||
{
|
||||
if (!self.valid) {
|
||||
return {};
|
||||
}
|
||||
|
||||
[_performanceLogger markStartForTag:RCTPLNativeModulePrepareConfig];
|
||||
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTCxxBridge createModuleRegistry]", nil);
|
||||
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTCxxBridge buildModuleRegistry]", nil);
|
||||
|
||||
std::vector<std::unique_ptr<NativeModule>> modules;
|
||||
for (RCTModuleData *moduleData in _moduleDataByID) {
|
||||
if ([moduleData.moduleClass isSubclassOfClass:[RCTCxxModule class]]) {
|
||||
// If a module does not support automatic instantiation, and
|
||||
// wasn't provided as an extra module, it may not have an
|
||||
// instance. If so, skip it.
|
||||
if (![moduleData hasInstance]) {
|
||||
continue;
|
||||
}
|
||||
modules.emplace_back(std::make_unique<CxxNativeModule>(
|
||||
_reactInstance,
|
||||
[moduleData.name UTF8String],
|
||||
[moduleData] { return [(RCTCxxModule *)(moduleData.instance) createModule]; },
|
||||
std::make_shared<DispatchMessageQueueThread>(moduleData)));
|
||||
} else {
|
||||
modules.emplace_back(std::make_unique<RCTNativeModule>(self, moduleData));
|
||||
}
|
||||
}
|
||||
|
||||
auto registry = std::make_shared<ModuleRegistry>(std::move(modules));
|
||||
auto registry = buildModuleRegistry(_moduleDataByID, self, _reactInstance);
|
||||
|
||||
[_performanceLogger markStopForTag:RCTPLNativeModulePrepareConfig];
|
||||
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
||||
|
@ -538,7 +489,7 @@ struct RCTInstanceCallback : public InstanceCallback {
|
|||
std::unique_ptr<RCTInstanceCallback>(new RCTInstanceCallback(self)),
|
||||
executorFactory,
|
||||
_jsMessageThread,
|
||||
std::move([self _createModuleRegistry]));
|
||||
std::move([self _buildModuleRegistry]));
|
||||
|
||||
#if RCT_PROFILE
|
||||
if (RCTProfileIsProfiling()) {
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#include <cxxreact/MessageQueueThread.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
// RCTNativeModule arranges for native methods to be invoked on a queue which
|
||||
// is not the JS thread. C++ modules don't use RCTNativeModule, so this little
|
||||
// adapter does the work.
|
||||
|
||||
class DispatchMessageQueueThread : public MessageQueueThread {
|
||||
public:
|
||||
DispatchMessageQueueThread(RCTModuleData *moduleData)
|
||||
: moduleData_(moduleData) {}
|
||||
|
||||
void runOnQueue(std::function<void()>&& func) override {
|
||||
dispatch_async(moduleData_.methodQueue, [func=std::move(func)] {
|
||||
func();
|
||||
});
|
||||
}
|
||||
void runOnQueueSync(std::function<void()>&& func) override {
|
||||
LOG(FATAL) << "Unsupported operation";
|
||||
}
|
||||
void quitSynchronous() override {
|
||||
LOG(FATAL) << "Unsupported operation";
|
||||
}
|
||||
|
||||
private:
|
||||
RCTModuleData *moduleData_;
|
||||
};
|
||||
|
||||
} }
|
|
@ -7,12 +7,18 @@
|
|||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
|
||||
#import <React/RCTConvert.h>
|
||||
#include <JavaScriptCore/JavaScriptCore.h>
|
||||
#include <cxxreact/JSCExecutor.h>
|
||||
#include <cxxreact/ModuleRegistry.h>
|
||||
#include <folly/dynamic.h>
|
||||
#include <jschelpers/JavaScriptCore.h>
|
||||
|
||||
@class RCTBridge;
|
||||
@class RCTModuleData;
|
||||
|
||||
@interface RCTConvert (folly)
|
||||
|
||||
+ (folly::dynamic)folly_dynamic:(id)json;
|
||||
|
@ -22,6 +28,10 @@
|
|||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class Instance;
|
||||
|
||||
std::shared_ptr<ModuleRegistry> buildModuleRegistry(NSArray<RCTModuleData *> *modules, RCTBridge *bridge, const std::shared_ptr<Instance> &instance);
|
||||
|
||||
JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef);
|
||||
|
||||
/*
|
||||
|
|
|
@ -10,10 +10,15 @@
|
|||
#import "RCTCxxUtils.h"
|
||||
|
||||
#import <React/RCTFollyConvert.h>
|
||||
#import <React/RCTModuleData.h>
|
||||
#import <React/RCTUtils.h>
|
||||
|
||||
#include <cxxreact/CxxNativeModule.h>
|
||||
#include <jschelpers/Value.h>
|
||||
|
||||
#import "DispatchMessageQueueThread.h"
|
||||
#import "RCTCxxModule.h"
|
||||
#import "RCTNativeModule.h"
|
||||
|
||||
using namespace facebook::react;
|
||||
|
||||
@implementation RCTConvert (folly)
|
||||
|
@ -36,6 +41,30 @@ using namespace facebook::react;
|
|||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
std::shared_ptr<ModuleRegistry> buildModuleRegistry(NSArray<RCTModuleData *> *modules, RCTBridge *bridge, const std::shared_ptr<Instance> &instance)
|
||||
{
|
||||
std::vector<std::unique_ptr<NativeModule>> nativeModules;
|
||||
for (RCTModuleData *moduleData in modules) {
|
||||
if ([moduleData.moduleClass isSubclassOfClass:[RCTCxxModule class]]) {
|
||||
// If a module does not support automatic instantiation, and
|
||||
// wasn't provided as an extra module, it may not have an
|
||||
// instance. If so, skip it.
|
||||
if (![moduleData hasInstance]) {
|
||||
continue;
|
||||
}
|
||||
nativeModules.emplace_back(std::make_unique<CxxNativeModule>(
|
||||
instance,
|
||||
[moduleData.name UTF8String],
|
||||
[moduleData] { return [(RCTCxxModule *)(moduleData.instance) createModule]; },
|
||||
std::make_shared<DispatchMessageQueueThread>(moduleData)));
|
||||
} else {
|
||||
nativeModules.emplace_back(std::make_unique<RCTNativeModule>(bridge, moduleData));
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_shared<ModuleRegistry>(std::move(nativeModules));
|
||||
}
|
||||
|
||||
JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef)
|
||||
{
|
||||
static std::mutex s_mutex;
|
||||
|
@ -61,7 +90,7 @@ JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef)
|
|||
return ctx;
|
||||
}
|
||||
|
||||
static NSError *errorWithException(const std::exception& e)
|
||||
static NSError *errorWithException(const std::exception &e)
|
||||
{
|
||||
NSString *msg = @(e.what());
|
||||
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionary];
|
||||
|
@ -75,7 +104,7 @@ static NSError *errorWithException(const std::exception& e)
|
|||
NSError *nestedError;
|
||||
try {
|
||||
std::rethrow_if_nested(e);
|
||||
} catch(const std::exception& e) {
|
||||
} catch(const std::exception &e) {
|
||||
nestedError = errorWithException(e);
|
||||
} catch(...) {}
|
||||
|
||||
|
|
Loading…
Reference in New Issue