<commit_before>import Ember from 'ember';

var ProgressEventTarget = Ember.Mixin.create(Ember.PromiseProxyMixin, {
  isAborted: false,
  isErrored: false,
  isLoaded: false,
  isLoadStarted: false,
  isTimedOut: false,
  isLoadEnded: false,


  deferred: Ember.computed(function() {
    var deferred = Ember.RSVP.defer();
    this.set('promise', deferred.promise);
    return deferred;
  }).readOnly(),

  target: Ember.required(),

  _setupProgressEventListeners: Ember.observer(function() {
    var object = this;
    var target = this.get('target');
    var deferred = this.get('deferred');
    target.onabort = function() {
      object.set('isAborted', true);
      deferred.reject('aborted');
    };
    target.onerror = function() {
      object.set('isErrored', true);
      deferred.reject('error');
    };
    target.onload = function() {
      object.set('isLoaded', true);
      deferred.resolve(target);
    };
    target.onloadstart = function() {
      object.set('loadStartedAt', new Date());
      object.set('isLoadStarted', true);
    };
    target.ontimeout = function() {
      object.set('isTimedOut', true);
      deferred.reject('timeout');
    };
    target.onloadend = function() {
      object.set('loadEndedAt', new Date());
      object.set('isLoadEnded', true);
    };
    target.onprogress = function(event) {
      object.set('progress', Progress.create({
        target: object,
        event: event,
        at: new Date()
      }));
    };
  }).on('init')
});

var Progress = Ember.Object.extend({
  target: null,
  event: null,
  lengthComputable: Ember.computed.readOnly('event.lengthComputable'),
  loaded: Ember.computed.readOnly('event.loaded'),
  total: Ember.computed.readOnly('event.total'),
  ratio: Ember.computed('lengthComputable', 'loaded', 'total', function() {
    if (this.get('lengthComputable')) {
      return this.get('loaded') / this.get('total');
    } else {
      return NaN;
    }
  }),
  percentage: Ember.computed('ratio', function() {
    var ratio = this.get('ratio');
    if (!!ratio) {
      return Math.round(ratio * 100);
    } else {
      return NaN;
    }
  }),
  averageSpeed: Ember.computed('loaded', 'totalTime', function() {
    var durationMillis = this.get('totalTime');
    var durationSeconds = Math.round(durationMillis / 1000);
    return this.get('loaded') / durationSeconds;
  }),
  totalTime: Ember.computed('target.loadStartedAt', 'at', function() {
    return this.get('at').getTime() - this.get('target.loadStartedAt').getTime();
  })
});

function delegateToXHR(method) {
  return function() {
    var target = this.get('target');
    return target[method].apply(target, arguments);
  };
}

var XHR = Ember.Object.extend(ProgressEventTarget, {
  target: Ember.computed(function() {
    return new XMLHttpRequest();
  }).readOnly(),

  open: delegateToXHR('open'),
  abort: delegateToXHR('abort'),
  send: function() {
    //materialize uploads
    this.get('upload');
    return delegateToXHR("send").apply(this, arguments);
  },
  setRequestHeader: delegateToXHR('setRequestHeader'),
  getResponseHeader: delegateToXHR('getResponseHeader'),
  overrideMimeType: delegateToXHR('overrideMimeType'),


  statusBinding: Ember.Binding.oneWay('state.status'),
  readyStateBinding: Ember.Binding.oneWay('state.readyState'),

  responseType: Ember.computed.alias('target.responseType'),
  responseBinding: Ember.Binding.oneWay('state.response'),
  responseTextBinding: Ember.Binding.oneWay('state.responseText'),
  responseXMLBinding: Ember.Binding.oneWay('state.responseXML'),

  timeout: Ember.computed.alias('target.timeout'),
  upload: Ember.computed(function() {
    return Upload.create({
      target: this.get('target.upload')
    });
  }).readOnly(),
  state: Ember.computed(function() {
    return UnsentState.create({
      request: this.get('request')
    });
  }),
  listenForReadyStateChanges: Ember.observer(function() {
    var object = this;
    var target = this.get('target');
    target.onreadystatechange = function() {
      var State = READY_STATES[target.readyState];
      object.set('state', State.create({request: target}));
    };
  }).on('init')
});


