auto-unsubscribe prefs observer when element is gone

This commit is contained in:
tophf 2017-12-09 17:38:00 +03:00
parent ae2efaa4a1
commit 36fcb02b51

View File

@ -201,6 +201,24 @@ var prefs = new function Prefs() {
onChange.any.add(listener); 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 // Unlike sync, HTML5 localStorage is ready at browser startup
@ -394,6 +412,10 @@ function setupLivePrefs(
element = $('#' + id), element = $('#' + id),
force, force,
}) { }) {
if (!element) {
prefs.unsubscribe(IDs, updateElement);
return;
}
const prop = checkedProps[id]; const prop = checkedProps[id];
if (force || element[prop] !== value) { if (force || element[prop] !== value) {
element[prop] = value; element[prop] = value;