Reputation: 3608
I'm using a localStorage to store some stuff ( profile visits history ).
Every time profile is open, i get object, add more entries and update object. This can lead to data loss (example: tab1:open, tab2:open, tab1:save, tab2:save).
Now, if I hold cntrl and open many new tabs at once, how to pervent data loss? Didn't find anyhing about localStorage locking.
Upvotes: 0
Views: 1319
Reputation: 6221
Option 1.
Check and put a variable (e.g. editing
) to localStorage when a tab is opened. So if another tab is open, you can create a warning on new tab. Clear that variable onPageUnload
event. (Pessimistic Lock)
Option 2.
Update/put a variable (e.g. saveTime
or saveCount
) on save action. When a tab is opening, retrieve this variable and on save action check if the variable is remaning the same. If value is not the same, warn the user that he/she is editing an oldest version of the data. (Optimistic Lock)
Optimistic Lock vs Pessimistic Lock
Upvotes: 0
Reputation: 31
You can increment a variable instead of getting a whole new object, or push that new object into an array and calculate the total views getting the length of that array. The localStorage is shared across the different tabs as long as they are in the same domain.
Upvotes: 1