summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-07-23 17:32:31 +0200
committerWojtek Kosior <koszko@koszko.org>2021-07-23 17:32:31 +0200
commitd42dadca04bd2c40cfe836b3f2bbe0ffbf176eda (patch)
tree66150ce87658c2d8cc931ade95b959712496c9f4
parentc483ae19e110ef5c1e539883a38fbc79b3dd4e4e (diff)
downloadbrowser-extension-d42dadca04bd2c40cfe836b3f2bbe0ffbf176eda.tar.gz
browser-extension-d42dadca04bd2c40cfe836b3f2bbe0ffbf176eda.zip
extract observables implementation from storage.js
-rw-r--r--background/storage.js24
-rw-r--r--common/observable.js36
2 files changed, 46 insertions, 14 deletions
diff --git a/background/storage.js b/background/storage.js
index 682f933..c2160b0 100644
--- a/background/storage.js
+++ b/background/storage.js
@@ -16,6 +16,7 @@
* IMPORT make_once
* IMPORT browser
* IMPORT is_chrome
+ * IMPORT observables
* IMPORTS_END
*/
@@ -98,7 +99,8 @@ async function list(prefix)
for (let item of await get_list_var(name))
map.set(item, await get(prefix + item));
- return {map, prefix, name, listeners : new Set(), lock : make_lock()};
+ return {map, prefix, name, observable: observables.make(),
+ lock: make_lock()};
}
var list_by_prefix = {};
@@ -121,7 +123,7 @@ exports.add_change_listener = function (cb, prefixes=list_prefixes)
prefixes = [prefixes];
for (let prefix of prefixes)
- list_by_prefix[prefix].listeners.add(cb);
+ observables.subscribe(list_by_prefix[prefix].observable, cb);
}
exports.remove_change_listener = function (cb, prefixes=list_prefixes)
@@ -130,13 +132,7 @@ exports.remove_change_listener = function (cb, prefixes=list_prefixes)
prefixes = [prefixes];
for (let prefix of prefixes)
- list_by_prefix[prefix].listeners.delete(cb);
-}
-
-function broadcast_change(change, list)
-{
- for (let listener_callback of list.listeners)
- listener_callback(change);
+ observables.unsubscribe(list_by_prefix[prefix].observable, cb);
}
/* Prepare some hepler functions to get elements of a list */
@@ -198,7 +194,7 @@ async function _set_item(item, value, list)
new_val : value
};
- broadcast_change(change, list);
+ observables.broadcast(list.observable, change);
return old_val;
}
@@ -236,7 +232,7 @@ async function _remove_item(item, list)
new_val : undefined
};
- broadcast_change(change, list);
+ observables.broadcast(list.observable, change);
return old_val;
}
@@ -282,7 +278,7 @@ async function _replace_item(old_item, new_item, list, new_val=undefined)
new_val : undefined
};
- broadcast_change(change, list);
+ observables.broadcast(list.observable, change);
list.map.set(new_item, new_val);
@@ -290,7 +286,7 @@ async function _replace_item(old_item, new_item, list, new_val=undefined)
change.old_val = undefined;
change.new_val = new_val;
- broadcast_change(change, list);
+ observables.broadcast(list.observable, change);
return old_val;
}
@@ -375,7 +371,7 @@ exports.clear = async function ()
for (let [item, val] of list_entries_it(list)) {
change.item = item;
change.old_val = val;
- broadcast_change(change, list);
+ observables.broadcast(list.observable, change);
}
list.map = new Map();
diff --git a/common/observable.js b/common/observable.js
new file mode 100644
index 0000000..1fb0b0a
--- /dev/null
+++ b/common/observable.js
@@ -0,0 +1,36 @@
+/**
+ * part of Hachette
+ * Facilitate listening to events
+ *
+ * Copyright (C) 2021 Wojtek Kosior
+ * Redistribution terms are gathered in the `copyright' file.
+ */
+
+function make()
+{
+ return new Set();
+}
+
+function subscribe(observable, cb)
+{
+ observable.add(cb);
+}
+
+function unsubscribe(observable, cb)
+{
+ observable.delete(cb);
+}
+
+function broadcast(observable, event)
+{
+ for (const callback of observable)
+ callback(event);
+}
+
+const observables = {make, subscribe, unsubscribe, broadcast};
+
+/*
+ * EXPORTS_START
+ * EXPORT observables
+ * EXPORTS_END
+ */