add closeDatabase

This commit is contained in:
Nolan Lawson 2020-05-08 16:22:34 -07:00
parent ea9310cb62
commit bfa7c1af14
3 changed files with 17 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { dbPromise, openDatabase } from './databaseLifecycle'
import { closeDatabase, dbPromise, openDatabase } from './databaseLifecycle'
import {
DATA_VERSION_CURRENT,
DB_NAME,
@ -74,4 +74,8 @@ export class IndexedDBEngine {
emojiStore.get(unicode).onsuccess = e => cb(e.target.result)
})
}
close () {
return closeDatabase(DB_NAME)
}
}

View File

@ -48,3 +48,8 @@ export async function getEmojiByUnicode (unicode) {
await init()
return idbEngine.getEmojiByUnicode(unicode)
}
export async function closeDatabase () {
await init()
return idbEngine.close()
}

View File

@ -57,12 +57,15 @@ export async function dbPromise (db, storeName, readOnlyOrReadWrite, cb) {
})
}
export function closeDatabase (instanceName) {
export function closeDatabase (dbName) {
if (!dbName) {
throw new Error('dbName is required')
}
// close any open requests
const openReq = openReqs[instanceName]
const openReq = openReqs[dbName]
if (openReq && openReq.result) {
openReq.result.close()
}
delete openReqs[instanceName]
delete databaseCache[instanceName]
delete openReqs[dbName]
delete databaseCache[dbName]
}