aboutsummaryrefslogtreecommitdiff
path: root/common/indexeddb.js
diff options
context:
space:
mode:
Diffstat (limited to 'common/indexeddb.js')
-rw-r--r--common/indexeddb.js63
1 files changed, 29 insertions, 34 deletions
diff --git a/common/indexeddb.js b/common/indexeddb.js
index 1741c91..c97c115 100644
--- a/common/indexeddb.js
+++ b/common/indexeddb.js
@@ -41,13 +41,12 @@
* proprietary program, I am not going to enforce this in court.
*/
-/*
- * IMPORTS_START
- * IMPORT initial_data
- * IMPORT entities
- * IMPORT broadcast
- * IMPORTS_END
- */
+#IMPORT common/entities.js
+#IMPORT common/broadcast.js
+
+let initial_data = (
+#INCLUDE_VERBATIM default_settings.json
+);
/* Update when changes are made to database schema. Must have 3 elements */
const db_version = [1, 0, 0];
@@ -79,6 +78,7 @@ async function idb_get(transaction, store_name, key)
const req = transaction.objectStore(store_name).get(key);
return (await wait_request(req)).target.result;
}
+#EXPORT idb_get
/* asynchronous wrapper for IDBObjectStore's put() method. */
async function idb_put(transaction, store_name, object)
@@ -132,6 +132,7 @@ async function get_db()
return db;
}
+#EXPORT get_db AS get
/* Helper function used by make_context(). */
function reject_discard(context)
@@ -177,10 +178,11 @@ function make_context(transaction, files)
*/
async function start_items_transaction(item_store_names, files)
{
- const db = await haketilodb.get();
+ const db = await get_db();
const scope = [...item_store_names, "files", "file_uses"];
return make_context(db.transaction(scope, "readwrite"), files);
}
+#EXPORT start_items_transaction
async function incr_file_uses(context, file_ref, by=1)
{
@@ -242,6 +244,7 @@ async function finalize_items_transaction(context)
return context.result;
}
+#EXPORT finalize_items_transaction
/*
* How a sample data argument to the function below might look like:
@@ -287,6 +290,7 @@ async function save_items(data)
return _save_items(data.resources, data.mappings, context);
}
+#EXPORT save_items
async function _save_items(resources, mappings, context)
{
@@ -322,6 +326,7 @@ async function save_item(item, context)
await _remove_item(store_name, item.identifier, context, false);
await idb_put(context.transaction, store_name, item);
}
+#EXPORT save_item
/* Helper function used by remove_item() and save_item(). */
async function _remove_item(store_name, identifier, context)
@@ -348,11 +353,16 @@ async function remove_item(store_name, identifier, context)
await idb_del(context.transaction, store_name, identifier);
}
+const remove_resource = (id, ctx) => remove_item("resources", id, ctx);
+#EXPORT remove_resource
+
+const remove_mapping = (id, ctx) => remove_item("mappings", id, ctx);
+#EXPORT remove_mapping
+
/* Callback used when listening to broadcasts while tracking db changes. */
async function track_change(tracking, identifier)
{
- const transaction =
- (await haketilodb.get()).transaction([tracking.store_name]);
+ const transaction = (await get_db()).transaction([tracking.store_name]);
const new_val = await idb_get(transaction, tracking.store_name, identifier);
tracking.onchange({identifier, new_val});
@@ -373,7 +383,7 @@ async function track_change(tracking, identifier)
* }
*
* Returns a [tracking, all_current_items] array where `tracking` is an object
- * that can be later passed to haketilodb.untrack() to stop tracking changes and
+ * that can be later passed to untrack() to stop tracking changes and
* `all_current_items` is an array of items currently present in the object
* store.
*
@@ -388,33 +398,18 @@ async function track(store_name, onchange)
broadcast.listener_connection(msg => track_change(tracking, msg[1]));
broadcast.subscribe(tracking.listener, `idb_changes_${store_name}`);
- const transaction = (await haketilodb.get()).transaction([store_name]);
+ const transaction = (await get_db()).transaction([store_name]);
const all_req = transaction.objectStore(store_name).getAll();
return [tracking, (await wait_request(all_req)).target.result];
}
-function untrack(tracking)
-{
- broadcast.close(tracking.listener);
-}
+const track_resources = onchange => track("resources", onchange);
+#EXPORT track_resources
-const haketilodb = {
- get: get_db,
- save_items,
- save_item,
- remove_resource: (id, ctx) => remove_item("resources", id, ctx),
- remove_mapping: (id, ctx) => remove_item("mappings", id, ctx),
- start_items_transaction,
- finalize_items_transaction,
- track_resources: onchange => track("resources", onchange),
- track_mappings: onchange => track("mappings", onchange),
- untrack
-};
+const track_mappings = onchange => track("mappings", onchange);
+#EXPORT track_mappings
+
+const untrack = tracking => broadcast.close(tracking.listener);
+#EXPORT untrack
-/*
- * EXPORTS_START
- * EXPORT haketilodb
- * EXPORT idb_get
- * EXPORTS_END
- */