diff options
author | Wojtek Kosior <koszko@koszko.org> | 2021-07-23 17:32:31 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2021-07-23 17:32:31 +0200 |
commit | d42dadca04bd2c40cfe836b3f2bbe0ffbf176eda (patch) | |
tree | 66150ce87658c2d8cc931ade95b959712496c9f4 /background | |
parent | c483ae19e110ef5c1e539883a38fbc79b3dd4e4e (diff) | |
download | browser-extension-d42dadca04bd2c40cfe836b3f2bbe0ffbf176eda.tar.gz browser-extension-d42dadca04bd2c40cfe836b3f2bbe0ffbf176eda.zip |
extract observables implementation from storage.js
Diffstat (limited to 'background')
-rw-r--r-- | background/storage.js | 24 |
1 files changed, 10 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(); |