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> { export default new class extends TypedEventTarget<EventMap> {
constructor () { constructor () {
super(); 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") { if (areaName !== "sync") {
return; return;
} }
// Types issue if ("options" in changes) {
const _changes = changes as { const { oldValue, newValue } = changes.options;
[key: string]: browser.storage.StorageChange
};
if ("options" in _changes) {
const { oldValue, newValue } = _changes.options;
const changedKeys = []; const changedKeys = [];
for (const key of Object.keys(newValue)) { for (const key of Object.keys(newValue)) {
if (oldValue) { if (oldValue) {
// Don't track added keys // Don't track added keys
@@ -70,9 +80,17 @@ export default new class extends TypedEventTarget<EventMap> {
const oldKeyValue = oldValue[key]; const oldKeyValue = oldValue[key];
const newKeyValue = newValue[key]; const newKeyValue = newValue[key];
// Equality comparison // Equality comparison
if (oldKeyValue === newKeyValue) { if (oldKeyValue === newKeyValue) {
continue; continue;
} }
// Array comparison // Array comparison
@@ -87,13 +105,14 @@ export default new class extends TypedEventTarget<EventMap> {
} }
changedKeys.push(key); changedKeys.push(key);
} }
this.dispatchEvent(new CustomEvent("changed", { this.dispatchEvent(new CustomEvent("changed", {
detail: changedKeys as Array<keyof Options> detail: changedKeys as Array<keyof Options>
})); }));
} }
});
} }
/** /**