diff options
author | Wojtek Kosior <koszko@koszko.org> | 2021-09-02 18:35:49 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2021-09-02 18:35:49 +0200 |
commit | 6247f163d3ca89d5570450ac7ac8fd18f73bb74b (patch) | |
tree | e3d4817ae475e1f3553d3a50a77792fc5c3c25a0 /common/observable.js | |
parent | 4b59dced912fb9b50ff041c67f0f72cbbad56b6c (diff) | |
download | browser-extension-6247f163d3ca89d5570450ac7ac8fd18f73bb74b.tar.gz browser-extension-6247f163d3ca89d5570450ac7ac8fd18f73bb74b.zip |
enable toggling of global script blocking policy\n\nThis commit also introduces `light_storage' module which is later going to replace the storage code we use right now.\nAlso included is a hack to properly display scrollbars under Mozilla (needs testing on newer Mozilla browsers).
Diffstat (limited to 'common/observable.js')
-rw-r--r-- | common/observable.js | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/common/observable.js b/common/observable.js index 1fb0b0a..02f1c1b 100644 --- a/common/observable.js +++ b/common/observable.js @@ -6,28 +6,22 @@ * Redistribution terms are gathered in the `copyright' file. */ -function make() -{ - return new Set(); -} +const make = (value=undefined) => ({value, listeners: new Set()}); +const subscribe = (observable, cb) => observable.listeners.add(cb); +const unsubscribe = (observable, cb) => observable.listeners.delete(cb); -function subscribe(observable, cb) -{ - observable.add(cb); -} - -function unsubscribe(observable, cb) -{ - observable.delete(cb); -} +const silent_set = (observable, value) => observable.value = value; +const broadcast = (observable, ...values) => + observable.listeners.forEach(cb => cb(...values)); -function broadcast(observable, event) +function set(observable, value) { - for (const callback of observable) - callback(event); + const old_value = observable.value; + silent_set(observable, value); + broadcast(observable, value, old_value); } -const observables = {make, subscribe, unsubscribe, broadcast}; +const observables = {make, subscribe, unsubscribe, broadcast, silent_set, set}; /* * EXPORTS_START |