0

Just want to ask on how to update some data in extension's storage.

This is how I set the data in chrome.storage:

var storage = chrome.storage.local;
var obj = {},
clientInfo = [];

clientInfo.push({
    "name" : "alde",
    "age" : "21"
});

obj['clientInfo'] = clientInfo;
storage.set(obj,function(){});

I want to add another obj in ClientInfo array like this:

clientInfo.push({
   "name" : "another name",
   "age" : 33
});

EDITED

Currently, I still need to get the array to manipulate and save it back to update my storage, I just thought that maybe there's a simplest way to do it.

My problem is just that I have many js files running at the same time and can't append the object synchronously.

Xan
  • 74,770
  • 16
  • 179
  • 206
aldesabido
  • 1,268
  • 2
  • 17
  • 38
  • This is already answered in [how to remove data from a object using chrome storage?](http://stackoverflow.com/q/28952599/934239), the problem is essentially the same. – Xan Mar 12 '15 at 22:41
  • Specifically, you have to get the array, update it, and save it back. You can't manipulate on a finer level with Storage API. – Xan Mar 12 '15 at 22:42
  • @Xan - yes, I still need to get the array to manipulate and save it back, I just thought that there's a simplest way to do it. My problem is just that I have many js file running at the same time and can't append the object asynchronously. – aldesabido Mar 12 '15 at 22:47
  • 1
    Then there's this: http://stackoverflow.com/q/28917824/934239 but no good answer. You should update your question with information from your comment, it makes for a better question. – Xan Mar 12 '15 at 22:54
  • Please take a look at any localStorage documentation. – Zig Mandel Mar 13 '15 at 13:23
  • @ZigMandel Please take a look at the question itself and see that OP means `chrome.storage.local` (unfortunate question title) – Xan Mar 13 '15 at 15:33
  • Either way the question is answered on the official documentation. – Zig Mandel Mar 13 '15 at 15:35
  • @ZigMandel Strongly disagree. Neither point (race condition on read-writes, dealing with objects inside objects) is covered well by the docs. – Xan Mar 14 '15 at 14:25
  • There are no objects inside objects and in any case it doesnt make a differencr. Op just needs to learn async programming basics. Its not particular to storage. – Zig Mandel Mar 14 '15 at 14:36
  • Okay, fine, basics. Can you provide an adequate answer then? Conditions: your storage contains an array that you need to add data to. Several pages within the extension rapidly do that, leading to read-write race condition. Go. Basics! – Xan Mar 14 '15 at 14:41
  • @ZigMandel I cleaned up the question to make it reflect the problem. Seriously, let's wager. I pledge to give this question a 250 rep bounty (can't yet), can you make a good answer that deals with the synchronization problem? – Xan Mar 14 '15 at 14:47
  • its not a matter of points, your question is still unclear. for starters, javascript is single-threaded so you cant have "many js running at the same time". also, since you are keeping the property as a global "obj", the object is always up to date. Other than the fact that you are not handling storage api errors, the code should work ok. show a case where it doesnt. – Zig Mandel Mar 14 '15 at 16:17
  • until then all I can offer is that you look at this extension of mine. Uses local/sync storage extensively (sometimes using globals like your case) and does not have concurrency issues. https://github.com/zmandel/Plus-for-Trello/tree/master/source. If you need to execute code after the storage api finishes, put your code where you currently have an empty "function(){}" – Zig Mandel Mar 14 '15 at 16:30

1 Answers1

-1

Just rebuild your object properly and call storage.set() again with that new value.

  • 1
    Actually, I am using that one now but the problem is that I have many js file running at the same time and can't append the object asynchronously. – aldesabido Mar 12 '15 at 22:49
  • @aldesabido You mean "can't ... synchronously", probably. – Xan Mar 14 '15 at 14:47