diff options
Diffstat (limited to 'common/indexeddb.js')
-rw-r--r-- | common/indexeddb.js | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/common/indexeddb.js b/common/indexeddb.js index f916162..bdf71e5 100644 --- a/common/indexeddb.js +++ b/common/indexeddb.js @@ -99,9 +99,25 @@ async function idb_del(transaction, store_name, key) return wait_request(transaction.objectStore(store_name).delete(key)); } +async function perform_upgrade(event) { + const opened_db = event.target.result; + + /* When we move to a new database schema, we will add upgrade logic here. */ + if (event.oldVersion > 0) + throw "bad db version: " + event.oldVersion; + + let store; + for (const [store_name, key_mode] of stores) + store = opened_db.createObjectStore(store_name, key_mode); + + const ctx = make_context(store.transaction, initial_data.files); + await _save_items(initial_data.resources, initial_data.mappings, ctx); + + return opened_db; +} + /* Open haketilo database, asynchronously return an IDBDatabase object. */ -async function get_db() -{ +async function get_db() { if (db) return db; @@ -109,28 +125,11 @@ async function get_db() const waiter = new Promise((...cbs) => [resolve, reject] = cbs); const request = indexedDB.open("haketilo", version_nr(db_version)); - request.onsuccess = resolve; + request.onsuccess = ev => resolve(ev.target.result); request.onerror = ev => reject("db error: " + ev.target.errorCode); - request.onupgradeneeded = resolve; - - const event = await waiter; - const opened_db = event.target.result; + request.onupgradeneeded = ev => perform_upgrade(ev).then(resolve, reject); - if (event instanceof IDBVersionChangeEvent) { - /* - * When we move to a new database schema, we will add upgrade logic - * here. - */ - if (event.oldVersion > 0) - throw "bad db version: " + event.oldVersion; - - let store; - for (const [store_name, key_mode] of stores) - store = opened_db.createObjectStore(store_name, key_mode); - - const ctx = make_context(store.transaction, initial_data.files); - await _save_items(initial_data.resources, initial_data.mappings, ctx); - } + const opened_db = await waiter; if (db) opened_db.close(); |