Skip to content

Commit

Permalink
Add OptionsSync#onChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Nov 17, 2024
1 parent 9568946 commit 9b5020a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ class OptionsSync<UserOptions extends Options> {
await saveFile(text, extension.name + ' options.json');
};

onChanged(callback: (options: UserOptions, oldOptions: UserOptions) => void, signal?: AbortSignal): void {
const onChanged = (changes: Record<string, chrome.storage.StorageChange>, area: chrome.storage.AreaName) => {
const data = changes[this.storageName];
if (data && area === this.storageType) {
callback(
this._decode(data.newValue as string),
this._decode(data.oldValue as string ?? {}),
);
}
};

chrome.storage.onChanged.addListener(onChanged);
signal?.addEventListener('abort', () => {
chrome.storage.onChanged.removeListener(onChanged);
});
}

private _log(method: 'log' | 'info', ...arguments_: unknown[]): void {
console[method](...arguments_);
}
Expand Down

0 comments on commit 9b5020a

Please sign in to comment.