Storage Set, Get an Listener issue

Hello,
I’m a beginner on Firefox adons and maybe i skipped some important steps…
i usually search on google for to find answers and solutions but this time i cant find my answer.
i have something like this:
on options page i set some storage variables
then on my background page on “storage change” i want to read storage variable…

is tired something like this:

background page:
var exclude = [];
var agents = [];
function onGot(item) {

agents = item[“agents”];
exclude = item[“exclude”];

}

function onError(error) {
console.log(‘Error: ${error}’);
}

browser.storage.onChanged.addListener(setUaString);

function setUaString() {
console.log(“xxxxxxxxxxxxxx”);

let gettingItem = browser.storage.sync.get([“agents”, “exclude”]);
gettingItem.then(onGot, onError);
}

i cant figure out why this is not working

my solution for now is to call another function on change :

function logStorageChange(changes, area){
agents = changes[‘agents’][‘newValue’];
exclude = changes[‘exclude’][‘newValue’];
}

but i’m afraid i will get into some issues later; for example if “agents” was not updated do i still get the value?

in my mind the ideea was simple: on any storage change read all parameters i need (not the changed ones)

thank you for you help

No, changes only contains keys for values that have been changed (or added or deleted). As such it allows you to atomically update your state (so check if the key exists, and if yes act on the affected parts of your state).

I assume “not working” here means that you are not observing the updated values as expected? Have you checked the flow using the debugger (so set a breakpoint in setUaString and one in onGot). I also assume onError never logs?
Lastly, are you setting the value in the sync storage, since that’s where you’re reading from?

thank you for you reply.
i made some changes checked everything you said still not working:

browser.storage.onChanged.addListener(updateData);
function updateData() {
console.log(“test”);
let gettingItem = browser.storage.sync.get([“agents”, “exclude”]);
gettingItem.then(onGot, onError);
}

function onGot(item) {
console.log(“test1”)
agents = item[“agents”];
exclude = item[“exclude”];
}
function onError(error) {
console.log(‘Error: ${error}’);
}

on update never get “test1” to cosole log; i get the “test” but noting after that… also no error

if i run this part
"let gettingItem = browser.storage.sync.get([“agents”, “exclude”]);
gettingItem.then(onGot, onError);
"
outside the updateData function (runs on background load only) it works as expected

thank you

found the soutions but still dont know why is not working:

replaced:
let gettingItem = browser.storage.sync.get([“agents”, “exclude”]);
gettingItem.then(onGot, onError);

with:
var gettingItem = browser.storage.sync.get([‘agents’,‘exclude’]);
gettingItem.then((res) => {
console.log(res.agents);
console.log(res.exclude);
console.log(“it works”);
});