diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 6ca0fad9c..debc00d9a 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -282,10 +282,11 @@ RCT_EXTERN NSArray *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; diff --git a/React/Base/RCTModuleData.m b/React/Base/RCTModuleData.m index 9d4b57539..78ef8471e 100644 --- a/React/Base/RCTModuleData.m +++ b/React/Base/RCTModuleData.m @@ -17,6 +17,7 @@ @implementation RCTModuleData { + NSDictionary *_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 *constants; - if (RCTClassOverridesInstanceMethod(_moduleClass, @selector(constantsToExport))) { - [self instance]; - RCTExecuteOnMainThread(^{ - constants = [_instance constantsToExport]; - }, YES); - } + __block NSDictionary *constants = _constantsToExport; + _constantsToExport = nil; // Not needed any more if (constants.count == 0 && self.methods.count == 0) { return (id)kCFNull; // Nothing to export