Bring back D2570057 (previously backed out) + fixes
Reviewed By: nicklockwood Differential Revision: D2590604 fb-gh-sync-id: 63a0e0c6afda740f22aacb3f469d411f236fa16b
This commit is contained in:
parent
89f1e6732e
commit
40f513aa71
|
@ -136,19 +136,25 @@ _Pragma("clang diagnostic pop")
|
|||
NSString *injectedStuff;
|
||||
RUN_RUNLOOP_WHILE(!(injectedStuff = executor.injectedStuff[@"__fbBatchedBridgeConfig"]));
|
||||
|
||||
NSDictionary *moduleConfig = RCTJSONParse(injectedStuff, NULL);
|
||||
NSDictionary *remoteModuleConfig = moduleConfig[@"remoteModuleConfig"];
|
||||
NSDictionary *testModuleConfig = remoteModuleConfig[@"TestModule"];
|
||||
NSDictionary *constants = testModuleConfig[@"constants"];
|
||||
NSDictionary *methods = testModuleConfig[@"methods"];
|
||||
__block NSNumber *testModuleID = nil;
|
||||
__block NSDictionary *testConstants = nil;
|
||||
__block NSNumber *testMethodID = nil;
|
||||
|
||||
NSArray *remoteModuleConfig = RCTJSONParse(injectedStuff, NULL)[@"remoteModuleConfig"];
|
||||
[remoteModuleConfig enumerateObjectsUsingBlock:^(id moduleConfig, NSUInteger i, BOOL *stop) {
|
||||
if ([moduleConfig isKindOfClass:[NSArray class]] && [moduleConfig[0] isEqualToString:@"TestModule"]) {
|
||||
testModuleID = @(i);
|
||||
testConstants = moduleConfig[1];
|
||||
testMethodID = @([moduleConfig[2] indexOfObject:@"testMethod"]);
|
||||
*stop = YES;
|
||||
}
|
||||
}];
|
||||
|
||||
XCTAssertNotNil(moduleConfig);
|
||||
XCTAssertNotNil(remoteModuleConfig);
|
||||
XCTAssertNotNil(testModuleConfig);
|
||||
XCTAssertNotNil(constants);
|
||||
XCTAssertEqualObjects(constants[@"eleventyMillion"], @42);
|
||||
XCTAssertNotNil(methods);
|
||||
XCTAssertNotNil(methods[@"testMethod"]);
|
||||
XCTAssertNotNil(testModuleID);
|
||||
XCTAssertNotNil(testConstants);
|
||||
XCTAssertEqualObjects(testConstants[@"eleventyMillion"], @42);
|
||||
XCTAssertNotNil(testMethodID);
|
||||
}
|
||||
|
||||
- (void)testCallNativeMethod
|
||||
|
@ -158,13 +164,19 @@ _Pragma("clang diagnostic pop")
|
|||
NSString *injectedStuff;
|
||||
RUN_RUNLOOP_WHILE(!(injectedStuff = executor.injectedStuff[@"__fbBatchedBridgeConfig"]));
|
||||
|
||||
NSDictionary *moduleConfig = RCTJSONParse(injectedStuff, NULL);
|
||||
NSDictionary *remoteModuleConfig = moduleConfig[@"remoteModuleConfig"];
|
||||
NSDictionary *testModuleConfig = remoteModuleConfig[@"TestModule"];
|
||||
NSNumber *testModuleID = testModuleConfig[@"moduleID"];
|
||||
NSDictionary *methods = testModuleConfig[@"methods"];
|
||||
NSDictionary *testMethod = methods[@"testMethod"];
|
||||
NSNumber *testMethodID = testMethod[@"methodID"];
|
||||
__block NSNumber *testModuleID = nil;
|
||||
__block NSDictionary *testConstants = nil;
|
||||
__block NSNumber *testMethodID = nil;
|
||||
|
||||
NSArray *remoteModuleConfig = RCTJSONParse(injectedStuff, NULL)[@"remoteModuleConfig"];
|
||||
[remoteModuleConfig enumerateObjectsUsingBlock:^(id moduleConfig, NSUInteger i, __unused BOOL *stop) {
|
||||
if ([moduleConfig isKindOfClass:[NSArray class]] && [moduleConfig[0] isEqualToString:@"TestModule"]) {
|
||||
testModuleID = @(i);
|
||||
testConstants = moduleConfig[1];
|
||||
testMethodID = @([moduleConfig[2] indexOfObject:@"testMethod"]);
|
||||
*stop = YES;
|
||||
}
|
||||
}];
|
||||
|
||||
NSArray *args = @[@1234, @5678, @"stringy", @{@"a": @1}, @42];
|
||||
NSArray *buffer = @[@[testModuleID], @[testMethodID], @[args], @[], @1234567];
|
||||
|
|
|
@ -30,7 +30,6 @@ let MIN_TIME_BETWEEN_FLUSHES_MS = 5;
|
|||
let SPY_MODE = false;
|
||||
|
||||
let MethodTypes = keyMirror({
|
||||
local: null,
|
||||
remote: null,
|
||||
remoteAsync: null,
|
||||
});
|
||||
|
@ -62,15 +61,18 @@ class MessageQueue {
|
|||
'flushedQueue',
|
||||
].forEach((fn) => this[fn] = this[fn].bind(this));
|
||||
|
||||
this._genModules(remoteModules);
|
||||
let modulesConfig = this._genModulesConfig(remoteModules);
|
||||
this._genModules(modulesConfig);
|
||||
localModules && this._genLookupTables(
|
||||
localModules, this._moduleTable, this._methodTable);
|
||||
this._genModulesConfig(localModules),this._moduleTable, this._methodTable
|
||||
);
|
||||
|
||||
this._debugInfo = {};
|
||||
this._remoteModuleTable = {};
|
||||
this._remoteMethodTable = {};
|
||||
this._genLookupTables(
|
||||
remoteModules, this._remoteModuleTable, this._remoteMethodTable);
|
||||
modulesConfig, this._remoteModuleTable, this._remoteMethodTable
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -182,54 +184,102 @@ class MessageQueue {
|
|||
/**
|
||||
* Private helper methods
|
||||
*/
|
||||
_genLookupTables(localModules, moduleTable, methodTable) {
|
||||
let moduleNames = Object.keys(localModules);
|
||||
for (var i = 0, l = moduleNames.length; i < l; i++) {
|
||||
let moduleName = moduleNames[i];
|
||||
let methods = localModules[moduleName].methods || {};
|
||||
let moduleID = localModules[moduleName].moduleID;
|
||||
moduleTable[moduleID] = moduleName;
|
||||
methodTable[moduleID] = {};
|
||||
|
||||
let methodNames = Object.keys(methods);
|
||||
for (var j = 0, k = methodNames.length; j < k; j++) {
|
||||
let methodName = methodNames[j];
|
||||
let methodConfig = methods[methodName];
|
||||
methodTable[moduleID][methodConfig.methodID] = methodName;
|
||||
/**
|
||||
* Converts the old, object-based module structure to the new
|
||||
* array-based structure. TODO (t8823865) Removed this
|
||||
* function once Android has been updated.
|
||||
*/
|
||||
_genModulesConfig(modules /* array or object */) {
|
||||
if (Array.isArray(modules)) {
|
||||
return modules;
|
||||
} else {
|
||||
let moduleArray = [];
|
||||
let moduleNames = Object.keys(modules);
|
||||
for (var i = 0, l = moduleNames.length; i < l; i++) {
|
||||
let moduleName = moduleNames[i];
|
||||
let moduleConfig = modules[moduleName];
|
||||
let module = [moduleName];
|
||||
if (moduleConfig.constants) {
|
||||
module.push(moduleConfig.constants);
|
||||
}
|
||||
let methodsConfig = moduleConfig.methods;
|
||||
if (methodsConfig) {
|
||||
let methods = [];
|
||||
let asyncMethods = [];
|
||||
let methodNames = Object.keys(methodsConfig);
|
||||
for (var j = 0, ll = methodNames.length; j < ll; j++) {
|
||||
let methodName = methodNames[j];
|
||||
let methodConfig = methodsConfig[methodName];
|
||||
methods[methodConfig.methodID] = methodName;
|
||||
if (methodConfig.type === MethodTypes.remoteAsync) {
|
||||
asyncMethods.push(methodConfig.methodID);
|
||||
}
|
||||
}
|
||||
if (methods.length) {
|
||||
module.push(methods);
|
||||
if (asyncMethods.length) {
|
||||
module.push(asyncMethods);
|
||||
}
|
||||
}
|
||||
}
|
||||
moduleArray[moduleConfig.moduleID] = module;
|
||||
}
|
||||
return moduleArray;
|
||||
}
|
||||
}
|
||||
|
||||
_genLookupTables(modulesConfig, moduleTable, methodTable) {
|
||||
modulesConfig.forEach((module, moduleID) => {
|
||||
if (!module) {
|
||||
return;
|
||||
}
|
||||
|
||||
let moduleName, methods;
|
||||
if (moduleHasConstants(module)) {
|
||||
[moduleName, , methods] = module;
|
||||
} else {
|
||||
[moduleName, methods] = module;
|
||||
}
|
||||
|
||||
moduleTable[moduleID] = moduleName;
|
||||
methodTable[moduleID] = Object.assign({}, methods);
|
||||
});
|
||||
}
|
||||
|
||||
_genModules(remoteModules) {
|
||||
let moduleNames = Object.keys(remoteModules);
|
||||
for (var i = 0, l = moduleNames.length; i < l; i++) {
|
||||
let moduleName = moduleNames[i];
|
||||
let moduleConfig = remoteModules[moduleName];
|
||||
remoteModules.forEach((module, moduleID) => {
|
||||
if (!module) {
|
||||
return;
|
||||
}
|
||||
|
||||
let moduleName, constants, methods, asyncMethods;
|
||||
if (moduleHasConstants(module)) {
|
||||
[moduleName, constants, methods, asyncMethods] = module;
|
||||
} else {
|
||||
[moduleName, methods, asyncMethods] = module;
|
||||
}
|
||||
|
||||
const moduleConfig = {moduleID, constants, methods, asyncMethods};
|
||||
this.RemoteModules[moduleName] = this._genModule({}, moduleConfig);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_genModule(module, moduleConfig) {
|
||||
let methods = moduleConfig.methods || {};
|
||||
let methodNames = Object.keys(methods);
|
||||
for (var i = 0, l = methodNames.length; i < l; i++) {
|
||||
let methodName = methodNames[i];
|
||||
let methodConfig = methods[methodName];
|
||||
module[methodName] = this._genMethod(
|
||||
moduleConfig.moduleID,
|
||||
methodConfig.methodID,
|
||||
methodConfig.type || MethodTypes.remote
|
||||
);
|
||||
}
|
||||
Object.assign(module, moduleConfig.constants);
|
||||
const {moduleID, constants, methods = [], asyncMethods = []} = moduleConfig;
|
||||
|
||||
methods.forEach((methodName, methodID) => {
|
||||
const methodType =
|
||||
arrayContains(asyncMethods, methodID) ?
|
||||
MethodTypes.remoteAsync : MethodTypes.remote;
|
||||
module[methodName] = this._genMethod(moduleID, methodID, methodType);
|
||||
});
|
||||
Object.assign(module, constants);
|
||||
|
||||
return module;
|
||||
}
|
||||
|
||||
_genMethod(module, method, type) {
|
||||
if (type === MethodTypes.local) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let fn = null;
|
||||
let self = this;
|
||||
if (type === MethodTypes.remoteAsync) {
|
||||
|
@ -264,6 +314,14 @@ class MessageQueue {
|
|||
|
||||
}
|
||||
|
||||
function moduleHasConstants(moduleArray: Array<Object|Array<>>): boolean {
|
||||
return !Array.isArray(moduleArray[1]);
|
||||
}
|
||||
|
||||
function arrayContains<T>(array: Array<T>, value: T): boolean {
|
||||
return array.indexOf(value) !== -1;
|
||||
}
|
||||
|
||||
function createErrorFromErrorData(errorData: {message: string}): Error {
|
||||
var {
|
||||
message,
|
||||
|
|
|
@ -308,12 +308,9 @@ RCT_EXTERN NSArray *RCTGetModuleClasses(void);
|
|||
|
||||
- (NSString *)moduleConfig
|
||||
{
|
||||
NSMutableDictionary *config = [NSMutableDictionary new];
|
||||
NSMutableArray *config = [NSMutableArray new];
|
||||
for (RCTModuleData *moduleData in _moduleDataByID) {
|
||||
NSDictionary *moduleConfig = moduleData.config;
|
||||
if (moduleConfig) {
|
||||
config[moduleData.name] = moduleConfig;
|
||||
}
|
||||
[config addObject:moduleData.config];
|
||||
if ([moduleData.instance conformsToProtocol:@protocol(RCTFrameUpdateObserver)]) {
|
||||
[_frameUpdateObservers addObject:moduleData];
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
@property (nonatomic, strong, readonly) Class moduleClass;
|
||||
@property (nonatomic, copy, readonly) NSString *name;
|
||||
@property (nonatomic, copy, readonly) NSArray *methods;
|
||||
@property (nonatomic, copy, readonly) NSDictionary *config;
|
||||
@property (nonatomic, copy, readonly) NSArray *config;
|
||||
|
||||
@property (nonatomic, strong) dispatch_queue_t queue;
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#import "RCTBridge.h"
|
||||
#import "RCTModuleMethod.h"
|
||||
#import "RCTLog.h"
|
||||
#import "RCTUtils.h"
|
||||
|
||||
@implementation RCTModuleData
|
||||
{
|
||||
|
@ -79,37 +80,36 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
|
|||
return _methods;
|
||||
}
|
||||
|
||||
- (NSDictionary *)config
|
||||
- (NSArray *)config
|
||||
{
|
||||
if (_constants.count == 0 && self.methods.count == 0) {
|
||||
return nil; // Nothing to export
|
||||
return (id)kCFNull; // Nothing to export
|
||||
}
|
||||
|
||||
NSMutableDictionary *config = [NSMutableDictionary new];
|
||||
config[@"moduleID"] = _moduleID;
|
||||
|
||||
if (_constants) {
|
||||
config[@"constants"] = _constants;
|
||||
}
|
||||
|
||||
NSMutableDictionary *methodconfig = [NSMutableDictionary new];
|
||||
[self.methods enumerateObjectsUsingBlock:^(id<RCTBridgeMethod> method, NSUInteger idx, __unused BOOL *stop) {
|
||||
NSMutableArray *methods = self.methods.count ? [NSMutableArray new] : nil;
|
||||
NSMutableArray *asyncMethods = nil;
|
||||
for (id<RCTBridgeMethod> method in self.methods) {
|
||||
[methods addObject:method.JSMethodName];
|
||||
if (method.functionType == RCTFunctionTypePromise) {
|
||||
methodconfig[method.JSMethodName] = @{
|
||||
@"methodID": @(idx),
|
||||
@"type": @"remoteAsync",
|
||||
};
|
||||
} else {
|
||||
methodconfig[method.JSMethodName] = @{
|
||||
@"methodID": @(idx),
|
||||
};
|
||||
if (!asyncMethods) {
|
||||
asyncMethods = [NSMutableArray new];
|
||||
}
|
||||
[asyncMethods addObject:@(methods.count)];
|
||||
}
|
||||
}];
|
||||
if (methodconfig.count) {
|
||||
config[@"methods"] = [methodconfig copy];
|
||||
}
|
||||
|
||||
return [config copy];
|
||||
NSMutableArray *config = [NSMutableArray new];
|
||||
[config addObject:_name];
|
||||
if (_constants.count) {
|
||||
[config addObject:_constants];
|
||||
}
|
||||
if (methods) {
|
||||
[config addObject:methods];
|
||||
if (asyncMethods) {
|
||||
[config addObject:asyncMethods];
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
- (dispatch_queue_t)queue
|
||||
|
|
|
@ -31,7 +31,6 @@ typedef NS_ENUM(NSUInteger, RCTNullability) {
|
|||
|
||||
@property (nonatomic, readonly) Class moduleClass;
|
||||
@property (nonatomic, readonly) SEL selector;
|
||||
@property (nonatomic, readonly) RCTFunctionType functionType;
|
||||
|
||||
- (instancetype)initWithObjCMethodName:(NSString *)objCMethodName
|
||||
JSMethodName:(NSString *)JSMethodName
|
||||
|
|
|
@ -54,6 +54,7 @@ typedef BOOL (^RCTArgumentBlock)(RCTBridge *, NSUInteger, id);
|
|||
}
|
||||
|
||||
@synthesize JSMethodName = _JSMethodName;
|
||||
@synthesize functionType = _functionType;
|
||||
|
||||
static void RCTLogArgumentError(RCTModuleMethod *method, NSUInteger index,
|
||||
id valueOrType, const char *issue)
|
||||
|
|
Loading…
Reference in New Issue