var Upload = Ember.Object.extend(ProgressEventTarget);

var ReadyState = Ember.Object.extend({
  status: Ember.computed.readOnly('request.status'),
  readyState: Ember.computed.readOnly('request.readyState'),
  response: Ember.computed.readOnly('request.response'),
  responseText: Ember.computed(function() {
    var responseType = this.get('request.responseType');
    if (Ember.isEmpty(responseType) || responseType == 'text') {
      return this.get('request.responseText');
    } else {
      return this.undefined;
    }
  }).readOnly(),
  responseXML: Ember.computed(function() {
    var responseType = this.get('request.responseType');
    if (Ember.isEmpty(responseType) || responseType == 'document') {
      return this.get('request.responseXML');
    } else {
      return this.undefined;
    }
  }).readOnly()
});

var UnsentState = ReadyState.extend({
  value: 0,
  name: 'UNSENT',
  description: 'open() has not been called yet.'
});

var OpenedState = ReadyState.extend({
  value: 1,
  name: 'OPENED',
  description: 'send() has not been called yet.'
});

var HeadersReceivedState = ReadyState.extend({
  value: 2,
  name: 'HEADERS_RECEIVED',
  description: 'send() has been called, and headers and status are available.'
});

var LoadingState = ReadyState.extend({
  value: 3,
  name: 'LOADING',
  description: 'Downloading; responseText holds partial data.'
});

var DoneState = ReadyState.extend({
  value: 4,
  name: 'DONE',
  description: 'The operation is complete.'
});

var READY_STATES = [UnsentState, OpenedState, HeadersReceivedState, LoadingState, DoneState];

Ember.XHR = XHR;

export default XHR;
<commit_msg>use computed properties where applicable<commit_after>var ProgressEventTarget = Ember.Mixin.create(Ember.PromiseProxyMixin, {
  isAborted: false,
  isErrored: false,
  isLoaded: false,
  isLoadStarted: false,
  isTimedOut: false,
  isLoadEnded: false,


  deferred: Ember.computed(function() {
    var deferred = Ember.RSVP.defer();
    this.set('promise', deferred.promise);
    return deferred;
  }).readOnly(),

  target: Ember.required(),

  _setupProgressEventListeners: Ember.observer(function() {
    var object = this;
    var target = this.get('target');
    var deferred = this.get('deferred');
    target.onabort = function() {
      object.set('isAborted', true);
      deferred.reject('aborted');
    };
    target.onerror = function() {
      object.set('isErrored', true);
      deferred.reject('error');
    };
    target.onload = function() {
      object.set('isLoaded', true);
      deferred.resolve(target);
    };
    target.onloadstart = function() {
      object.set('loadStartedAt', new Date());
      object.set('isLoadStarted', true);
    };
    target.ontimeout = function() {
      object.set('isTimedOut', true);
      deferred.reject('timeout');
    };
    target.onloadend = function() {
      object.set('loadEndedAt', new Date());
      object.set('isLoadEnded', true);
    };
    target.onprogress = function(event) {
      object.set('progress', Progress.create({
        target: object,
        event: event,
        at: new Date()
      }));
    };
  }).on('init')
});

var Progress = Ember.Object.extend({
  target: null,
  event: null,
  lengthComputable: Ember.computed.readOnly('event.lengthComputable'),
  loaded: Ember.computed.readOnly('event.loaded'),
  total: Ember.computed.readOnly('event.total'),
  ratio: Ember.computed('lengthComputable', 'loaded', 'total', function() {
    if (this.get('lengthComputable')) {
      return this.get('loaded') / this.get('total');
    } else {
      return NaN;
    }
  }),
  percentage: Ember.computed('ratio', function() {
    var ratio = this.get('ratio');
    if (!!ratio) {
      return Math.round(ratio * 100);
    } else {
      return NaN;
    }
  }),
  averageSpeed: Ember.computed('loaded', 'totalTime', function() {
    var durationMillis = this.get('totalTime');
    var durationSeconds = Math.round(durationMillis / 1000);
    return this.get('loaded') / durationSeconds;
  }),
  totalTime: Ember.computed('target.loadStartedAt', 'at', function() {
    return this.get('at').getTime() - this.get('target.loadStartedAt').getTime();
  })
});

