async expireEntries()

in workbox-v4.3.1/workbox-expiration.dev.js [121:172]


    async expireEntries(minTimestamp, maxCount) {
      const entriesToDelete = await this._db.transaction(OBJECT_STORE_NAME, 'readwrite', (txn, done) => {
        const store = txn.objectStore(OBJECT_STORE_NAME);
        const entriesToDelete = [];
        let entriesNotDeletedCount = 0;

        store.index('timestamp').openCursor(null, 'prev').onsuccess = ({
          target
        }) => {
          const cursor = target.result;

          if (cursor) {
            const result = cursor.value; // TODO(philipwalton): once we can use a multi-key index, we
            // won't have to check `cacheName` here.

            if (result.cacheName === this._cacheName) {
              // Delete an entry if it's older than the max age or
              // if we already have the max number allowed.
              if (minTimestamp && result.timestamp < minTimestamp || maxCount && entriesNotDeletedCount >= maxCount) {
                // TODO(philipwalton): we should be able to delete the
                // entry right here, but doing so causes an iteration
                // bug in Safari stable (fixed in TP). Instead we can
                // store the keys of the entries to delete, and then
                // delete the separate transactions.
                // https://github.com/GoogleChrome/workbox/issues/1978
                // cursor.delete();
                // We only need to return the URL, not the whole entry.
                entriesToDelete.push(cursor.value);
              } else {
                entriesNotDeletedCount++;
              }
            }

            cursor.continue();
          } else {
            done(entriesToDelete);
          }
        };
      }); // TODO(philipwalton): once the Safari bug in the following issue is fixed,
      // we should be able to remove this loop and do the entry deletion in the
      // cursor loop above:
      // https://github.com/GoogleChrome/workbox/issues/1978

      const urlsDeleted = [];

      for (const entry of entriesToDelete) {
        await this._db.delete(OBJECT_STORE_NAME, entry.id);
        urlsDeleted.push(entry.url);
      }

      return urlsDeleted;
    }