|
123456789101112131415161718192021222324252627 |
- /* Augments the WebExtension storage API to allow listening for changes on a
- * single variable of a single storage area (i.e. local or sync).
- *
- * Usage example:
- * const storage = listenableStorage(browser.storage)
- * storage.local.onChanged('someVar', newValue => { .... })
- */
-
- export default storage => {
- const mkOnChange = storageName => {
- return (key, cb) => {
- const listener = (changes, areaName) => {
- if (changes[key]) {
- const newValue = changes[key].newValue
- cb(newValue)
- }
- }
- storage.onChanged.addListener(listener)
- }
- }
-
- return storage && ({
- ...storage,
- local: storage.local && { ...storage.local, onChanged: mkOnChange('local') },
- sync: storage.sync && { ...storage.sync, onChanged: mkOnChange('sync') },
- })
- }
|