<commit_before>// Backbone.CachedFetch, v1.0.0
// ----------------------------------
//
// Copyright (c) 2015 <NAME>
// Distributed under MIT license
//
// http://github.com/pavelkomiagin/backbone.cachedfetch
(function(root, factory) {

  if (typeof define === 'function' && define.amd) {
    define(['underscore', 'backbone'], function(_, Backbone) {
      return factory(_, Backbone);
    });
  } else if (typeof exports !== 'undefined') {
    var _ = require('underscore');
    var Backbone = require('backbone');
    module.exports = factory(_, Backbone);
  } else {
    factory(root._, root.Backbone);
  }

}(this, function(_, Backbone) {
  'use strict';

  // Helpers
  // -------
  // localStorage with expiration
  var StoreWithExpiration = function() {
    return {
      set: function(key, value, expireTime) {
        var item = {
          value: value,
          expireTime: expireTime,
          cachedAt: new Date().getTime()
        };

        try {
          localStorage.setItem(key, JSON.stringify(item));
        } catch(e) {}
      },

      get: function(key) {
        try {
          var info = JSON.parse(localStorage.getItem(key));

          if (!info || new Date().getTime() - info.cachedAt > info.expireTime)
            return null;

          return info.value;

        } catch(e) {
          localStorage.removeItem(key);
        }
      }
    };
  };

  var expirationStore = new StoreWithExpiration();
  Backbone.Collection.extend({

    cacheKey: 'bbCollection_' + this.url,

    cachedFetch: function(options) {
      options = options || {};
      var expireTime = options.expireTime || 60 * 60 * 1000;

      if (!window.localStorage)
        return this.fetch(options);

      var cachedData = expirationStore.get(this.cacheKey);
      var cachedModels = cachedData ? cachedData.value : [];
      var success = options.success;

      if (!cachedData) {

        options.success = _.bind(function (resp) {
          if (success)
            success.call(options.context, this, resp, options);

          expirationStore.set(cacheKey, {
            value: this.models,
            expireTime: expireTime
          });

        }, this);
        return this.fetch(options);

      } else {

        options = _.extend({parse: true}, options);
        var method = options.reset ? 'reset' : 'set';
        this[method](cachedModels, options);

        if (success)
          success.call(options.context, this, cachedModels, options);

        this.trigger('sync', this, cachedModels, options);

        return this.sync('cachedRead', this, options);

      }
    }

  });



  Backbone.Model.extend({

    cacheKey: 'bbModel_' + this.url + '_' + this.id,

    cachedFetch: function(options) {
      options = options || {};
      var expireTime = options.expireTime || 60 * 60 * 1000;

      if (!window.localStorage)
        return this.fetch(options);

      var cachedData = expirationStore.get(this.cacheKey);
      var cachedModel = cachedData ? cachedData.value : {};
      var success = options.success;

      if (!cachedData) {

        options.success = _.bind(function (resp) {
          if (success)
            success.call(options.context, this, resp, options);

          expirationStore.set(this.cacheKey, {
            value: this.models,
            expireTime: expireTime
          });

        }, this);
        return this.fetch(options);<commit_msg>Fix bug<commit_after>// Backbone.CachedFetch, v1.0.0
// ----------------------------------
//
// Copyright (c) 2015 <NAME>
// Distributed under MIT license
//
// http://github.com/pavelkomiagin/backbone.cachedfetch
(function(root, factory) {

  if (typeof define === 'function' && define.amd) {
    define(['underscore', 'backbone'], function(_, Backbone) {
      return factory(_, Backbone);
    });
  } else if (typeof exports !== 'undefined') {
    var _ = require('underscore');
    var Backbone = require('backbone');
    module.exports = factory(_, Backbone);
  } else {
    factory(root._, root.Backbone);
  }

}(this, function(_, Backbone) {
  'use strict';

  // Helpers
  // -------
  // localStorage with expiration
  var StoreWithExpiration = function() {
    return {
      set: function(key, value, expireTime) {
        var item = {
          value: value,
          expireTime: expireTime,
          cachedAt: new Date().getTime()
        };

        try {
          localStorage.setItem(key, JSON.stringify(item));
        } catch(e) {}
      },

      get: function(key) {
        try {
          var info = JSON.parse(localStorage.getItem(key));

          if (!info || new Date().getTime() - info.cachedAt > info.expireTime)
            return null;

          return info.value;

        } catch(e) {
          localStorage.removeItem(key);
        }
      }
    };
  };

  var expirationStore = new StoreWithExpiration();
  Backbone.Collection.extend({

    cachedFetch: function(options) {
      options = options || {};
      var expireTime = options.expireTime || 60 * 60 * 1000;
      var cacheKey = 'bbCollection_' + this.url;

      if (!window.localStorage)
        return this.fetch(options);

      var cachedData = expirationStore.get(cacheKey);
      var cachedModels = cachedData ? cachedData.value : [];
      var success = options.success;

      if (!cachedData) {

        options.success = _.bind(function (resp) {
          if (success)
            success.call(options.context, this, resp, options);

          expirationStore.set(cacheKey, {
            value: this.models,
            expireTime: expireTime
          });

        }, this);
        return this.fetch(options);

      } else {

        options = _.extend({parse: true}, options);
        var method = options.reset ? 'reset' : 'set';
        this[method](cachedModels, options);

        if (success)
          success.call(options.context, this, cachedModels, options);

        this.trigger('sync', this, cachedModels, options);

        return this.sync('cachedRead', this, options);

      }
    }

  });

  Backbone.Model.extend({

    cachedFetch: function(options) {
      options = options || {};
      var expireTime = options.expireTime || 60 * 60 * 1000;
      var cacheKey = 'bbModel_' + this.url + '_' + this.id;

      if (!window.localStorage)
        return this.fetch(options);

      var cachedData = expirationStore.get(cacheKey);
      var cachedModel = cachedData ? cachedData.value : {};
      var success = options.success;

      if (!cachedData) {

        options.success = _.bind(function (resp) {
          if (success)
            success.call(options.context, this, resp, options);

          expirationStore.set(cacheKey, {
            value: this.models,
            expireTime: expireTime
          });

        }, this);
        return this.fetch(options);
{'fraction_non_alphanumeric': 0.14524893523278015, 'fraction_numerical': 0.006755764429431634, 'mean_word_length': 2.119560238204306, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '319550', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1895, 'n_words': 432}