<commit_before>/** @module emberfire-utils */
import { pluralize } from 'ember-inflector';
import { assign } from 'ember-platform';
import { bind } from 'ember-runloop';
import Adapter from 'ember-data/adapter';
import RSVP from 'rsvp';
import inject from 'ember-service/inject';

/**
 * @class FirebaseFlex
 * @namespace Adapter
 * @extends DS.Adapter
 */
export default Adapter.extend({
  defaultSerializer: '-firebase-flex',

  /**
   * @type {Ember.Service}
   * @default
   * @readonly
   */
  firebase: inject(),

  /**
   * @type {Object}
   * @default
   */
  trackedListeners: {},

  /**
   * @return {string} Push ID
   */
  generateIdForRecord() {
    return this.get('firebase').push().key;
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @param {DS.Snapshot} snapshot
   * @return {Promise} Resolves when create record succeeds
   */
  createRecord(store, type, snapshot) {
    return this.updateRecord(store, type, snapshot).then(() => {
      this._setupValueListener(store, type.modelName, snapshot.id);
    });
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @param {DS.Snapshot} snapshot
   * @return {Promise} Resolves when update record succeeds
   */
  updateRecord(store, type, snapshot) {
    return new RSVP.Promise(bind(this, (resolve, reject) => {
      const serializedSnapshot = this.serialize(snapshot);
      const serializedInclude = this._serializeInclude(snapshot);
      const fanout = assign({}, serializedSnapshot, serializedInclude);

      this.get('firebase').update(fanout, bind(this, (error) => {
        if (error) {
          reject(error);
        } else {
          resolve();
        }
      }));
    }));
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @param {string} id
   * @return {Promise} Resolves with the fetched record
   */
  findRecord(store, type, id) {
    return new RSVP.Promise(bind(this, (resolve, reject) => {
      const modelName = type.modelName;
      const ref = this._getFirebaseReference(modelName, id);
      const onValue = bind(this, (snapshot) => {
        if (snapshot.exists()) {
          resolve(this._getGetSnapshotWithId(snapshot));

          this._setupValueListener(store, modelName, id);
          ref.off('value', onValue);
        } else {
          reject();
        }
      });

      ref.on('value', onValue);
    }));
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @return {Promise} Resolves with the fetched records
   */
  findAll(store, type) {
    return new RSVP.Promise(bind(this, (resolve, reject) => {
      const modelName = type.modelName;
      const ref = this._getFirebaseReference(modelName);

      ref.on('value', bind(this, (snapshot) => {
        if (snapshot.exists()) {
          const records = [];

          snapshot.forEach((child) => {
            this._setupValueListener(store, modelName, child.key);
            records.push(this._getGetSnapshotWithId(child));
          });

          resolve(records);

          this._setupListListener(store, modelName);
          ref.off('value');
        } else {
          reject();
        }
      }), bind(this, (error) => {
        reject(error);
      }));
    }));
  },<commit_msg>Move some resolves<commit_after>/** @module emberfire-utils */
import { pluralize } from 'ember-inflector';
import { assign } from 'ember-platform';
import { bind } from 'ember-runloop';
import Adapter from 'ember-data/adapter';
import RSVP from 'rsvp';
import inject from 'ember-service/inject';

/**
 * @class FirebaseFlex
 * @namespace Adapter
 * @extends DS.Adapter
 */
export default Adapter.extend({
  defaultSerializer: '-firebase-flex',

  /**
   * @type {Ember.Service}
   * @default
   * @readonly
   */
  firebase: inject(),

  /**
   * @type {Object}
   * @default
   */
  trackedListeners: {},

  /**
   * @return {string} Push ID
   */
  generateIdForRecord() {
    return this.get('firebase').push().key;
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @param {DS.Snapshot} snapshot
   * @return {Promise} Resolves when create record succeeds
   */
  createRecord(store, type, snapshot) {
    return this.updateRecord(store, type, snapshot).then(() => {
      this._setupValueListener(store, type.modelName, snapshot.id);
    });
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @param {DS.Snapshot} snapshot
   * @return {Promise} Resolves when update record succeeds
   */
  updateRecord(store, type, snapshot) {
    return new RSVP.Promise(bind(this, (resolve, reject) => {
      const serializedSnapshot = this.serialize(snapshot);
      const serializedInclude = this._serializeInclude(snapshot);
      const fanout = assign({}, serializedSnapshot, serializedInclude);

      this.get('firebase').update(fanout, bind(this, (error) => {
        if (error) {
          reject(error);
        } else {
          resolve();
        }
      }));
    }));
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @param {string} id
   * @return {Promise} Resolves with the fetched record
   */
  findRecord(store, type, id) {
    return new RSVP.Promise(bind(this, (resolve, reject) => {
      const modelName = type.modelName;
      const ref = this._getFirebaseReference(modelName, id);
      const onValue = bind(this, (snapshot) => {
        if (snapshot.exists()) {
          this._setupValueListener(store, modelName, id);
          ref.off('value', onValue);
          resolve(this._getGetSnapshotWithId(snapshot));
        } else {
          reject();
        }
      });

      ref.on('value', onValue);
    }));
  },

  /**
   * @param {DS.Store} store
   * @param {DS.Model} type
   * @return {Promise} Resolves with the fetched records
   */
  findAll(store, type) {
    return new RSVP.Promise(bind(this, (resolve, reject) => {
      const modelName = type.modelName;
      const ref = this._getFirebaseReference(modelName);

      ref.on('value', bind(this, (snapshot) => {
        if (snapshot.exists()) {
          const records = [];

          snapshot.forEach((child) => {
            this._setupValueListener(store, modelName, child.key);
            records.push(this._getGetSnapshotWithId(child));
          });

          this._setupListListener(store, modelName);
          ref.off('value');
          resolve(records);
        } else {
          reject();
        }
      }), bind(this, (error) => {
        reject(error);
      }));
    }));
  },
{'fraction_non_alphanumeric': 0.15342044581091469, 'fraction_numerical': 0.0, 'mean_word_length': 2.584573002754821, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '1186087', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1977, 'n_words': 438}