aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/indexeddb.js60
1 files changed, 33 insertions, 27 deletions
diff --git a/common/indexeddb.js b/common/indexeddb.js
index 371d491..cf38d23 100644
--- a/common/indexeddb.js
+++ b/common/indexeddb.js
@@ -59,13 +59,13 @@ const nr_reductor = ([i, s], num) => [i - 1, s + num * 1024 ** i];
const version_nr = ver => ver.slice(0, 3).reduce(nr_reductor, [2, 0])[1];
const stores = [
- ["files", {keyPath: "sha256"}],
+ ["file", {keyPath: "sha256"}],
["file_uses", {keyPath: "sha256"}],
["resource", {keyPath: "identifier"}],
["mapping", {keyPath: "identifier"}],
- ["settings", {keyPath: "name"}],
+ ["setting", {keyPath: "name"}],
["blocking", {keyPath: "pattern"}],
- ["repos", {keyPath: "url"}]
+ ["repo", {keyPath: "url"}]
];
let db = null;
@@ -111,7 +111,7 @@ async function perform_upgrade(event) {
store = opened_db.createObjectStore(store_name, key_mode);
const ctx = make_context(store.transaction, initial_data.file);
- await _save_items(initial_data.resources, initial_data.mappings, ctx);
+ await _save_items(initial_data.resource, initial_data.mapping, [], ctx);
return opened_db;
}
@@ -186,7 +186,7 @@ function make_context(transaction, files)
async function start_items_transaction(item_store_names, files)
{
const db = await get_db();
- const scope = [...item_store_names, "files", "file_uses"];
+ const scope = [...item_store_names, "file", "file_uses"];
return make_context(db.transaction(scope, "readwrite"), files);
}
#EXPORT start_items_transaction
@@ -226,7 +226,7 @@ async function finalize_transaction(context)
if (uses.uses < 1) {
if (!is_new) {
idb_del(context.transaction, "file_uses", sha256);
- idb_del(context.transaction, "files", sha256);
+ idb_del(context.transaction, "file", sha256);
}
continue;
@@ -246,7 +246,7 @@ async function finalize_transaction(context)
throw "file not present: " + sha256;
}
- idb_put(context.transaction, "files", {sha256, contents: file});
+ idb_put(context.transaction, "file", {sha256, contents: file});
}
return context.result;
@@ -257,7 +257,7 @@ async function finalize_transaction(context)
* How a sample data argument to the function below might look like:
*
* data = {
- * resources: {
+ * resource: {
* "resource1": {
* "1": {
* // some stuff
@@ -272,7 +272,7 @@ async function finalize_transaction(context)
* }
* },
* },
- * mappings: {
+ * mapping: {
* "mapping1": {
* "2": {
* // some stuff
@@ -295,13 +295,16 @@ async function finalize_transaction(context)
async function save_items(data)
{
const item_store_names = ["resource", "mapping"];
+ if ("repo" in data)
+ item_store_names.push("repo");
+
const context = await start_items_transaction(item_store_names, data.file);
- return _save_items(data.resources, data.mappings, context);
+ return _save_items(data.resource, data.mapping, data.repo || [], context);
}
#EXPORT save_items
-async function _save_items(resources, mappings, context)
+async function _save_items(resources, mappings, repos, context)
{
resources = Object.values(resources || {}).map(entities.get_newest);
mappings = Object.values(mappings || {}).map(entities.get_newest);
@@ -309,6 +312,9 @@ async function _save_items(resources, mappings, context)
for (const item of resources.concat(mappings))
await save_item(item, context);
+ for (const repo_url of repos)
+ await idb_put(context.transaction, "repo", {url: repo_url});
+
await finalize_transaction(context);
}
@@ -378,7 +384,7 @@ async function get_all(store_name)
/*
* A simplified kind of transaction for modifying stores without special
- * inter-store integrity constraints ("settings", "blocking", "repos").
+ * inter-store integrity constraints ("setting", "blocking", "repo").
*/
async function start_simple_transaction(store_name)
{
@@ -386,20 +392,20 @@ async function start_simple_transaction(store_name)
return make_context(db.transaction(store_name, "readwrite"), {});
}
-/* Functions to access the "settings" store. */
+/* Functions to access the "setting" store. */
async function set_setting(name, value)
{
- const context = await start_simple_transaction("settings");
- broadcast.prepare(context.sender, "idb_changes_settings", name);
- await idb_put(context.transaction, "settings", {name, value});
+ const context = await start_simple_transaction("setting");
+ broadcast.prepare(context.sender, "idb_changes_setting", name);
+ await idb_put(context.transaction, "setting", {name, value});
return finalize_transaction(context);
}
#EXPORT set_setting
async function get_setting(name)
{
- const transaction = (await get_db()).transaction("settings");
- return ((await idb_get(transaction, "settings", name)) || {}).value;
+ const transaction = (await get_db()).transaction("setting");
+ return ((await idb_get(transaction, "setting", name)) || {}).value;
}
#EXPORT get_setting
@@ -429,15 +435,15 @@ async function get_allowing(pattern)
}
#EXPORT get_allowing
-/* Functions to access the "repos" store. */
+/* Functions to access the "repo" store. */
async function set_repo(url, remove=false)
{
- const context = await start_simple_transaction("repos");
- broadcast.prepare(context.sender, "idb_changes_repos", url);
+ const context = await start_simple_transaction("repo");
+ broadcast.prepare(context.sender, "idb_changes_repo", url);
if (remove)
- await idb_del(context.transaction, "repos", url);
+ await idb_del(context.transaction, "repo", url);
else
- await idb_put(context.transaction, "repos", {url});
+ await idb_put(context.transaction, "repo", {url});
return finalize_transaction(context);
}
#EXPORT set_repo
@@ -445,7 +451,7 @@ async function set_repo(url, remove=false)
const del_repo = url => set_repo(url, true);
#EXPORT del_repo
-const get_repos = () => get_all("repos").then(list => list.map(obj => obj.url));
+const get_repos = () => get_all("repo").then(list => list.map(obj => obj.url));
#EXPORT get_repos
/* Callback used when listening to broadcasts while tracking db changes. */
@@ -460,8 +466,8 @@ async function track_change(tracking, key)
/*
* Monitor changes to `store_name` IndexedDB object store.
*
- * `store_name` should be either "resource", "mapping", "settings", "blocking"
- * or "repos".
+ * `store_name` should be either "resource", "mapping", "setting", "blocking"
+ * or "repo".
*
* `onchange` should be a callback that will be called when an item is added,
* modified or removed from the store. The callback will be passed an object
@@ -492,7 +498,7 @@ async function start_tracking(store_name, onchange)
}
const track = {};
-const trackable = ["resource", "mapping", "settings", "blocking", "repos"];
+const trackable = ["resource", "mapping", "setting", "blocking", "repo"];
for (const store_name of trackable)
track[store_name] = onchange => start_tracking(store_name, onchange);
#EXPORT track