Moved constants export to bridge init

Summary:
public
Lazy export of module constants required a sync dispatch to the main thread, which was deadlocking in some of our projects.

This moves the constants export to the initial bridge init, which may slightly increase initial startup time, but avoids the deadlock.

Reviewed By: javache

Differential Revision: D2911295

fb-gh-sync-id: 0d14a629ac4fc7ee21acd293c09595c18232659b
shipit-source-id: 0d14a629ac4fc7ee21acd293c09595c18232659b
This commit is contained in:
Nick Lockwood 2016-02-09 03:29:18 -08:00 committed by facebook-github-bot-5
parent 7b2b0c3c1c
commit 855d411321
2 changed files with 12 additions and 10 deletions

View File

@ -282,10 +282,11 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
id module = preregisteredModules[moduleName];
if (!module) {
// Check if the module class, or any of its superclasses override init
// or setBridge:. If they do, we assume that they are expecting to be
// initialized when the bridge first loads.
// or setBridge:, or has exported constants. If they do, we assume that
// they are expecting to be initialized when the bridge first loads.
if ([moduleClass instanceMethodForSelector:@selector(init)] != objectInitMethod ||
[moduleClass instancesRespondToSelector:setBridgeSelector]) {
[moduleClass instancesRespondToSelector:setBridgeSelector] ||
RCTClassOverridesInstanceMethod(moduleClass, @selector(constantsToExport))) {
module = [moduleClass new];
if (!module) {
module = (id)kCFNull;

View File

@ -17,6 +17,7 @@
@implementation RCTModuleData
{
NSDictionary<NSString *, id> *_constantsToExport;
NSString *_queueName;
__weak RCTBridge *_bridge;
NSLock *_instanceLock;
@ -79,6 +80,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDidInitializeModuleNotification
object:_bridge
userInfo:@{@"module": _instance}];
if (RCTClassOverridesInstanceMethod(_moduleClass, @selector(constantsToExport))) {
RCTAssertMainThread();
_constantsToExport = [_instance constantsToExport];
}
}
}
@ -181,13 +187,8 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
- (NSArray *)config
{
__block NSDictionary<NSString *, id> *constants;
if (RCTClassOverridesInstanceMethod(_moduleClass, @selector(constantsToExport))) {
[self instance];
RCTExecuteOnMainThread(^{
constants = [_instance constantsToExport];
}, YES);
}
__block NSDictionary<NSString *, id> *constants = _constantsToExport;
_constantsToExport = nil; // Not needed any more
if (constants.count == 0 && self.methods.count == 0) {
return (id)kCFNull; // Nothing to export