Update object stored in chrome extension's local storage

DMkitten

I'm developing a chrome extension and I will store objects sent by server. For example, I will receive:

command = {id:"1", type: "A", size: "B", priority: "C"}

If I had a database, I would insert it as a line in table commands. Using chrome.storage, I'm storing an array of these object in key commands.

But, when I receive a new command by server, I have to get from local storage, update the array and then set again. I'm worried about cases when I receive another command while I'm getting and setting or while I delete a stored command. I'm thinking about semaphores, but I don't know if it's a great idea.

Can someone suggest me what to do?

thanks!

wOxxOm

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();
        });
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sharing a local storage object across a web-app and a Chrome extension

From Dev

Update chrome.storage if extension's file changes

From Dev

when chrome extension gets updated does the data stored in local storage gets deleted?

From Dev

Where are cookies and local storage stored for Chrome?

From Dev

Chrome Extension: Local Storage, how to export

From Dev

Chrome Extension: Local Storage, how to export

From Dev

chrome extension: switching from localstorage to chrome.storage.local

From Dev

Unable to pass an object to chrome local storage

From Dev

angular update local storage single object

From Dev

uploading file from local storage to server with a chrome extension

From Dev

Clear session and local storage for specific domain from a chrome extension

From Dev

Passing a local storage variable to a url and fetch the output of url in chrome extension

From Dev

Easy way to get Local Storage Value to Content Script [Chrome Extension]

From Dev

Is there API to get local storage, cache, indexed db statistics for chrome extension?

From Dev

Accessing local storage in CSS when using a chrome extension?

From Dev

Storage for chrome-extension

From Dev

How to update Google Chrome Developer Tools local storage view?

From Dev

chrome.storage for Chrome extension

From Dev

Can't view chrome extension local storage from developers tool but can access it from extension api

From Dev

chrome.storage.local strange behaviour: confused by a duplicate object

From Dev

How to clear `chrome.storage.local` cache when developing a Chrome Extension?

From Dev

How To Clear History & Cache in Google Chrome While Leaving Chrome Extension Local Storage alone?

From Dev

Chrome extension update flow

From Dev

Chrome extension cleans storage on uninstall

From Dev

How to update my Chrome Extension's tooltip or description?

From Dev

Save chrome extension options to chrome.storage?

From Dev

Save chrome extension options to chrome.storage?

From Dev

chrome storage local set not setting

From Dev

Saving Array to Chrome Local Storage

Related Related

  1. 1

    Sharing a local storage object across a web-app and a Chrome extension

  2. 2

    Update chrome.storage if extension's file changes

  3. 3

    when chrome extension gets updated does the data stored in local storage gets deleted?

  4. 4

    Where are cookies and local storage stored for Chrome?

  5. 5

    Chrome Extension: Local Storage, how to export

  6. 6

    Chrome Extension: Local Storage, how to export

  7. 7

    chrome extension: switching from localstorage to chrome.storage.local

  8. 8

    Unable to pass an object to chrome local storage

  9. 9

    angular update local storage single object

  10. 10

    uploading file from local storage to server with a chrome extension

  11. 11

    Clear session and local storage for specific domain from a chrome extension

  12. 12

    Passing a local storage variable to a url and fetch the output of url in chrome extension

  13. 13

    Easy way to get Local Storage Value to Content Script [Chrome Extension]

  14. 14

    Is there API to get local storage, cache, indexed db statistics for chrome extension?

  15. 15

    Accessing local storage in CSS when using a chrome extension?

  16. 16

    Storage for chrome-extension

  17. 17

    How to update Google Chrome Developer Tools local storage view?

  18. 18

    chrome.storage for Chrome extension

  19. 19

    Can't view chrome extension local storage from developers tool but can access it from extension api

  20. 20

    chrome.storage.local strange behaviour: confused by a duplicate object

  21. 21

    How to clear `chrome.storage.local` cache when developing a Chrome Extension?

  22. 22

    How To Clear History & Cache in Google Chrome While Leaving Chrome Extension Local Storage alone?

  23. 23

    Chrome extension update flow

  24. 24

    Chrome extension cleans storage on uninstall

  25. 25

    How to update my Chrome Extension's tooltip or description?

  26. 26

    Save chrome extension options to chrome.storage?

  27. 27

    Save chrome extension options to chrome.storage?

  28. 28

    chrome storage local set not setting

  29. 29

    Saving Array to Chrome Local Storage

HotTag

Archive