Skip to content

Commit

Permalink
Export native modules without RCT or RK prefix
Browse files Browse the repository at this point in the history
Reviewed By: mmmulani

Differential Revision: D3901600

fbshipit-source-id: 7d4a027f0f2478e2a9ac9916326b91279bec3cb3
  • Loading branch information
javache authored and Facebook Github Bot 8 committed Sep 23, 2016
1 parent ff79224 commit 31b158c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 40 deletions.
21 changes: 0 additions & 21 deletions Libraries/BatchedBridge/BatchedBridgedModules/NativeModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 3 additions & 8 deletions Libraries/Utilities/UIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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') {
Expand All @@ -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];
Expand Down
6 changes: 1 addition & 5 deletions React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ - (BOOL)moduleIsInitialized:(Class)moduleClass
- (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:)]) {
Expand All @@ -246,9 +243,8 @@ - (NSArray *)configForModuleName:(NSString *)moduleName
@"Required config for %@, which was not whitelisted", moduleName);
}
#endif
return moduleData.config;
}
return (id)kCFNull;
return moduleData.config;
}

- (void)initModulesWithDispatchGroup:(dispatch_group_t)dispatchGroup
Expand Down
6 changes: 5 additions & 1 deletion React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ void RCTRegisterModule(Class moduleClass)
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;
}

Expand Down
2 changes: 1 addition & 1 deletion React/Executors/RCTJSCExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ - (void)setUp
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){
Expand Down
21 changes: 17 additions & 4 deletions React/Views/RCTComponentData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -58,11 +59,23 @@ - (instancetype)initWithManagerClass:(Class)managerClass
_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;
Expand Down

0 comments on commit 31b158c

Please sign in to comment.