<commit_before>import Ember from 'ember';
import Base from './base';
import ModelMixin from './-model-mixin';
import LoaderState from './-loader-state';
import ObserveOwner from './-observe-owner';

const {
  RSVP: { resolve, defer },
  A
} = Ember;

class Operation {

  constructor(loader, force) {
    this.loader = loader;
    this.force = force;
    this.deferred = defer();
  }

  invoke() {
    let operation = this.loader._scheduleDocumentOperation(this.force);
    operation.promise.then(arg => this.deferred.resolve(arg), err => this.deferred.reject(err));
  }

  get promise() {
    return this.deferred.promise;
  }

}

export default class LoaderInternal extends ObserveOwner(ModelMixin(Base)) {

  /*
    opts: {
      autoload: true,
      owner: [ 'id' ],
      query(props) {
        return { id: props.id };
      }
    }
    type: 'first' / 'find'
  */
  constructor(store, database, owner, opts, type) {
    super();
    this.store = store;
    this.database = database;
    this.owner = owner;
    this.type = type;
    this.opts = opts;
    this.state = new LoaderState();
    this.operations = A();
  }

  _didCreateModel() {
    super._didCreateModel();
    this._startObserving();
  }

  _createModel() {
    return this.store._createLoader(this);
  }

  //

  _state(key) {
    this._scheduleAutoload(false, true, [ key ]);
    return this.state[key];
  }

  _withState(cb, except) {
    return this.withPropertyChanges(changed => cb(this.state, changed), true, except);
  }

  //

  get query() {
    let query = this.opts.query;
    let owner = this.owner;
    return query.call(owner, owner);
  }

  _scheduleDocumentOperation(force) {
    let { database, query } = this;
    if(force) {
      query.force = true;
    }
    return database._scheduleDocumentOperation(query, this.type, () => {
      this._withState((state, changed) => state.onLoading(changed));
    }, () => {
      this._withState((state, changed) => state.onLoaded(changed));
    }, err => {
      this._withState((state, changed) => state.onError(err, changed));
    });
  }

  //

  _scheduleLoad(force, reuse, except) {
    this._withState((state, changed) => state.onLoadScheduled(changed), except);

    let operations = this.operations;
    let operation = operations.get('lastObject');

    if(!operation || !reuse || (force && this.state.isLoaded && !operation.force)) {
      operation = new Operation(this, force);
      operation.promise.catch(() => {}).finally(() => operations.removeObject(operation));
      operations.pushObject(operation);
      operation.invoke();
    }
<commit_msg>cleanup<commit_after>import Ember from 'ember';
import Base from './base';
import ModelMixin from './-model-mixin';
import LoaderState from './-loader-state';
import ObserveOwner from './-observe-owner';

const {
  RSVP: { resolve, defer },
  A,
  merge
} = Ember;

class Operation {

  constructor(loader, force) {
    this.loader = loader;
    this.force = force;
    this.deferred = defer();
  }

  invoke() {
    let operation = this.loader._scheduleDocumentOperation(this.force);
    operation.promise.then(arg => this.deferred.resolve(arg), err => this.deferred.reject(err));
  }

  get promise() {
    return this.deferred.promise;
  }

}

export default class LoaderInternal extends ObserveOwner(ModelMixin(Base)) {

  /*
    opts: {
      autoload: true,
      owner: [ 'id' ],
      query(props) {
        return { id: props.id };
      }
    }
    type: 'first' / 'find'
  */
  constructor(store, database, owner, opts, type) {
    super();
    this.store = store;
    this.database = database;
    this.owner = owner;
    this.opts = merge({ autoload: true }, opts);
    this.type = type;
    this.state = new LoaderState();
    this.operations = A();
  }

  _didCreateModel() {
    super._didCreateModel();
    this._startObserving();
  }

  _createModel() {
    return this.store._createLoader(this);
  }

  //

  _state(key) {
    this._scheduleAutoload(false, true, [ key ]);
    return this.state[key];
  }

  _withState(cb, except) {
    return this.withPropertyChanges(changed => cb(this.state, changed), true, except);
  }

  //

  get query() {
    let query = this.opts.query;
    let owner = this.owner;
    return query.call(owner, owner);
  }

  _scheduleDocumentOperation(force) {
    let { database, query } = this;

    if(force) {
      query.force = true;
    }

    return database._scheduleDocumentOperation(query, this.type, () => {
      this._withState((state, changed) => state.onLoading(changed));
    }, () => {
      this._withState((state, changed) => state.onLoaded(changed));
    }, err => {
      this._withState((state, changed) => state.onError(err, changed));
    });
  }

  //

  _scheduleLoad(force, reuse, except) {
    this._withState((state, changed) => state.onLoadScheduled(changed), except);

    let operations = this.operations;
    let operation = operations.get('lastObject');

    if(!operation || !reuse || (force && this.state.isLoaded && !operation.force)) {
      operation = new Operation(this, force);
      operation.promise.catch(() => {}).finally(() => operations.removeObject(operation));
      operations.pushObject(operation);
      operation.invoke();
    }

{'fraction_non_alphanumeric': 0.15512624330527927, 'fraction_numerical': 0.0, 'mean_word_length': 3.0099693251533743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '2210168', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1626, 'n_words': 338}