<commit_before>/* global XMLHttpRequest JSON */
import {getMajorVersion} from './utils.js';

const registryBaseUrl = 'https://aframe.io/aframe-registry/build/';

/**
 * Asynchronously load and register components from the registry.
 */
function ComponentLoader () {
  this.components = null;
  this.loadFromRegistry();
}

ComponentLoader.prototype = {
  /**
   * XHR the registry JSON.
   */
  loadFromRegistry: function () {
    var xhr = new XMLHttpRequest();

    // @todo Remove the sync call and use a callback
    xhr.open('GET', registryBaseUrl + getMajorVersion(AFRAME.version) + '.json');

    xhr.onload = () => {
      this.components = JSON.parse(xhr.responseText).components;
      console.info('Components in registry:', Object.keys(this.components).length);
    };
    xhr.onerror = () => { console.error('Error loading registry file.'); };
    xhr.send();
  },

  /**
   * Inject component script. If already injected, then resolve.
   *
   * @returns {Promise}
   */
  addComponentToScene: function (packageName, componentName) {
    const component = this.components[packageName];
    if (component.included) { return Promise.resolve(componentName); }

    let script = document.createElement('script');
    script.src = component.file;
    script.setAttribute('data-component-description', component.description);
    script.setAttribute('data-component-names', component.names);
    script.setAttribute('data-component-url', component.npmUrl);
    script.setAttribute('data-package-name', packageName);

    return new Promise(resolve => {
      script.addEventListener('load', () => {
        script.onreadystatechange = script.onload = null;
        resolve(componentName);
      });
      (document.head || document.body).appendChild(script);
      component.included = true;
    });
  }
};

module.exports = ComponentLoader;
<commit_msg>Fixes #391 - Inspector panel doesnt works if registry build fails<commit_after>/* global XMLHttpRequest JSON */
import {getMajorVersion} from './utils.js';

const registryBaseUrl = 'https://aframe.io/aframe-registry/build/';

/**
 * Asynchronously load and register components from the registry.
 */
function ComponentLoader () {
  this.components = [];
  this.loadFromRegistry();
}

ComponentLoader.prototype = {
  /**
   * XHR the registry JSON.
   */
  loadFromRegistry: function () {
    var xhr = new XMLHttpRequest();

    // @todo Remove the sync call and use a callback
    xhr.open('GET', registryBaseUrl + getMajorVersion(AFRAME.version) + '.json');

    xhr.onload = () => {
      if (xhr.status === 404) {
        console.error('Error loading registry file: 404');
      } else {
        this.components = JSON.parse(xhr.responseText).components;
        console.info('Components in registry:', Object.keys(this.components).length);
      }
    };
    xhr.onerror = () => { console.error('Error loading registry file.'); };
    xhr.send();
  },

  /**
   * Inject component script. If already injected, then resolve.
   *
   * @returns {Promise}
   */
  addComponentToScene: function (packageName, componentName) {
    const component = this.components[packageName];
    if (component.included) { return Promise.resolve(componentName); }

    let script = document.createElement('script');
    script.src = component.file;
    script.setAttribute('data-component-description', component.description);
    script.setAttribute('data-component-names', component.names);
    script.setAttribute('data-component-url', component.npmUrl);
    script.setAttribute('data-package-name', packageName);

    return new Promise(resolve => {
      script.addEventListener('load', () => {
        script.onreadystatechange = script.onload = null;
        resolve(componentName);
      });
      (document.head || document.body).appendChild(script);
      component.included = true;
    });
  }
};

module.exports = ComponentLoader;

{'fraction_non_alphanumeric': 0.14061696658097686, 'fraction_numerical': 0.002313624678663239, 'mean_word_length': 3.5034722222222223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '785023', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1074, 'n_words': 235}