From 10ce4eeefa34898920ec787dc900814839be8c8c Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 16 Nov 2017 11:45:48 +0300 Subject: [PATCH] prefs.subscribe: allow multiple listeners for one key --- js/prefs.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/js/prefs.js b/js/prefs.js index 59735db2..20903389 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -136,9 +136,13 @@ var prefs = new function Prefs() { } } if (hasChanged) { - const listener = onChange.specific.get(key); - if (listener) { - listener(key, value); + 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) { - onChange.specific.set(key, listener); + 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);