2017-12-22 15:24:31 +00:00
|
|
|
/*
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
import { NativeModules } from 'react-native';
|
2018-01-03 20:00:38 +00:00
|
|
|
import { initialiseNativeModuleEventEmitter } from './events';
|
2017-12-22 15:24:31 +00:00
|
|
|
import INTERNALS from './internals';
|
|
|
|
|
|
|
|
import type ModuleBase from './ModuleBase';
|
2018-01-03 20:00:38 +00:00
|
|
|
import type { FirebaseModuleConfig } from '../types';
|
2017-12-22 15:24:31 +00:00
|
|
|
|
2018-01-05 18:23:38 +00:00
|
|
|
const NATIVE_MODULES: { [string]: Object } = {};
|
2017-12-22 15:24:31 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-21 23:37:05 +00:00
|
|
|
* Prepends all arguments in prependArgs to all native method calls
|
2017-12-22 15:24:31 +00:00
|
|
|
* @param NativeModule
|
2017-11-21 23:37:05 +00:00
|
|
|
* @param argToPrepend
|
2017-12-22 15:24:31 +00:00
|
|
|
*/
|
2017-11-21 23:37:05 +00:00
|
|
|
const nativeWithArgs = (
|
|
|
|
NativeModule: Object,
|
|
|
|
argToPrepend: Array<mixed>
|
|
|
|
): Object => {
|
2017-12-22 15:24:31 +00:00
|
|
|
const native = {};
|
|
|
|
const methods = Object.keys(NativeModule);
|
|
|
|
|
|
|
|
for (let i = 0, len = methods.length; i < len; i++) {
|
|
|
|
const method = methods[i];
|
2017-11-21 23:37:05 +00:00
|
|
|
native[method] = (...args) =>
|
|
|
|
NativeModule[method](...[...argToPrepend, ...args]);
|
2017-12-22 15:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return native;
|
|
|
|
};
|
|
|
|
|
2017-11-21 23:37:05 +00:00
|
|
|
const nativeModuleKey = (module: ModuleBase): string =>
|
|
|
|
`${module._serviceUrl || module.app.name}:${module.namespace}`;
|
2018-01-05 18:23:38 +00:00
|
|
|
|
2017-11-21 23:37:05 +00:00
|
|
|
export const getNativeModule = (module: ModuleBase): Object =>
|
|
|
|
NATIVE_MODULES[nativeModuleKey(module)];
|
2017-12-22 15:24:31 +00:00
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
export const initialiseNativeModule = (
|
|
|
|
module: ModuleBase,
|
2017-11-21 23:37:05 +00:00
|
|
|
config: FirebaseModuleConfig,
|
|
|
|
serviceUrl: ?string
|
2018-01-25 18:25:39 +00:00
|
|
|
): Object => {
|
2017-11-21 23:37:05 +00:00
|
|
|
const { moduleName, multiApp, hasShards, namespace } = config;
|
2018-01-03 20:00:38 +00:00
|
|
|
const nativeModule = NativeModules[moduleName];
|
2017-11-21 23:37:05 +00:00
|
|
|
const key = nativeModuleKey(module);
|
2017-12-22 15:24:31 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
if (!nativeModule && namespace !== 'utils') {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_MISSING_MODULE(namespace, moduleName)
|
|
|
|
);
|
2017-12-22 15:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// used by the modules that extend ModuleBase
|
|
|
|
// to access their native module counterpart
|
2017-11-21 23:37:05 +00:00
|
|
|
const argToPrepend = [];
|
2018-01-09 17:31:00 +00:00
|
|
|
if (multiApp) {
|
2017-11-21 23:37:05 +00:00
|
|
|
argToPrepend.push(module.app.name);
|
|
|
|
}
|
|
|
|
if (hasShards) {
|
|
|
|
argToPrepend.push(serviceUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argToPrepend.length) {
|
|
|
|
NATIVE_MODULES[key] = nativeWithArgs(nativeModule, argToPrepend);
|
2018-01-09 17:31:00 +00:00
|
|
|
} else {
|
|
|
|
NATIVE_MODULES[key] = nativeModule;
|
2017-12-22 15:24:31 +00:00
|
|
|
}
|
2018-01-03 20:00:38 +00:00
|
|
|
|
|
|
|
initialiseNativeModuleEventEmitter(module, config);
|
|
|
|
|
2018-01-05 18:23:38 +00:00
|
|
|
return NATIVE_MODULES[key];
|
2017-12-22 15:24:31 +00:00
|
|
|
};
|