function delegateToXHR(method) {
  return function() {
    var target = this.get('target');
    return target[method].apply(target, arguments);
  };
}

var XHR = Ember.Object.extend(ProgressEventTarget, {
  target: Ember.computed(function() {
    return new XMLHttpRequest();
  }).readOnly(),

  open: delegateToXHR('open'),
  abort: delegateToXHR('abort'),
  send: function() {
    //materialize uploads
    this.get('upload');
    return delegateToXHR("send").apply(this, arguments);
  },
  setRequestHeader: delegateToXHR('setRequestHeader'),
  getResponseHeader: delegateToXHR('getResponseHeader'),
  overrideMimeType: delegateToXHR('overrideMimeType'),


  status: Ember.computed.readOnly('state.status'),
  readyState: Ember.computed.readOnly('state.readyState'),

  responseType: Ember.computed.alias('target.responseType'),
  responseBinding: Ember.Binding.oneWay('state.response'),
  responseTextBinding: Ember.Binding.oneWay('state.responseText'),
  responseXMLBinding: Ember.Binding.oneWay('state.responseXML'),

  timeout: Ember.computed.alias('target.timeout'),
  upload: Ember.computed(function() {
    return Upload.create({
      target: this.get('target.upload')
    });
  }).readOnly(),
  state: Ember.computed(function() {
    return UnsentState.create({
      request: this.get('request')
    });
  }),
  listenForReadyStateChanges: Ember.observer(function() {
    var object = this;
    var target = this.get('target');
    target.onreadystatechange = function() {
      var State = READY_STATES[target.readyState];
      object.set('state', State.create({request: target}));
    };
  }).on('init')
});


var Upload = Ember.Object.extend(ProgressEventTarget);

var ReadyState = Ember.Object.extend({
  status: Ember.computed.readOnly('request.status'),
  readyState: Ember.computed.readOnly('request.readyState'),
  response: Ember.computed.readOnly('request.response'),
  responseText: Ember.computed(function() {
    var responseType = this.get('request.responseType');
    if (Ember.isEmpty(responseType) || responseType === 'text') {
      return this.get('request.responseText');
    } else {
      return this.undefined;
    }
  }).readOnly(),
  responseXML: Ember.computed(function() {
    var responseType = this.get('request.responseType');
    if (Ember.isEmpty(responseType) || responseType === 'document') {
      return this.get('request.responseXML');
    } else {
      return this.undefined;
    }
  }).readOnly()
});

var UnsentState = ReadyState.extend({
  value: 0,
  name: 'UNSENT',
  description: 'open() has not been called yet.'
});

var OpenedState = ReadyState.extend({
  value: 1,
  name: 'OPENED',
  description: 'send() has not been called yet.'
});

var HeadersReceivedState = ReadyState.extend({
  value: 2,
  name: 'HEADERS_RECEIVED',
  description: 'send() has been called, and headers and status are available.'
});

var LoadingState = ReadyState.extend({
  value: 3,
  name: 'LOADING',
  description: 'Downloading; responseText holds partial data.'
});

var DoneState = ReadyState.extend({
  value: 4,
  name: 'DONE',
  description: 'The operation is complete.'
});

var READY_STATES = [UnsentState, OpenedState, HeadersReceivedState, LoadingState, DoneState];

Ember.XHR = XHR;

export default XHR;

{'fraction_non_alphanumeric': 0.15707754679548003, 'fraction_numerical': 0.0020702147847839214, 'mean_word_length': 3.929421768707483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4179779', 'n_tokens_mistral': 3708, 'n_tokens_neox': 3629, 'n_words': 710}