Revision 6247f163
Added by koszko about 2 years ago
common/observable.js | ||
---|---|---|
6 | 6 |
* Redistribution terms are gathered in the `copyright' file. |
7 | 7 |
*/ |
8 | 8 |
|
9 |
function make() |
|
10 |
{ |
|
11 |
return new Set(); |
|
12 |
} |
|
9 |
const make = (value=undefined) => ({value, listeners: new Set()}); |
|
10 |
const subscribe = (observable, cb) => observable.listeners.add(cb); |
|
11 |
const unsubscribe = (observable, cb) => observable.listeners.delete(cb); |
|
13 | 12 |
|
14 |
function subscribe(observable, cb) |
|
15 |
{ |
|
16 |
observable.add(cb); |
|
17 |
} |
|
18 |
|
|
19 |
function unsubscribe(observable, cb) |
|
20 |
{ |
|
21 |
observable.delete(cb); |
|
22 |
} |
|
13 |
const silent_set = (observable, value) => observable.value = value; |
|
14 |
const broadcast = (observable, ...values) => |
|
15 |
observable.listeners.forEach(cb => cb(...values)); |
|
23 | 16 |
|
24 |
function broadcast(observable, event)
|
|
17 |
function set(observable, value)
|
|
25 | 18 |
{ |
26 |
for (const callback of observable) |
|
27 |
callback(event); |
|
19 |
const old_value = observable.value; |
|
20 |
silent_set(observable, value); |
|
21 |
broadcast(observable, value, old_value); |
|
28 | 22 |
} |
29 | 23 |
|
30 |
const observables = {make, subscribe, unsubscribe, broadcast}; |
|
24 |
const observables = {make, subscribe, unsubscribe, broadcast, silent_set, set};
|
|
31 | 25 |
|
32 | 26 |
/* |
33 | 27 |
* EXPORTS_START |
Also available in: Unified diff
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).