Defer constants gathering until after setup

Reviewed By: javache

Differential Revision: D2986095

fb-gh-sync-id: 1f449e69ca74466e7951d621ceaf624abe034139
shipit-source-id: 1f449e69ca74466e7951d621ceaf624abe034139
This commit is contained in:
Nick Lockwood 2016-02-29 09:47:14 -08:00 committed by Facebook Github Bot 6
parent 8d44c2db4f
commit d7d47d8120
3 changed files with 28 additions and 7 deletions

View File

@ -342,6 +342,12 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
}
}
for (RCTModuleData *moduleData in _moduleDataByID) {
if (moduleData.hasInstance) {
[moduleData gatherConstants];
}
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

View File

@ -37,6 +37,14 @@
*/
- (void)finishSetupForInstance;
/**
* Calls `constantsToExport` on the module and stores the result. Note that
* this will init the module if it has not already been created. This method
* can be called on any thread, but may block the main thread briefly if the
* module implements `constantsToExport`.
*/
- (void)gatherConstants;
@property (nonatomic, strong, readonly) Class moduleClass;
@property (nonatomic, copy, readonly) NSString *name;

View File

@ -80,11 +80,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDidInitializeModuleNotification
object:_bridge
userInfo:@{@"module": _instance}];
if (RCTClassOverridesInstanceMethod(_moduleClass, @selector(constantsToExport))) {
RCTAssertMainThread();
_constantsToExport = [_instance constantsToExport];
}
}
}
@ -140,7 +135,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
[_instanceLock unlock];
[self finishSetupForInstance];
return _instance;
}
@ -185,10 +179,23 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
return _methods;
}
- (void)gatherConstants
{
if (!_constantsToExport) {
if (RCTClassOverridesInstanceMethod(_moduleClass, @selector(constantsToExport))) {
RCTAssert(_instance, @"constantsToExport called before instance created.");
RCTExecuteOnMainThread(^{
_constantsToExport = [_instance constantsToExport] ?: @{};
}, YES);
} else {
_constantsToExport = @{};
}
}
}
- (NSArray *)config
{
__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