summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/broadcast.js2
-rw-r--r--common/indexeddb.js43
2 files changed, 22 insertions, 23 deletions
diff --git a/common/broadcast.js b/common/broadcast.js
index 4fc5237..12b365a 100644
--- a/common/broadcast.js
+++ b/common/broadcast.js
@@ -119,6 +119,6 @@ function close(conn)
{
if (conn.type === "sender")
flush(conn);
- conn.port.disconnect();
+ setTimeout(conn.port.disconnect());
}
#EXPORT close
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();