prefs.subscribe: allow multiple listeners for one key

This commit is contained in:
tophf 2017-11-16 11:45:48 +03:00
parent 0ded91d987
commit 10ce4eeefa

View File

@ -136,10 +136,14 @@ var prefs = new function Prefs() {
}
}
if (hasChanged) {
const listener = onChange.specific.get(key);
if (listener) {
const specific = onChange.specific.get(key);
if (typeof specific === 'function') {
specific(key, value);
} else if (specific instanceof Set) {
for (const listener of specific.values()) {
listener(key, value);
}
}
for (const listener of onChange.any.values()) {
listener(key, value);
}
@ -164,7 +168,14 @@ var prefs = new function Prefs() {
// listener: function (key, value)
if (keys) {
for (const key of keys) {
const existing = onChange.specific.get(key);
if (!existing) {
onChange.specific.set(key, listener);
} else if (existing instanceof Set) {
existing.add(listener);
} else {
onChange.specific.set(key, new Set([existing, listener]));
}
}
} else {
onChange.any.add(listener);