Extensions can use a database: IndexedDB (the sample code may look convoluted, but it’s pretty simple in the actual extensions, for example two small functions here, getStyles and saveStyle, or IDB-keyval wrapper library).
If you want to use chrome.storage
, just maintain a global queue
array that is populated by the server listener:
queue.push(newItem);
updateStorage();
and processed in chrome.storage.local.get
callback:
function updateStorage() {
if (!queue.length || updateStorage.running) {
return;
}
updateStorage.running = true;
chrome.storage.local.get('commands', data => {
data.commands = [].concat(data.commands || [], queue);
queue = [];
chrome.storage.local.set(data, () => {
updateStorage.running = false;
if (queue.length) updateStorage();
});
});
}