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.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import React from 'react';
  2. import { loadScript } from '../../base/util';
  3. /**
  4. * Loads config.js file from remote server.
  5. *
  6. * @param {string} host - Host where config.js is hosted.
  7. * @param {string} path='/config.js' - Relative pah to config.js file.
  8. * @returns {Promise<Object>}
  9. */
  10. export function loadConfig(host, path = '/config.js') {
  11. if (!React.View) {
  12. // Returns config.js file from global scope. We can't use the version
  13. // that's being used for the React Native app because the old/current
  14. // Web app uses config from the global scope.
  15. return Promise.resolve(window.config);
  16. }
  17. return loadScript(new URL(path, host).toString())
  18. .then(() => {
  19. const config = window.config;
  20. // We don't want to pollute global scope.
  21. window.config = undefined;
  22. if (typeof config !== 'object') {
  23. throw new Error('window.config is not an object');
  24. }
  25. return config;
  26. })
  27. .catch(err => {
  28. console.error(`Failed to load ${path} from ${host}`, err);
  29. throw err;
  30. });
  31. }