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> {
constructor () {
super();
this.onStorageChanged = this.onStorageChanged.bind(this);
browser.storage.onChanged.addListener(this.onStorageChanged);
browser.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== "sync") {
return;
}
// Supresses sendRemoveListener closed conduit error
window.addEventListener("unload", () => {
browser.storage.onChanged.removeListener(this.onStorageChanged);
});
}
// Types issue
const _changes = changes as {
[key: string]: browser.storage.StorageChange
};
private onStorageChanged(
changes: { [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 (oldValue) {
// Don't track added keys
if (!(key in oldValue)) {
continue;
}
if (areaName !== "sync") {
return;
}
const oldKeyValue = oldValue[key];
const newKeyValue = newValue[key];
if ("options" in changes) {
const { oldValue, newValue } = changes.options;
const changedKeys = [];
// 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;
}
}
for (const key of Object.keys(newValue)) {
if (oldValue) {
// Don't track added keys
if (!(key in oldValue)) {
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", {
detail: changedKeys as Array<keyof Options>
}));
changedKeys.push(key);
}
});
this.dispatchEvent(new CustomEvent("changed", {
detail: changedKeys as Array<keyof Options>
}));
}
}
/**