Permalink
Browse files

Merge allowOffMainQueueRegistration and requiresMainQueueSetup

Reviewed By: fromcelticpark

Differential Revision: D5398021

fbshipit-source-id: 7e721cce579678f4c82582f5068cf46574afe961
  • Loading branch information...
javache authored and facebook-github-bot committed Jul 17, 2017
1 parent 7a4eda2 commit 980d5140d62ee5e65aa45ad3b4b41b526d60e990
Showing with 11 additions and 23 deletions.
  1. +1 −14 React/Base/RCTModuleData.h
  2. +9 −8 React/Base/RCTModuleData.mm
  3. +1 −1 React/CxxBridge/RCTCxxBridge.mm
View
@@ -58,27 +58,14 @@ typedef id<RCTBridgeModule>(^RCTBridgeModuleProvider)(void);
/**
* Returns YES if module instance must be created on the main thread.
* May be overriden by "allowOffMainQueueRegistration".
*/
@property (nonatomic, assign, readonly) BOOL requiresMainQueueSetup;
@property (nonatomic, assign) BOOL requiresMainQueueSetup;
/**
* Returns YES if module has constants to export.
* May be overriden by "allowOffMainQueueRegistration".
*/
@property (nonatomic, assign, readonly) BOOL hasConstantsToExport;
/**
* If set to YES, it will force both setup and constants export process
* to explicitly happen off the main queue.
* Overrides "requiresMainQueueSetup" & "hasConstantsToExport"
* Defaults to NO.
*
* @experimental
*/
@property (nonatomic, assign) BOOL allowOffMainQueueRegistration;
/**
* Returns the current module instance. Note that this will init the instance
* if it has not already been created. To check if the module instance exists
@@ -44,14 +44,15 @@ - (void)setUp
objectInitMethod = [NSObject instanceMethodForSelector:@selector(init)];
});
// If a module overrides `init` then we must assume that it expects to be
// initialized on the main thread, because it may need to access UIKit.
_requiresMainQueueSetup = !_instance &&
[_moduleClass instanceMethodForSelector:@selector(init)] != objectInitMethod;
// If a module overrides `constantsToExport` then we must assume that it
// must be called on the main thread, because it may need to access UIKit.
_hasConstantsToExport = [_moduleClass instancesRespondToSelector:@selector(constantsToExport)];
// If a module overrides `init` then we must assume that it expects to be
// initialized on the main thread, because it may need to access UIKit.
const BOOL hasCustomInit = !_instance && [_moduleClass instanceMethodForSelector:@selector(init)] != objectInitMethod;
_requiresMainQueueSetup = _hasConstantsToExport || hasCustomInit;
}
- (instancetype)initWithModuleClass:(Class)moduleClass
@@ -101,7 +102,7 @@ - (void)setUpInstanceAndBridge
if (!_setupComplete && _bridge.valid) {
if (!_instance) {
if (RCT_DEBUG && _requiresMainQueueSetup && !_allowOffMainQueueRegistration) {
if (RCT_DEBUG && _requiresMainQueueSetup) {
RCTAssertMainQueue();
}
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpInstanceAndBridge] Create module", nil);
@@ -222,7 +223,7 @@ - (BOOL)hasInstance
{
if (!_setupComplete) {
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, ([NSString stringWithFormat:@"[RCTModuleData instanceForClass:%@]", _moduleClass]), nil);
if (_requiresMainQueueSetup && !_allowOffMainQueueRegistration) {
if (_requiresMainQueueSetup) {
// The chances of deadlock here are low, because module init very rarely
// calls out to other threads, however we can't control when a module might
// get accessed by client code during bridge setup, and a very low risk of
@@ -295,7 +296,7 @@ - (void)gatherConstants
if (_hasConstantsToExport && !_constantsToExport) {
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, ([NSString stringWithFormat:@"[RCTModuleData gatherConstants] %@", _moduleClass]), nil);
(void)[self instance];
if (_allowOffMainQueueRegistration) {
if (!_requiresMainQueueSetup) {
_constantsToExport = [_instance constantsToExport] ?: @{};
} else {
if (!RCTIsMainQueue()) {
@@ -651,7 +651,7 @@ - (void)_prepareModulesWithDispatchGroup:(dispatch_group_t)dispatchGroup
continue;
}
if (moduleData.requiresMainQueueSetup || moduleData.hasConstantsToExport) {
if (moduleData.requiresMainQueueSetup) {
// Modules that need to be set up on the main thread cannot be initialized
// lazily when required without doing a dispatch_sync to the main thread,
// which can result in deadlock. To avoid this, we initialize all of these

0 comments on commit 980d514

Please sign in to comment.