From 31b158c9feb2e6bdb956ece00f5e6d39f16162ae Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 23 Sep 2016 11:12:54 -0700 Subject: [PATCH] Export native modules without RCT or RK prefix Reviewed By: mmmulani Differential Revision: D3901600 fbshipit-source-id: 7d4a027f0f2478e2a9ac9916326b91279bec3cb3 --- .../BatchedBridgedModules/NativeModules.js | 21 ------------------- Libraries/Utilities/UIManager.js | 11 +++------- React/Base/RCTBatchedBridge.m | 6 +----- React/Base/RCTBridge.m | 6 +++++- React/Executors/RCTJSCExecutor.mm | 2 +- React/Views/RCTComponentData.m | 21 +++++++++++++++---- 6 files changed, 27 insertions(+), 40 deletions(-) diff --git a/Libraries/BatchedBridge/BatchedBridgedModules/NativeModules.js b/Libraries/BatchedBridge/BatchedBridgedModules/NativeModules.js index 62b9156d0d..380376d35f 100644 --- a/Libraries/BatchedBridge/BatchedBridgedModules/NativeModules.js +++ b/Libraries/BatchedBridge/BatchedBridgedModules/NativeModules.js @@ -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. diff --git a/Libraries/Utilities/UIManager.js b/Libraries/Utilities/UIManager.js index 11bb236ea5..b8879da4dc 100644 --- a/Libraries/Utilities/UIManager.js +++ b/Libraries/Utilities/UIManager.js @@ -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]; diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 443c578330..a407c693f6 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -235,9 +235,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(id)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)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 diff --git a/React/Base/RCTBridge.m b/React/Base/RCTBridge.m index 35c4fb153f..e89cbf6c43 100644 --- a/React/Base/RCTBridge.m +++ b/React/Base/RCTBridge.m @@ -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; } diff --git a/React/Executors/RCTJSCExecutor.mm b/React/Executors/RCTJSCExecutor.mm index 09f83a1176..d4f8cdd39c 100644 --- a/React/Executors/RCTJSCExecutor.mm +++ b/React/Executors/RCTJSCExecutor.mm @@ -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 *calls){ diff --git a/React/Views/RCTComponentData.m b/React/Views/RCTComponentData.m index 1120661ab9..9d1881505a 100644 --- a/React/Views/RCTComponentData.m +++ b/React/Views/RCTComponentData.m @@ -16,6 +16,7 @@ #import "RCTShadowView.h" #import "RCTUtils.h" #import "UIView+React.h" +#import "RCTBridgeModule.h" typedef void (^RCTPropBlock)(id view, id json); @@ -58,11 +59,23 @@ typedef void (^RCTPropBlock)(id 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;