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.