tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

reading-autoincrement-common.js (3131B)


      1 // Returns the "name" property written to the object with the given ID.
      2 'use strict';
      3 function nameForId(id) {
      4  return `Object ${id}`;
      5 }
      6 
      7 // Initial database setup used by all the reading-autoincrement tests.
      8 async function setupAutoincrementDatabase(testCase) {
      9  const database = await createDatabase(testCase, database => {
     10    const store = database.createObjectStore(
     11        'store', { autoIncrement: true, keyPath: 'id' });
     12    store.createIndex('by_name', 'name', { unique: true });
     13    store.createIndex('by_id', 'id', { unique: true });
     14 
     15    // Cover writing from the initial upgrade transaction.
     16    for (let i = 1; i <= 16; ++i) {
     17      if (i % 2 == 0) {
     18        store.put({name: nameForId(i), id: i});
     19      } else {
     20        store.put({name: nameForId(i)});
     21      }
     22    }
     23  });
     24 
     25  // Cover writing from a subsequent transaction.
     26  const transaction = database.transaction(['store'], 'readwrite');
     27  const store = transaction.objectStore('store');
     28  for (let i = 17; i <= 32; ++i) {
     29    if (i % 2 == 0) {
     30      store.put({name: nameForId(i), id: i});
     31    } else {
     32      store.put({name: nameForId(i)});
     33    }
     34  }
     35  await promiseForTransaction(testCase, transaction);
     36 
     37  return database;
     38 }
     39 
     40 // Returns the IDs used by the object store, sorted as strings.
     41 //
     42 // This is used to determine the correct order of records when retrieved from an
     43 // index that uses stringified IDs.
     44 function idsSortedByStringCompare() {
     45  const stringIds = [];
     46  for (let i = 1; i <= 32; ++i)
     47    stringIds.push(i);
     48  stringIds.sort((a, b) => indexedDB.cmp(`${a}`, `${b}`));
     49  return stringIds;
     50 }
     51 
     52 async function iterateCursor(testCase, cursorRequest, callback) {
     53  // This uses requestWatcher() directly instead of using promiseForRequest()
     54  // inside the loop to avoid creating multiple EventWatcher instances. In turn,
     55  // this avoids ending up with O(N) listeners for the request and O(N^2)
     56  // dispatched events.
     57  const eventWatcher = requestWatcher(testCase, cursorRequest);
     58  while (true) {
     59    const event = await eventWatcher.wait_for('success');
     60    const cursor = event.target.result;
     61    if (cursor === null)
     62      return;
     63    callback(cursor);
     64    cursor.continue();
     65  }
     66 }
     67 
     68 // Returns equivalent information to getAllKeys() by iterating a cursor.
     69 //
     70 // Returns an array with one dictionary per entry in the source. The dictionary
     71 // has the properties "key" and "primaryKey".
     72 async function getAllKeysViaCursor(testCase, cursorSource) {
     73  const results = [];
     74  await iterateCursor(testCase, cursorSource.openKeyCursor(), cursor => {
     75    results.push({ key: cursor.key, primaryKey: cursor.primaryKey });
     76  });
     77  return results;
     78 }
     79 
     80 // Returns equivalent information to getAll() by iterating a cursor.
     81 //
     82 // Returns an array with one dictionary per entry in the source. The dictionary
     83 // has the properties "key", "primaryKey" and "value".
     84 async function getAllViaCursor(testCase, cursorSource) {
     85  const results = [];
     86  await iterateCursor(testCase, cursorSource.openCursor(), cursor => {
     87    results.push({
     88      key: cursor.key,
     89      primaryKey: cursor.primaryKey,
     90      value: cursor.value,
     91    });
     92  });
     93  return results;
     94 }