Export native modules without RCT or RK prefix

Reviewed By: mmmulani

Differential Revision: D3901600

fbshipit-source-id: 7d4a027f0f2478e2a9ac9916326b91279bec3cb3
This commit is contained in:
Pieter De Baets 2016-09-23 11:12:54 -07:00 коммит произвёл Facebook Github Bot 8
Родитель ff79224d37
Коммит 31b158c9fe
6 изменённых файлов: 27 добавлений и 40 удалений

Просмотреть файл

@ -14,27 +14,6 @@
const BatchedBridge = require('BatchedBridge');
const RemoteModules = BatchedBridge.RemoteModules;
function normalizePrefix(moduleName: string): string {
return moduleName.replace(/^(RCT|RK)/, '');
}
/**
* Dirty hack to support old (RK) and new (RCT) native module name conventions.
* TODO 10487027: kill this behaviour
*/
Object.keys(RemoteModules).forEach((moduleName) => {
const strippedName = normalizePrefix(moduleName);
if (RemoteModules['RCT' + strippedName] && RemoteModules['RK' + strippedName]) {
throw new Error(
'Module cannot be registered as both RCT and RK: ' + moduleName
);
}
if (strippedName !== moduleName) {
RemoteModules[strippedName] = RemoteModules[moduleName];
delete RemoteModules[moduleName];
}
});
/**
* Define lazy getters for each module.
* These will return the module if already loaded, or load it if not.

Просмотреть файл

@ -11,8 +11,8 @@
*/
'use strict';
const Platform = require('Platform');
const NativeModules = require('NativeModules');
const Platform = require('Platform');
const { UIManager } = NativeModules;
const findNodeHandle = require('react/lib/findNodeHandle');
@ -64,11 +64,6 @@ UIManager.takeSnapshot = async function(
* namespace instead of UIManager, unlike Android.
*/
if (Platform.OS === 'ios') {
// Copied from NativeModules
function normalizePrefix(moduleName: string): string {
return moduleName.replace(/^(RCT|RK)/, '');
}
Object.keys(UIManager).forEach(viewName => {
const viewConfig = UIManager[viewName];
if (viewConfig.Manager) {
@ -82,7 +77,7 @@ if (Platform.OS === 'ios') {
return constants;
}
constants = {};
const viewManager = NativeModules[normalizePrefix(viewConfig.Manager)];
const viewManager = NativeModules[viewConfig.Manager];
viewManager && Object.keys(viewManager).forEach(key => {
const value = viewManager[key];
if (typeof value !== 'function') {
@ -102,7 +97,7 @@ if (Platform.OS === 'ios') {
return commands;
}
commands = {};
const viewManager = NativeModules[normalizePrefix(viewConfig.Manager)];
const viewManager = NativeModules[viewConfig.Manager];
let index = 0;
viewManager && Object.keys(viewManager).forEach(key => {
const value = viewManager[key];

Просмотреть файл

@ -235,9 +235,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)dele
- (NSArray *)configForModuleName:(NSString *)moduleName
{
RCTModuleData *moduleData = _moduleDataByName[moduleName];
if (!moduleData) {
moduleData = _moduleDataByName[[@"RCT" stringByAppendingString:moduleName]];
}
if (moduleData) {
#if RCT_DEV
if ([self.delegate respondsToSelector:@selector(whitelistedModulesForBridge:)]) {
@ -246,9 +243,8 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)dele
@"Required config for %@, which was not whitelisted", moduleName);
}
#endif
return moduleData.config;
}
return (id)kCFNull;
return moduleData.config;
}
- (void)initModulesWithDispatchGroup:(dispatch_group_t)dispatchGroup

Просмотреть файл

@ -67,9 +67,13 @@ NSString *RCTBridgeModuleNameForClass(Class cls)
if (name.length == 0) {
name = NSStringFromClass(cls);
}
if ([name hasPrefix:@"RK"]) {
name = [name stringByReplacingCharactersInRange:(NSRange){0,@"RK".length} withString:@"RCT"];
name = [name substringFromIndex:2];
} else if ([name hasPrefix:@"RCT"]) {
name = [name substringFromIndex:3];
}
return name;
}

Просмотреть файл

@ -357,7 +357,7 @@ static NSThread *newJavaScriptThread(void)
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"nativeRequireModuleConfig", @{ @"moduleName": moduleName });
NSArray *result = [strongSelf->_bridge configForModuleName:moduleName];
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"js_call,config");
return result;
return RCTNullIfNil(result);
};
context[@"nativeFlushQueueImmediate"] = ^(NSArray<NSArray *> *calls){

Просмотреть файл

@ -16,6 +16,7 @@
#import "RCTShadowView.h"
#import "RCTUtils.h"
#import "UIView+React.h"
#import "RCTBridgeModule.h"
typedef void (^RCTPropBlock)(id<RCTComponent> view, id json);
@ -58,11 +59,23 @@ typedef void (^RCTPropBlock)(id<RCTComponent> view, id json);
_viewPropBlocks = [NSMutableDictionary new];
_shadowPropBlocks = [NSMutableDictionary new];
_name = RCTBridgeModuleNameForClass(_managerClass);
RCTAssert(_name.length, @"Invalid moduleName '%@'", _name);
if ([_name hasSuffix:@"Manager"]) {
_name = [_name substringToIndex:_name.length - @"Manager".length];
// Hackety hack, this partially re-implements RCTBridgeModuleNameForClass
// We want to get rid of RCT and RK prefixes, but a lot of JS code still references
// view names by prefix. So, while RCTBridgeModuleNameForClass now drops these
// prefixes by default, we'll still keep them around here.
NSString *name = [managerClass moduleName];
if (name.length == 0) {
name = NSStringFromClass(managerClass);
}
if ([name hasPrefix:@"RK"]) {
name = [name stringByReplacingCharactersInRange:(NSRange){0, @"RK".length} withString:@"RCT"];
}
if ([name hasSuffix:@"Manager"]) {
name = [name substringToIndex:name.length - @"Manager".length];
}
RCTAssert(name.length, @"Invalid moduleName '%@'", name);
_name = name;
_implementsUIBlockToAmendWithShadowViewRegistry = NO;
Class cls = _managerClass;