2015-01-29 17:10:49 -08:00
|
|
|
/**
|
2015-03-23 15:07:33 -07:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-01-29 17:10:49 -08:00
|
|
|
*
|
|
|
|
* @providesModule NativeModules
|
2015-03-25 15:36:50 -07:00
|
|
|
* @flow
|
2015-01-29 17:10:49 -08:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-12-10 04:27:45 -08:00
|
|
|
const BatchedBridge = require('BatchedBridge');
|
|
|
|
const RemoteModules = BatchedBridge.RemoteModules;
|
2015-01-29 17:10:49 -08:00
|
|
|
|
2015-12-10 04:27:45 -08:00
|
|
|
function normalizePrefix(moduleName: string): string {
|
|
|
|
return moduleName.replace(/^(RCT|RK)/, '');
|
|
|
|
}
|
2015-03-17 13:42:44 -07:00
|
|
|
|
2015-12-10 04:27:45 -08:00
|
|
|
/**
|
|
|
|
* Dirty hack to support old (RK) and new (RCT) native module name conventions.
|
2016-09-08 03:59:32 -07:00
|
|
|
* TODO 10487027: kill this behaviour
|
2015-12-10 04:27:45 -08:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const NativeModules = {};
|
|
|
|
Object.keys(RemoteModules).forEach((moduleName) => {
|
|
|
|
Object.defineProperty(NativeModules, moduleName, {
|
2016-04-11 07:56:02 -07:00
|
|
|
configurable: true,
|
2015-12-10 13:47:32 -08:00
|
|
|
enumerable: true,
|
2015-12-10 04:27:45 -08:00
|
|
|
get: () => {
|
|
|
|
let module = RemoteModules[moduleName];
|
2015-12-10 07:09:08 -08:00
|
|
|
if (module && typeof module.moduleID === 'number' && global.nativeRequireModuleConfig) {
|
2016-09-08 03:59:32 -07:00
|
|
|
const config = global.nativeRequireModuleConfig(moduleName);
|
2015-12-10 04:27:45 -08:00
|
|
|
module = config && BatchedBridge.processModuleConfig(config, module.moduleID);
|
|
|
|
RemoteModules[moduleName] = module;
|
|
|
|
}
|
2016-05-16 03:55:58 -07:00
|
|
|
Object.defineProperty(NativeModules, moduleName, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: module,
|
|
|
|
});
|
2015-12-10 04:27:45 -08:00
|
|
|
return module;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-29 17:10:49 -08:00
|
|
|
module.exports = NativeModules;
|