modules.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
  2. * Apache-2.0 */
  3. import { getScript, isLoaded, loadScript } from './script';
  4. import utils from './utils/index';
  5. // wrap Dojo's require() in a promise
  6. function requireModules(modules) {
  7. return new utils.Promise(function (resolve, reject) {
  8. // If something goes wrong loading the esri/dojo scripts, reject with the error.
  9. var errorHandler = window['require'].on('error', reject);
  10. window['require'](modules, function () {
  11. var args = [];
  12. for (var _i = 0; _i < arguments.length; _i++) {
  13. args[_i] = arguments[_i];
  14. }
  15. // remove error handler
  16. errorHandler.remove();
  17. // Resolve with the parameters from dojo require as an array.
  18. resolve(args);
  19. });
  20. });
  21. }
  22. // returns a promise that resolves with an array of the required modules
  23. // also will attempt to lazy load the ArcGIS API if it has not already been loaded
  24. export function loadModules(modules, loadScriptOptions) {
  25. if (loadScriptOptions === void 0) { loadScriptOptions = {}; }
  26. if (!isLoaded()) {
  27. // script is not yet loaded, is it in the process of loading?
  28. var script = getScript();
  29. var src = script && script.getAttribute('src');
  30. if (!loadScriptOptions.url && src) {
  31. // script is still loading and user did not specify a URL
  32. // in this case we want to default to the URL that's being loaded
  33. // instead of defaulting to the latest 4.x URL
  34. loadScriptOptions.url = src;
  35. }
  36. // attempt to load the script then load the modules
  37. return loadScript(loadScriptOptions).then(function () { return requireModules(modules); });
  38. }
  39. else {
  40. // script is already loaded, just load the modules
  41. return requireModules(modules);
  42. }
  43. }