<commit_before>import { or } from '@ember/object/computed';
import { computed, get } from '@ember/object';
import Mixin from '@ember/object/mixin';
import isEqual from 'lodash/isEqual';

export default Mixin.create({
  init() {
    this._super();
    this._relationshipTracker = Object.create(null);
  },

  watchRelationship(field, fn) {
    let entry = this._relationshipTracker[field];
    if (!entry) {
      entry = this._relationshipTracker[field] = Object.create(null);
    }
    let changed = changedKey(this);
    if (!(changed in entry)) {
      entry[changed] = currentState(this, field);
    }
    fn();
    this.notifyPropertyChange('hasDirtyRelationships');
  },

  hasDirtyRelationships: computed('changed', function() {
    let changed = changedKey(this);
    return Object.keys(this._relationshipTracker).some(field => {
      let entry = this._relationshipTracker[field];
      return (changed in entry) && !isEqual(entry[changed], currentState(this, field));
    });
  }),

  hasDirtyFields: or('hasDirtyAttributes', 'hasDirtyRelationships'),

  rollbackRelationships() {
    let changed = changedKey(this);
    let tracker = this._relationshipTracker;
    Object.keys(tracker).forEach(field => {
      if (!tracker[field] || !(changed in tracker[field])) { return; }
      this.set(field, tracker[field][changed]);
    });
    this.notifyPropertyChange('hasDirtyRelationships');
  }

});

function currentState(model, field) {
  let config = get(model.constructor, 'relationshipsByName').get(field);
  if (config.kind === 'hasMany') {
    let reference = model.hasMany(field);
    let refValue = reference.value();
    return refValue ? refValue.toArray() : [];
  } else {
    let reference = model.belongsTo(field);
    return reference.value();
  }
}

function changedKey(model) {
  let changed = model.get('changed');
  if (changed) {
    return changed.getTime();
  } else {
    return -1;
  }
}
<commit_msg>Allow `changed` to not be a Date<commit_after>import { or, gt } from '@ember/object/computed';
import { computed, get } from '@ember/object';
import Mixin from '@ember/object/mixin';
import isEqual from 'lodash/isEqual';

export default Mixin.create({
  init() {
    this._super();
    this._relationshipTracker = Object.create(null);
  },

  watchRelationship(field, fn) {
    let entry = this._relationshipTracker[field];
    if (!entry) {
      entry = this._relationshipTracker[field] = Object.create(null);
    }
    let changed = changedKey(this);
    if (!(changed in entry)) {
      entry[changed] = currentState(this, field);
    }
    fn();
    this.notifyPropertyChange('dirtyRelationships');
  },

  hasDirtyRelationships: gt('dirtyRelationships.length', 0),

  hasDirtyFields: or('hasDirtyAttributes', 'hasDirtyRelationships'),

  dirtyRelationships: computed('changed', function() {
    let changed = changedKey(this);
    let dirty = [];
    this._forEachRelationship(field => {
      let entry = this._relationshipTracker[field];
      let relationshipChanged = (changed in entry) && !isEqual(entry[changed], currentState(this, field));
      if (relationshipChanged) {
        dirty.push(field);
      }
    });
    return dirty;
  }),

  rollbackRelationships() {
    let changed = changedKey(this);
    let tracker = this._relationshipTracker;
    this._forEachRelationship(field => {
      if (!tracker[field] || !(changed in tracker[field])) { return; }
      this.set(field, tracker[field][changed]);
    });
    this.notifyPropertyChange('dirtyRelationships');
  },

  _forEachRelationship(fn) {
    let tracker = this._relationshipTracker;
    Object.keys(tracker).forEach(field => {
      fn(field);
    });
  }
});

function currentState(model, field) {
  let config = get(model.constructor, 'relationshipsByName').get(field);
  if (config.kind === 'hasMany') {
    let reference = model.hasMany(field);
    let refValue = reference.value();
    return refValue ? refValue.toArray() : [];
  } else {
    let reference = model.belongsTo(field);
    return reference.value();
  }
}

function changedKey(model) {
  let changed = model.get('changed');
  if (!changed) {
    return -1;
  }
  if (typeof changed.getTime === 'function') {
    return changed.getTime();
  }
  return changed;
}

{'fraction_non_alphanumeric': 0.1470311027332705, 'fraction_numerical': 0.000706880301602262, 'mean_word_length': 3.8074745186862966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '175677', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1254, 'n_words': 270}