{"size": 1721, "ext": "js", "max_stars_repo_path": "packages/mccree-loader-moz-xhr/src/index.js", "max_stars_repo_name": "xiongmaotv/open-mccree", "max_stars_repo_stars_event_min_datetime": "2017-11-08T05:52:45.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-11T03:25:28.000Z", "max_issues_repo_path": "packages/mccree-loader-moz-xhr/src/index.js", "max_issues_repo_name": "xiongmaotv/open-mccree", "max_issues_repo_issues_event_min_datetime": "2017-11-14T14:32:09.000Z", "max_issues_repo_issues_event_max_datetime": "2018-09-28T08:19:28.000Z", "max_forks_repo_path": "packages/mccree-loader-moz-xhr/src/index.js", "max_forks_repo_name": "xiongmaotv/open-mccree", "max_forks_repo_forks_event_min_datetime": "2017-11-09T08:56:37.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-11T08:12:41.000Z"}
{"max_stars_count": 53, "max_issues_count": 2, "max_forks_count": 23, "avg_line_length": 24.5857142857, "max_line_length": 71, "alphanum_fraction": 0.6170830912}
import LoaderController from 'mccree-controller-loader';

class MozXhrLoader {
  static isSupported() {}

  constructor(config) {
    this.TAG = 'Mccree-loader-moz-xhr';
    this.type = 'loader';
    this.config = config || {};
  }

  init(mccree) {
    this.controller = new LoaderController(this);
    this.controller.init.call(this, mccree);
  }

  load(source, opt) {
    this._cleanLoaderBuffer();
    this.loadPartail(source, {
      start: -1,
      end: -1
    }, opt);
  }

  loadPartail(source, range, opts) {
    if (!this.mccree) {
      this.logger.warn(this.TAG, 'Live is not init yet');
      return;
    }

    this.source = source;
    this._loading = false;

    this.xhr = new XMLHttpRequest();
    let that = this;
    this.xhr.open("get", source, true);
    this.xhr.responseType = 'moz-chunked-arraybuffer';
    this.xhr.onreadystatechange = e => {
      if(this.status === 200) {
        that.controller.onConnected.call(that, e);
      } else if(this.status === 404){
        that.controller.onNotfound.call(that, e);
      }
    };
    this.xhr.onprogress = e => {
      that.mccree.url = this.xhr.response.url || that.mccree.url;
      let chunk = e.target.response;
      that.mccree.loaderBuffer.push(new Uint8Array(chunk));
      that.observer.trigger(that.events.FRAG_LOADED, chunk.byteLength);
    };
    this.xhr.send();
  }

  unload() {
    let that = this;
    return new Promise((resolve, reject) => {
      that._loading = false;
      that._cleanLoaderBuffer();
      this.xhr.onprogress = null;
      this.xhr.abort();
      resolve();
    });
  }

  _cleanLoaderBuffer() {
    this.mccree.loaderBuffer.clear();
  }
}
window.MozXhrLoader = MozXhrLoader;
export default MozXhrLoader;