I'm working on a chrome extension that lets users create video annotations. I render an iframe with a file system to help the use sort their files. The representation of the file system in chrome.storage is the following:
const storage = {
"ROOT": {
files: [],
folders: [{id: "folder_id", name: "Folder 1"}]
},
"folder_id": {
files: [{id: "file_id", name: "File 1"}],
folders: []
},
"file_id": {
"bookmarks": {}
},
}
Notice that each key in the storage is the id for a folder, file, or the root. Each folder object contains two arrays of objects representing information to be displayed about its nested files and folders. But each object within those arrays does not contain information nested any further. With this structure, I'm trying to figure out how to enable folder deletion asynchronously, maybe using recursion. Here's what I have:
const deapRemoveFolder = async (uuid) => {
const promiseList = [];
const removeFolder = async (uuid) => {
const storage = await chrome.storage.sync.get(uuid);
if (storage[uuid]) {
const { files, folders } = storage[uuid];
// remove all directly nested files from storage
files.forEach((file) =>
promiseList.push(chrome.storage.sync.remove(file.uuid))
);
// remove the key for the folder itself
promiseList.push(chrome.storage.sync.remove(uuid));
// if no more folders are nested, then exist the function
if (folders.length === 0) return;
folders.forEach((folder) => removeFolder(folder.uuid));
}
};
await removeFolder(uuid);
await Promise.all(promiseList);
};
I'm not sure if this is right, and I don't know if I need to include "await" at the last line of the function "removeFolder". I want to make sure that I'm running these promises in parallel because not all of them depend on each other. I can give more clarification if needed.
you said: "But each object within those arrays does not contain information nested any further".
So there are not folder inside first-level folder. Did I understand correctly?
If so, why not to read the whole storage with chrome.storage.sync.get, delete the substructure you want (i.e delete storage.folder_id or delete storage.file_id") and finally save the trimmed object with chrome.storage.sync.set ?
Note that elements within chrome.storage are stored as a key + value pair, and it is not possible to delete a subtree directly with the remove method.
EDIT
I may have misunderstood one thing.
If you call await chrome.storage.sync.get(null) you get only one item called "storage" or you get one root item with several folders and files items?
If the right answer is one then my previous answer is still valid (you have to cut\trim the object and then save it at the end of the work in the chrome.storage).
If the right answer is two then the thing is simpler because you can directly delete any item using remove method and the object id without bothering recursion and other things.