<commit_before>const { declareInjections } = require('@cardstack/di');

module.exports = declareInjections({
  schemaCache: 'hub:schema-cache',
  pluginLoader: 'hub:plugin-loader'
},

class PluginIndexer {
  async branches() {
    return ['master'];
  }
  async beginUpdate(/* branch */) {
    let schema = await this.schemaCache.schemaForControllingBranch();
    return new Updater(schema.plugins);
  }
});

class Updater {
  constructor(plugins) {
    this.plugins = plugins;
  }

  async schema() {
    return [];
  }

  async updateContent(meta, hints, ops) {
    for (let plugin of this.plugins.describeAll()) {
      await ops.save('plugins', plugin.id, plugin);
      for (let { type, id } of plugin.relationships.features.data) {
        await ops.save(type, id, this.plugins.describeFeature(type, id));
      }
    }
  }

  async read(type, id /*, isSchema */) {
    if (type === 'plugins') {
      return this.plugins.describe(id);
    } else {
      return this.plugins.describeFeature(type, id);
    }
  }
}<commit_msg>don't leak plugin state across runs<commit_after>const { declareInjections } = require('@cardstack/di');
const { isEqual } = require('lodash');

module.exports = declareInjections({
  schemaCache: 'hub:schema-cache',
  pluginLoader: 'hub:plugin-loader'
},

class PluginIndexer {
  async branches() {
    return ['master'];
  }
  async beginUpdate(/* branch */) {
    let schema = await this.schemaCache.schemaForControllingBranch();
    return new Updater(schema.plugins);
  }
});

class Updater {
  constructor(plugins) {
    this.plugins = plugins;
  }

  async schema() {
    return [];
  }

  async updateContent(meta, hints, ops) {
    let plugins = this.plugins.describeAll();
    if (meta && isEqual(meta.plugins, plugins)) {
      return { plugins };
    }
    await ops.beginReplaceAll();
    for (let plugin of plugins) {
      await ops.save('plugins', plugin.id, plugin);
      for (let { type, id } of plugin.relationships.features.data) {
        await ops.save(type, id, this.plugins.describeFeature(type, id));
      }
    }
    await ops.finishReplaceAll();
    return { plugins };
  }

  async read(type, id /*, isSchema */) {
    if (type === 'plugins') {
      return this.plugins.describe(id);
    } else {
      return this.plugins.describeFeature(type, id);
    }
  }
}
{'fraction_non_alphanumeric': 0.14679294016358158, 'fraction_numerical': 0.0, 'mean_word_length': 3.164874551971326, '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': '4418403', 'n_tokens_mistral': 737, 'n_tokens_neox': 725, 'n_words': 163}