Implement getConstants()
Summary: Instead of assigning all the constants exported by a NativeModule to the native module JavaScript object itself, we want to instead export a `getConstants()` method that can be used to access native module constants. This change simplifies the API of native modules. Eventually, we'll remove the ability to access constants as native module object properties alltogether, but that's comes later. **Note**: I didn't need to make any cpp changes because `JSIExecutor::NativeModuleProxy::get` calls `JSINativeModules::getModule` (here: https://goo.gl/QwPDWF), which eventually calls `JSINativeModules::createModule`, which uses `global.__fbGenNativeModule` (here: https://goo.gl/pSxMgE), which is just an alias to `genModule` in `NativeModules` (here: https://goo.gl/u2wjCs). Reviewed By: fkgozali Differential Revision: D13207152 fbshipit-source-id: 375aab1346232819187a5d5b272b33c55992346a
This commit is contained in:
parent
ada7089066
commit
db43aa8fc3
|
@ -60,8 +60,17 @@ function genModule(
|
|||
const methodType = isPromise ? 'promise' : isSync ? 'sync' : 'async';
|
||||
module[methodName] = genMethod(moduleID, methodID, methodType);
|
||||
});
|
||||
|
||||
Object.assign(module, constants);
|
||||
|
||||
if (module.getConstants == null) {
|
||||
module.getConstants = () => constants;
|
||||
} else {
|
||||
console.warn(
|
||||
`Unable to define method 'getConstants()' on NativeModule '${moduleName}'. NativeModule '${moduleName}' already has a constant or method called 'getConstants'. Please remove it.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (__DEV__) {
|
||||
BatchedBridge.createDebugLookup(moduleID, moduleName, methods);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue