Skip to content

Commit

Permalink
Merge allowOffMainQueueRegistration and requiresMainQueueSetup
Browse files Browse the repository at this point in the history
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 980d514
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
15 changes: 1 addition & 14 deletions React/Base/RCTModuleData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions React/Base/RCTModuleData.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 980d514

Please sign in to comment.