Remove storage onChanged listener on unload to fix console spam

This commit is contained in:
hensm
2021-04-25 05:16:23 +01:00
parent 01dc026745
commit 15c6b66dee

View File

@@ -45,55 +45,74 @@ interface EventMap {
export default new class extends TypedEventTarget<EventMap> { export default new class extends TypedEventTarget<EventMap> {
constructor () { constructor () {
super(); super();
this.onStorageChanged = this.onStorageChanged.bind(this);
browser.storage.onChanged.addListener(this.onStorageChanged);
browser.storage.onChanged.addListener((changes, areaName) => { // Supresses sendRemoveListener closed conduit error
if (areaName !== "sync") { window.addEventListener("unload", () => {
return; browser.storage.onChanged.removeListener(this.onStorageChanged);
} });
}
// Types issue private onStorageChanged(
const _changes = changes as { changes: { [key: string]: browser.storage.StorageChange }
[key: string]: browser.storage.StorageChange , areaName: string) {
};
if ("options" in _changes) {
const { oldValue, newValue } = _changes.options;
const changedKeys = [];
for (const key of Object.keys(newValue)) { if (areaName !== "sync") {
if (oldValue) { return;
// Don't track added keys }
if (!(key in oldValue)) {
continue;
}
const oldKeyValue = oldValue[key]; if ("options" in changes) {
const newKeyValue = newValue[key]; const { oldValue, newValue } = changes.options;
const changedKeys = [];
// Equality comparison
if (oldKeyValue === newKeyValue) {
continue;
}
// Array comparison
if (oldKeyValue instanceof Array
&& newKeyValue instanceof Array) { for (const key of Object.keys(newValue)) {
if (oldKeyValue.length === newKeyValue.length if (oldValue) {
&& oldKeyValue.every((value, index) => // Don't track added keys
value === newKeyValue[index])) { if (!(key in oldValue)) {
continue; continue;
}
}
} }
changedKeys.push(key); const oldKeyValue = oldValue[key];
const newKeyValue = newValue[key];
// Equality comparison
if (oldKeyValue === newKeyValue) {
continue;
}
// Array comparison
if (oldKeyValue instanceof Array
&& newKeyValue instanceof Array) {
if (oldKeyValue.length === newKeyValue.length
&& oldKeyValue.every((value, index) =>
value === newKeyValue[index])) {
continue;
}
}
} }
this.dispatchEvent(new CustomEvent("changed", { changedKeys.push(key);
detail: changedKeys as Array<keyof Options>
}));
} }
});
this.dispatchEvent(new CustomEvent("changed", {
detail: changedKeys as Array<keyof Options>
}));
}
} }
/** /**