the purpose of this function is to add data to an object saved in chrome.storage.local
; I am implementing this function:
/**
* Set object setting
*
* @param {object} setting
*/
function addSetting(setting) {
chrome.storage.local.set({ atsu_setting: setting });
}
Then in different areas of the code I try to add more data to what already exists:
chrome.storage.local.set({ atsu_setting: {opened:false} });
chrome.storage.local.set({ atsu_setting: {url:"dato"} });
The problem is that I am monitoring the data they are saving with:
setInterval(()=>{
chrome.storage.local.get(atsu)
.then((setting) => {
console.log(setting);
});
}, 10000);
but this only shows me the last saved; They told me that this is because I have to obtain the information, merge it and then save it again; this implies that I must use async; therefore do this:
chrome.storage.local.set({ atsu_setting: {opened:false} });
metodo_siguiente();
You will have unwanted behavior; since the asynchronization that I know asks me to implement something like this; causing that in each memory read you need to nest code inside a.then
.then((setting) => {
metodo_siguiente();
})
How do I resolve/improve Data Update? I am needing to read the data; save, the new ones and return this data to a variable:
Example:
function udateSetting(objeto){
chrome.storage.local.get(atsu_setting)
.then((setting) => {
//validar que existe:
if(setting){
//agregarmos nuevos datos
setting[Object.keys(objeto)]='dato';
chrome.storage.local.set(setting);
return setting;
}
});
}
let new_setting = udateSetting({nueva_propiedad:"dato de la propiedad"});
console.log(new_setting);
When working with
chrome.storage.local
up to now, you cannot perform direct updates on the information withchrome.storage.local.set(dato);
; must be done in the following way:chrome.storage.local.get(dato)
.dato.propiedad = 'nuevo dato';
.chrome.storage.local.set(dato);
.This would be an example of how to get the data and Save it again (Updated):
This would be an example of how a slightly more complex and dynamic data or node can be altered: https://es.stackoverflow.com/a/494185/46896