<commit_before>/** Imports **/
import HelpersProgressBar from '../helpers/helpers.progressbar';


/**
 *
 * It is typically used to load a DICOM image. Use loading manager for
 * advanced usage, such as multiple files handling.
 *
 * Demo: {@link https://fnndsc.github.io/vjs#loader_dicom}
 *
 * @module loaders/base
 *
 * @example
 * var files = ['/data/dcm/fruit'];
 *
 * // Instantiate a dicom loader
 * var lDicomoader = new dicom();
 *
 * // load a resource
 * loader.load(
 *   // resource URL
 *   files[0],
 *   // Function when resource is loaded
 *   function(object) {
 *     //scene.add( object );
 *     window.console.log(object);
 *   }
 * );
 */
export default class LoadersBase {
  /**
   * Create a Loader.
   * @param {dom} container - The dom container of loader.
   * @param {object} ProgressBar - The progressbar of loader.
   */
  constructor(container = null, ProgressBar = HelpersProgressBar) {
    this._loaded = -1;
    this._totalLoaded = -1;
    this._parsed = -1;
    this._totalParsed = -1;

    this._data = [];

    this._container = container;
    this._progressBar = null;
    if (this._container && ProgressBar) {
      this._progressBar = new ProgressBar(this._container);
    }
  }

  /**
   * free the reference.
   */
  free() {
    this._container = null;
    this._helpersProgressBar = null;

    if (this._progressBar) {
      this._progressBar.free();
      this._progressBar = null;
    }
  }

  /**
   * load the resource by url.
   * @param {string} url - resource url.
   * @return {promise} promise.
   */
  fetch(url) {
    return new Promise((resolve, reject) => {
      let request = new XMLHttpRequest();
      request.open('GET', url);
      request.crossOrigin = true;
      request.responseType = 'arraybuffer';

      request.onload = (event) => {
        if (request.status === 200) {
          this._loaded = event.loaded;
          this._totalLoaded = event.total;
          if (this._progressBar) {
            this._progressBar.update(this._loaded, this._totalLoaded, 'load');
          }

          let buffer = request.response;
          let response = {
            url,
            buffer,
          };

          resolve(response);
        } else {
          reject(request.statusText);
        }
      };

      request.onerror = () => {
        reject(request.statusText);
      };

      request.onprogress = (event) => {
        this._loaded = event.loaded;
        this._totalLoaded = event.total;
        if (this._progressBar) {
          this._progressBar.update(this._loaded, this._totalLoaded, 'load');
        }
      };

      request.send();
    });
  }

  /**
   * parse the data loaded
   * SHOULD BE implementd by detail loader.
   * @param {object} response - loaded data.
   * @return {promise} promise.
   */
  parse(response) {
    return new Promise((resolve, reject) => {
      resolve(null);
    });
  }

  /**
   * default load sequence promise.
   * @param {string} url - resource url.
   * @return {promise} promise.
   */
  loadSequence(url) {
    return this.fetch(url)
      .then((rawdata) => {
        return this.parse(rawdata);
      })
      .then((data) => {
        this._data.push(data);
      })
      .catch(function(error) {
        window.console.log('oops... something went wrong...');
        window.console.log(error);
      });
  }

  /**
   * load the data by url(urls)
   * @param {string|array} url - resource url.
   * @return {promise} promise
   */
  load(url) {
    // if we load a single file, convert it to an array
    if (!Array.isArray(url)) {
      url = [url];
    }

    let loadSequences = [];
    url.forEach((file) => {
      loadSequences.push(
        this.loadSequence(file)
      );
    });

    return Promise.all(loadSequences);
  }

  /**
   * Set data<commit_msg>improve loader promise<commit_after>/** Imports **/
import HelpersProgressBar from '../helpers/helpers.progressbar';


/**
 *
 * It is typically used to load a DICOM image. Use loading manager for
 * advanced usage, such as multiple files handling.
 *
 * Demo: {@link https://fnndsc.github.io/vjs#loader_dicom}
 *
 * @module loaders/base
 *
 * @example
 * var files = ['/data/dcm/fruit'];
 *
 * // Instantiate a dicom loader
 * var lDicomoader = new dicom();
 *
 * // load a resource
 * loader.load(
 *   // resource URL
 *   files[0],
 *   // Function when resource is loaded
 *   function(object) {
 *     //scene.add( object );
 *     window.console.log(object);
 *   }
 * );
 */
export default class LoadersBase {
  /**
   * Create a Loader.
   * @param {dom} container - The dom container of loader.
   * @param {object} ProgressBar - The progressbar of loader.
   */
  constructor(container = null, ProgressBar = HelpersProgressBar) {
    this._loaded = -1;
    this._totalLoaded = -1;
    this._parsed = -1;
    this._totalParsed = -1;

    this._data = [];

    this._container = container;
    this._progressBar = null;
    if (this._container && ProgressBar) {
      this._progressBar = new ProgressBar(this._container);
    }
  }

  /**
   * free the reference.
   */
  free() {
    this._container = null;
    this._helpersProgressBar = null;

    if (this._progressBar) {
      this._progressBar.free();
      this._progressBar = null;
    }
  }

  /**
   * load the resource by url.
   * @param {string} url - resource url.
   * @return {promise} promise.
   */
  fetch(url) {
    return new Promise((resolve, reject) => {
      let request = new XMLHttpRequest();
      request.open('GET', url);
      request.crossOrigin = true;
      request.responseType = 'arraybuffer';

      request.onload = (event) => {
        if (request.status === 200) {
          this._loaded = event.loaded;
          this._totalLoaded = event.total;
          if (this._progressBar) {
            this._progressBar.update(this._loaded, this._totalLoaded, 'load');
          }

          let buffer = request.response;
          let response = {
            url,
            buffer,
          };

          resolve(response);
        } else {
          reject(request.statusText);
        }
      };

      request.onerror = () => {
        reject(request.statusText);
      };

      request.onprogress = (event) => {
        this._loaded = event.loaded;
        this._totalLoaded = event.total;
        if (this._progressBar) {
          this._progressBar.update(this._loaded, this._totalLoaded, 'load');
        }
      };

      request.send();
    });
  }

  /**
   * parse the data loaded
   * SHOULD BE implementd by detail loader.
   * @param {object} response - loaded data.
   * @return {promise} promise.
   */
  parse(response) {
    return new Promise((resolve, reject) => {
      resolve(response);
    });
  }

  /**
   * default load sequence promise.
   * @param {string} url - resource url.
   * @return {promise} promise.
   */
  loadSequence(url) {
    return this.fetch(url)
      .then((rawdata) => {
        return this.parse(rawdata);
      })
      .then((data) => {
        this._data.push(data);
        return data;
      })
      .catch(function(error) {
        window.console.log('oops... something went wrong...');
        window.console.log(error);
      });
  }

  /**
   * load the data by url(urls)
   * @param {string|array} url - resource url.
   * @return {promise} promise
   */
  load(url) {
    // if we load a single file, convert it to an array
    if (!Array.isArray(url)) {
      url = [url];
    }

    let loadSequences = [];
    url.forEach((file) => {
      loadSequences.push(
        this.loadSequence(file)
      );
    });

    return Promise.all(loadSequences);
  }

  /**
   * Set data
{'fraction_non_alphanumeric': 0.14257659073055773, 'fraction_numerical': 0.002094789211835559, 'mean_word_length': 2.0914609469850265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6059655', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2337, 'n_words': 545}