I am trying to write a Chrome extension that involves using chrome’s unlimited storage.
I am currently retrieving from storage like this:
chrome.storage.local.get(null, function (items) {
return items;
}
And then saving back to storage like this:
chrome.storage.local.set(items);
My problem is that in this case ‘items’ is an associative array and I want to be able to add multiple values to this array simultaneously. Because the get method is asynchronous, I can’t guarantee that two different processes trying to call get the array from storage, add a key/value pair to it and then set it back will result in both being saved. Even worse, doing so appears to crash the extension if there are enough simultaneous processes.
Is there a better way to use chrome.storage? Or do I have to implement some kind of queueing system? I was previously using localStorage which doesn’t have the asynchronous aspect, but was exceeding the maximum storage capacity so am trying to switch to using unlimited storage.
I would go ahead with the queuing system you have in mind. If both of the processes have access to a global object, have that object manage the writes and have the rest of your application to write using that object instead of
chrome.local.storage.set(...).