<commit_before>var localforage = require('localforage');
var lodash = require('lodash');

var storageVersion = 3;

function fullKeyFor(key) {
  return 'workspaces/' + storageVersion + '/' + key;
}

var Storage = {
  getCurrentProjectKey: function() {
    return localforage.getItem('currentProjectKey');
  },

  setCurrentProjectKey: function(projectKey) {
    localforage.setItem('currentProjectKey', projectKey);
  },

  all: function() {
    return localforage.getItem('allKeys').then(function(keys) {
      return Promise.all(keys.map(this.load));
    }.bind(this));
  },

  load: function(projectKey) {
    return localforage.getItem(fullKeyFor(projectKey)).then(function(payload) {
      if (payload !== null) {
        return payload;
      }
    });
  },

  save: function(data) {
    var key = data.projectKey;

    localforage.getItem('allKeys').then(function(oldKeys) {
      if (oldKeys === null || oldKeys[oldKeys.length - 1] !== key) {
        var keys = lodash.without(oldKeys || [], key);
        keys.unshift(key);
        localforage.setItem('allKeys', keys);
      }
    });

    localforage.setItem(
      fullKeyFor(key),
      lodash.extend({
        storageVersion: storageVersion,
        updatedAt: new Date(),
      }, data)
    );

    return data;
  },
};

module.exports = Storage;
<commit_msg>Wait for project to finish saving before updating key list<commit_after>var localforage = require('localforage');
var lodash = require('lodash');

var storageVersion = 3;

function fullKeyFor(key) {
  return 'workspaces/' + storageVersion + '/' + key;
}

var Storage = {
  getCurrentProjectKey: function() {
    return localforage.getItem('currentProjectKey');
  },

  setCurrentProjectKey: function(projectKey) {
    localforage.setItem('currentProjectKey', projectKey);
  },

  all: function() {
    return localforage.getItem('allKeys').then(function(keys) {
      return Promise.all(keys.map(this.load));
    }.bind(this));
  },

  load: function(projectKey) {
    return localforage.getItem(fullKeyFor(projectKey)).then(function(payload) {
      if (payload !== null) {
        return payload;
      }
    });
  },

  save: function(data) {
    var key = data.projectKey;

    localforage.setItem(
      fullKeyFor(key),
      lodash.extend({
        storageVersion: storageVersion,
        updatedAt: new Date(),
      }, data)
    ).then(function() {
      localforage.getItem('allKeys').then(function(oldKeys) {
        if (oldKeys === null || oldKeys[oldKeys.length - 1] !== key) {
          var keys = lodash.without(oldKeys || [], key);
          keys.unshift(key);
          localforage.setItem('allKeys', keys);
        }
      });
    });

    return data;
  },
};

module.exports = Storage;

{'fraction_non_alphanumeric': 0.14840600952729938, 'fraction_numerical': 0.0014657383657017222, 'mean_word_length': 3.123867069486405, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2230722', 'n_tokens_mistral': 859, 'n_tokens_neox': 843, 'n_words': 156}