Skip to content

Commit

Permalink
fix: return true if either cache or idb entry is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiVandivier committed Aug 23, 2021
1 parent f6242d1 commit 43d8001
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pwa/src/offline-interface/offline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class OfflineInterface {
/**
* Removes a specified section from the IndexedDB and CacheStorage cache.
* @param {String} sectionId - ID of the section to remove
* @returns {Promise} A promise that resolves to `true` if the section is successfully deleted or `false` if it was not found.
* @returns {Promise} A promise that resolves to `true` if at least one of the cache or the idb entry are deleted or `false` if neither were found.
*/
async removeSection(sectionId) {
if (!this.pwaEnabled)
Expand All @@ -203,14 +203,20 @@ export class OfflineInterface {
if (!sectionId) {
throw new Error('No section ID specified to delete')
}

await navigator.serviceWorker.ready
if (this.dbPromise === undefined) {
this.dbPromise = openSectionsDB()
}
// todo: feedback if one but not the other is removed
const db = await this.dbPromise

const sectionExists = await db.count(SECTIONS_STORE, sectionId)
return Promise.all([
caches.delete(sectionId),
(await this.dbPromise).delete(SECTIONS_STORE, sectionId),
]).then(([cacheDeleted]) => cacheDeleted)
!!sectionExists &&
db.delete(SECTIONS_STORE, sectionId).then(() => true),
]).then(
([cacheDeleted, dbEntryDeleted]) => cacheDeleted || dbEntryDeleted
)
}
}

0 comments on commit 43d8001

Please sign in to comment.