<commit_before>/**
 * Provides findWithCache method that forces a minimum cache time on the find
 * requests.
 *
 * @class Ember Data Store Cache Mixin
 */

import Ember from "ember";

export default Ember.Mixin.create({
    /**
     * Number of seconds to require caching when using findWithCache method.
     *
     * @param cacheSeconds
     * @type {Integer}
     * @default 600
     */
    cacheSeconds: 600,

    /**
     * Customized find method that checks for cache within a certain timeframe.
     *
     * @method findWithCache
     * @param {String} typeKey
     * @param {Mixed|Integer|Object} id
     * @param {Object} payload
     * @param {Integer} cacheTime Amount of time in seconds to cache for
     * (overrides cacheSeconds param per instance)
     */
    findWithCache: function(typeKey, id, payload, cacheTime) {
        if (arguments.length === 1) {
            // Find all.

            // Get last queried time.
            var type = this.modelFor(typeKey);
            var requestedAt = this.typeMapFor(type).metadata.find_all_requested;
            var now = this._getTimeNow();

            cacheTime = cacheTime || this.get('cacheSeconds');
            if (requestedAt && now - requestedAt < cacheTime) {
                // If time is within cacheSeconds, get from store.
                return this.all(typeKey);
            } else {
                // Else, make request.
                return this.findAll(typeKey, id);
            }
        }

        if (Ember.typeOf(id) === 'object') {
            // Find query.
            Ember.assert('findWithCache does not support queries yet. To use cache, request with find and use store.filter instead.');
        }

        // Find record by ID. Already loads from store if present.
        Ember.assert('findWithCache does not support finding by id. Use find since it returns the record from the store if it is present.');
    },

    /**
     * Remove all entries of typeKey from the store and reset the metadata
     * cache time so the next request via findWithCache will make the request
     * to the server.
     *
     * @method clearCache
     * @param {String} typeKey Type to clear
     */
    clearCache: function(typeKey) {
        this.unloadAll(typeKey);
        this.clearCacheTime(typeKey);
    },

    /**
     * Reset the cache time for time to null so the next request via
     * findWithCache will make the request to the server.
     *
     * @method clearCacheTime
     * @param {String} typeKey Type of clear
     */
    clearCacheTime(typeKey) {
        var type = this.modelFor(typeKey);
        this.typeMapFor(type).metadata.find_all_requested = null;
    },

    /**
     * Override findAll method of store to set the find_all_requested metadata
     * for use in determining the last request time in findWithCache
     *
     * @method findAll
     * @param {String} typeKey
     */
    findAll: function(typeKey) {
        var type = this.modelFor(typeKey);
        var promise = this._super.apply(this, arguments);
        promise.then(function() {
            this.typeMapFor(type).metadata.find_all_requested = this._getTimeNow();
        }.bind(this));
        return promise;
    },

    /**
     * Return the current timestamp in seconds.
     *
     * @method _getTimeNow
     * @private
     * @return {Integer} Timestamp in seconds.
     */
    _getTimeNow: function() {
        return Math.ceil(new Date() / 1000);
    }
});
<commit_msg>Wrap the returned promise in a promise so errors are handled as default intead of thrown<commit_after>/**
 * Provides findWithCache method that forces a minimum cache time on the find
 * requests.
 *
 * @class Ember Data Store Cache Mixin
 */

import Ember from "ember";

export default Ember.Mixin.create({
    /**
     * Number of seconds to require caching when using findWithCache method.
     *
     * @param cacheSeconds
     * @type {Integer}
     * @default 600
     */
    cacheSeconds: 600,

    /**
     * Customized find method that checks for cache within a certain timeframe.
     *
     * @method findWithCache
     * @param {String} typeKey
     * @param {Mixed|Integer|Object} id
     * @param {Object} payload
     * @param {Integer} cacheTime Amount of time in seconds to cache for
     * (overrides cacheSeconds param per instance)
     */
    findWithCache: function(typeKey, id, payload, cacheTime) {
        if (arguments.length === 1) {
            // Find all.

            // Get last queried time.
            var type = this.modelFor(typeKey);
            var requestedAt = this.typeMapFor(type).metadata.find_all_requested;
            var now = this._getTimeNow();

            cacheTime = cacheTime || this.get('cacheSeconds');
            if (requestedAt && now - requestedAt < cacheTime) {
                // If time is within cacheSeconds, get from store.
                return this.all(typeKey);
            } else {
                // Else, make request.
                return this.findAll(typeKey, id);
            }
        }

        if (Ember.typeOf(id) === 'object') {
            // Find query.
            Ember.assert('findWithCache does not support queries yet. To use cache, request with find and use store.filter instead.');
        }

        // Find record by ID. Already loads from store if present.
        Ember.assert('findWithCache does not support finding by id. Use find since it returns the record from the store if it is present.');
    },

    /**
     * Remove all entries of typeKey from the store and reset the metadata
     * cache time so the next request via findWithCache will make the request
     * to the server.
     *
     * @method clearCache
     * @param {String} typeKey Type to clear
     */
    clearCache: function(typeKey) {
        this.unloadAll(typeKey);
        this.clearCacheTime(typeKey);
    },

    /**
     * Reset the cache time for time to null so the next request via
     * findWithCache will make the request to the server.
     *
     * @method clearCacheTime
     * @param {String} typeKey Type of clear
     */
    clearCacheTime(typeKey) {
        var type = this.modelFor(typeKey);
        this.typeMapFor(type).metadata.find_all_requested = null;
    },

    /**
     * Override findAll method of store to set the find_all_requested metadata
     * for use in determining the last request time in findWithCache
     *
     * @method findAll
     * @param {String} typeKey
     */
    findAll: function(typeKey) {
        var args = arguments;
        var type = this.modelFor(typeKey);
        return new Ember.RSVP.Promise(function(resolve, reject) {
            this._super.apply(this, args)
            .then(function(result) {
                Ember.run(this, function() {
                    this.typeMapFor(type).metadata.find_all_requested = this._getTimeNow();
                    resolve(result);
                });
            }.bind(this))
            .catch(function(result) {
                Ember.run(this, function() {
                    reject(result);
                });
            });
        }.bind(this));
    },

    /**
     * Return the current timestamp in seconds.
     *
     * @method _getTimeNow
     * @private
     * @return {Integer} Timestamp in seconds.
     */
    _getTimeNow: function() {
        return Math.ceil(new Date() / 1000);
    }
});

{'fraction_non_alphanumeric': 0.09598366235534377, 'fraction_numerical': 0.0029952348536419334, 'mean_word_length': 2.0380479735318446, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1066926', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1982, 'n_words': 660}