Skip to content

Commit

Permalink
Closes #2517
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Jan 17, 2025
1 parent 1ee4525 commit cd87145
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions frontend/model/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,43 @@ const localforage = {
reject(new Error('Unsupported characters in name: -'))
return
}
const request = self.indexedDB.open(name + '--' + storeName)

// Create the object store if it doesn't exist
request.onupgradeneeded = (event) => {
const db = event.target.result
db.createObjectStore(storeName)
const openDB = (version?: number) => {
// By default `version` is the latest DB version. Initially, we
// try to open that, but in some cases (e.g., when manually
// deleting the DBs), the schema will be wrong and miss the object
// store. In these cases, we need to upgrade the DB by
// incrementing the version number to re-create the schema, which
// can only be done when the DB is being 'upgraded'.
const request = self.indexedDB.open(name + '--' + storeName, version)

// Create the object store if it doesn't exist
request.onupgradeneeded = (event) => {
const db = event.target.result
db.createObjectStore(storeName)
}

request.onsuccess = (event) => {
const db = event.target.result
if (!db.objectStoreNames.contains(storeName)) {
return openDB(db.version + 1)
}

resolve(db)
}

request.onerror = (error) => {
reject(error)
}

// If this happens, closing all tabs and stopping the SW could
// help.
request.onblocked = (event) => {
reject(new Error('DB is blocked'))
}
}

request.onsuccess = (event) => {
const db = event.target.result
resolve(db)
}

request.onerror = (error) => {
reject(error)
}

request.onblocked = (event) => {
reject(new Error('DB is blocked'))
}
openDB()
})
}
return promise
Expand Down

0 comments on commit cd87145

Please sign in to comment.