From 36fcb02b51a3c820f81738b1f7781af1e58de767 Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 9 Dec 2017 17:38:00 +0300 Subject: [PATCH] auto-unsubscribe prefs observer when element is gone --- js/prefs.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/js/prefs.js b/js/prefs.js index 080b3a6a..5e8314d5 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -201,6 +201,24 @@ var prefs = new function Prefs() { onChange.any.add(listener); } }, + + unsubscribe(keys, listener) { + if (keys) { + for (const key of keys) { + const existing = onChange.specific.get(key); + if (existing instanceof Set) { + existing.delete(listener); + if (!existing.size) { + onChange.specific.delete(key); + } + } else if (existing) { + onChange.specific.delete(key); + } + } + } else { + onChange.all.remove(listener); + } + }, }); // Unlike sync, HTML5 localStorage is ready at browser startup @@ -394,6 +412,10 @@ function setupLivePrefs( element = $('#' + id), force, }) { + if (!element) { + prefs.unsubscribe(IDs, updateElement); + return; + } const prop = checkedProps[id]; if (force || element[prop] !== value) { element[prop] = value;