2017-08-22 17:22:40 -07:00
|
|
|
import {
|
|
|
|
|
NativeModules,
|
|
|
|
|
findNodeHandle,
|
|
|
|
|
Platform,
|
|
|
|
|
} from 'react-native';
|
|
|
|
|
|
|
|
|
|
export const IS_ANDROID = Platform.OS === 'android';
|
|
|
|
|
|
2017-08-24 13:17:22 -07:00
|
|
|
export function runNativeCommand (module, name, nativeRef, args = []) {
|
2017-08-22 17:22:40 -07:00
|
|
|
// android native command
|
2017-08-24 13:17:22 -07:00
|
|
|
const managerInstance = getManagerInstance(module);
|
2017-08-22 17:22:40 -07:00
|
|
|
if (!managerInstance) {
|
2017-08-24 13:17:22 -07:00
|
|
|
throw new Error(`Could not find ${module}`);
|
2017-08-22 17:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get react tag so we can find, this component on the otherside
|
|
|
|
|
const handle = findNodeHandle(nativeRef);
|
|
|
|
|
|
|
|
|
|
// android native command
|
|
|
|
|
if (IS_ANDROID) {
|
|
|
|
|
NativeModules.UIManager.dispatchViewManagerCommand(
|
|
|
|
|
handle,
|
|
|
|
|
managerInstance.Commands[name],
|
|
|
|
|
args,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ios native command
|
2017-08-24 13:17:22 -07:00
|
|
|
const method = managerInstance[name];
|
2017-08-22 17:22:40 -07:00
|
|
|
if (!method) {
|
2017-08-24 13:17:22 -07:00
|
|
|
throw new Error(`Could not find method ${name} on module ${module}`);
|
2017-08-22 17:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
method(handle, ...args);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 13:17:22 -07:00
|
|
|
function getManagerInstance (module) {
|
2017-08-22 17:22:40 -07:00
|
|
|
const obj = IS_ANDROID ? NativeModules.UIManager : NativeModules;
|
|
|
|
|
return obj[module];
|
|
|
|
|
}
|