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,21 +45,31 @@ interface EventMap {
export default new class extends TypedEventTarget<EventMap> {
constructor () {
super();
this.onStorageChanged = this.onStorageChanged.bind(this);
browser.storage.onChanged.addListener(this.onStorageChanged);
// Supresses sendRemoveListener closed conduit error
window.addEventListener("unload", () => {
browser.storage.onChanged.removeListener(this.onStorageChanged);
});
}
private onStorageChanged(
changes: { [key: string]: browser.storage.StorageChange }
, areaName: string) {
browser.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== "sync") {
return;
}
// Types issue
const _changes = changes as {
[key: string]: browser.storage.StorageChange
};
if ("options" in _changes) {
const { oldValue, newValue } = _changes.options;
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
@@ -70,9 +80,17 @@ export default new class extends TypedEventTarget<EventMap> {
const oldKeyValue = oldValue[key];
const newKeyValue = newValue[key];
// Equality comparison
if (oldKeyValue === newKeyValue) {
continue;
}
// Array comparison
@@ -87,13 +105,14 @@ export default new class extends TypedEventTarget<EventMap> {
}
changedKeys.push(key);
}
this.dispatchEvent(new CustomEvent("changed", {
detail: changedKeys as Array<keyof Options>
}));
}
});
}
/**