You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.native.js 963B

12345678910111213141516171819202122232425262728293031323334353637
  1. // @flow
  2. import { NativeModules } from 'react-native';
  3. import { loadScript } from '../util';
  4. import logger from './logger';
  5. export * from './functions.any';
  6. const { JavaScriptSandbox } = NativeModules;
  7. /**
  8. * Loads config.js from a specific remote server.
  9. *
  10. * @param {string} url - The URL to load.
  11. * @returns {Promise<Object>}
  12. */
  13. export async function loadConfig(url: string): Promise<Object> {
  14. try {
  15. const configTxt = await loadScript(url, 2.5 * 1000 /* Timeout in ms */, true /* skipeval */);
  16. const configJson = await JavaScriptSandbox.evaluate(`${configTxt}\nJSON.stringify(config);`);
  17. const config = JSON.parse(configJson);
  18. if (typeof config !== 'object') {
  19. throw new Error('config is not an object');
  20. }
  21. logger.info(`Config loaded from ${url}`);
  22. return config;
  23. } catch (err) {
  24. logger.error(`Failed to load config from ${url}`, err);
  25. throw err;
  26. }
  27. }