<commit_before><script type="text/javascript">
(function() {
  "use strict";

  function getRequestPromise(request) {
    return new Promise((resolve, reject) => {
      request.onsuccess = (event) => {
        resolve(event.target.result);
      };
      request.onerror = (event) => {
        reject(event.target.errorCode);
      };
    });
  }

  function getTransactionPromise(transaction) {
    console.log('create!');
    return new Promise((resolve, reject) => {
      transaction.oncomplete = () => {
        console.log('success');
        resolve();
      };
      transaction.onerror = (event) => {
        console.log('error');
        reject(event.target.errorCode);
      };
    });
  }

  let getDB = (function() {
    var global_db;

    return function() {
      if ($.isDefined(global_db)) {
        return Promise.resolve(global_db);
      }
      if (!$.isDefined(document.account) || !$.isDefined(document.account.screen_name)) {
        throw 'No account is logged in!';
      }
      let request = window.indexedDB.open(`Coolol_${document.account.screen_name}`, 1);
      request.onupgradeneeded = (event) => {
        let db = event.target.result;
        switch (event.oldVersion) {
          case 0:
            db.createObjectStore('tweet', {keyPath: 'id_str'});
          // fall through
        }
      };
      return getRequestPromise(request).then((db) => {
        global_db = db;
        return db;
      });
    };
  })();


  class Store {

    static addTableAccess(table_name) {
      let capitalized = $.camelize(table_name);

      let getMethodName = `get${capitalized}`;
      let putMethodName = `put${capitalized}`;
      let deleteMethodName = `delete${capitalized}`;
      let registerMethodName = `register${capitalized}`;
      let unregisterMethodName = `unregister${capitalized}`;

      // read
      this.prototype[getMethodName] = function(key) {
        return getDB().then((db) => {
          let request = db
            .transaction([table_name], 'readonly')
            .objectStore(table_name)
            .get(key);
          return getRequestPromise(request);
        });
      };
      this.prototype[`${getMethodName}s`] = function(keys) {
        return getDB().then((db) => {
          let object_store = db
            .transaction([table_name], 'readonly')
            .objectStore(table_name);
          return Promise.all(keys.map((id_str) => {
            return getRequestPromise(object_store.get(id_str));
          }));
        });
      };

      // write
      this.prototype[putMethodName] = function(tweet) {
        return getDB().then((db) => {
          let request = db.transaction(['tweet'], 'readwrite')
            .objectStore('tweet')
            .put(tweet);
          return getRequestPromise(request);
        });
      };
      this.prototype[`${putMethodName}s`] = function(tweets) {
        return getDB().then((db) => {
          let transaction = db.transaction(['tweet'], 'readwrite');
          let tweet_store = transaction.objectStore('tweet');
          for (let tweet of tweets) {
            tweet_store.put(tweet);
          }
          return getTransactionPromise(transaction);
        });
      };

      // delete
      this.prototype[deleteMethodName] = function() {
        let old_value = this[getMethodName]();
        Store.log(`[store] ${deleteMethodName}:`, old_value);
        this.saveJSON(table_name, factory());
        return old_value;
      };

      // register
      this.prototype[registerMethodName] = function(element) {
        return this.register(table_name, element);
      };
      this.prototype[unregisterMethodName] = function(element) {
        return this.unregister(table_name, element);
      };

      return table_name;
    }

    static log() {<commit_msg>rewrite indexeddb with async functions<commit_after><script type="text/javascript">
(function() {
  "use strict";

  function getRequestPromise(request) {
    return new Promise((resolve, reject) => {
      request.onsuccess = (event) => {
        resolve(event.target.result);
      };
      request.onerror = (event) => {
        reject(event.target.errorCode);
      };
    });
  }

  function getTransactionPromise(transaction) {
    console.log('create!');
    return new Promise((resolve, reject) => {
      transaction.oncomplete = () => {
        console.log('success');
        resolve();
      };
      transaction.onerror = (event) => {
        console.log('error');
        reject(event.target.errorCode);
      };
    });
  }

  let getDB = (function() {
    let global_db;

    return async function() {
      if ($.isDefined(global_db)) {
        return global_db;
      }
      if (!$.isDefined(document.account) || !$.isDefined(document.account.screen_name)) {
        throw 'No account is logged in!';
      }
      let request = window.indexedDB.open(`Coolol_${document.account.screen_name}`, 1);
      request.onupgradeneeded = (event) => {
        let db = event.target.result;
        switch (event.oldVersion) {
          case 0:
            db.createObjectStore('tweet', {keyPath: 'id_str'});
            // fall through
        }
      };
      global_db = await getRequestPromise(request);
      return global_db;
    };
  })();


  class Store {

    static addTableAccess(table_name) {
      let capitalized = $.camelize(table_name);

      let getMethodName = `get${capitalized}`;
      let putMethodName = `put${capitalized}`;
      let deleteMethodName = `delete${capitalized}`;
      let registerMethodName = `register${capitalized}`;
      let unregisterMethodName = `unregister${capitalized}`;

      // read
      this.prototype[getMethodName] = async function(key) {
        let db = await getDB();
        let request = db
          .transaction([table_name], 'readonly')
          .objectStore(table_name)
          .get(key);
        return await getRequestPromise(request);
      };
      this.prototype[`${getMethodName}s`] = async function(keys) {
        let db = await getDB();
        let object_store = db
          .transaction([table_name], 'readonly')
          .objectStore(table_name);
        return await Promise.all(keys.map((key) => {
          return getRequestPromise(object_store.get(key));
        }));
      };

      // write
      this.prototype[putMethodName] = async function(object) {
        let db = await getDB();
        let request = db
          .transaction([table_name], 'readwrite')
          .objectStore(table_name)
          .put(object);
        return await getRequestPromise(request);
      };
      this.prototype[`${putMethodName}s`] = async function(objects) {
        let db = await getDB();
        let transaction = db.transaction([table_name], 'readwrite');
        let object_store = transaction.objectStore(table_name);
        for (let object of objects) {
          object_store.put(object);
        }
        return await getTransactionPromise(transaction);
      };

      // delete
      this.prototype[deleteMethodName] = async function(key) {
        let db = await getDB();
        let request = db.transaction([table_name], 'write')
          .objectStore(table_name)
          .delete(key);
        return await getRequestPromise(request);
      };
      this.prototype[`${deleteMethodName}s`] = async function() {
        let db = await getDB();
        let request = db.transaction([table_name], 'write')
          .objectStore(table_name)
          .clear();
        return await getRequestPromise(request);
      };

      // register
      this.prototype[registerMethodName] = function(element) {
        return this.register(table_name, element);
      };
      this.prototype[unregisterMethodName] = function(element) {
        return this.unregister(table_name, element);
      };

      return table_name;
    }

    static log() {
{'fraction_non_alphanumeric': 0.13782542113323124, 'fraction_numerical': 0.0005104645227156713, 'mean_word_length': 2.3235793044953352, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '2312778', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2207, 'n_words': 411}