Refactor forget function to use async/await

This commit is contained in:
buzzcode2007 2024-04-08 20:28:27 +08:00
parent 921dae135b
commit 6a5efb219d

View file

@ -305,57 +305,53 @@ export async function write(PATH, DATA, CLOUD = -1) {
@param {int} CLOUD the storage of the data @param {int} CLOUD the storage of the data
@return {boolean} the user's confirmation @return {boolean} the user's confirmation
*/ */
export function forget(preference, subpreference, CLOUD = 0) { export async function forget(preference, subpreference, CLOUD = 0) {
let forget_action = false; // Import alerts module.
let alerts = (await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))).default;
(async () => { // Confirm the action.
// Import alerts module. let forget_action = await alerts.confirm();
let alerts = (await import(chrome.runtime.getURL(`gui/scripts/alerts.js`)))[
`alerts`
];
// Confirm the action. if (forget_action) {
let forget_action = alerts.confirm_action(); if (preference) {
if (subpreference) {
if (forget_action) { // Get the data.
if (preference) { let DATA = await read(preference, CLOUD);
if (subpreference) {
// Get the data.
data = read(preference, CLOUD);
// Should only run when existent // Should only run when existent
if (data[subpreference]) { if (DATA[subpreference]) {
delete data[subpreference]; delete DATA[subpreference];
write([preference, subpreference], data, CLOUD); console.log(preference, DATA, CLOUD);
} write(preference, DATA, CLOUD);
} else {
// Remove that particular data.
if (CLOUD <= 0) {
chrome.storage.local.get(null, (data) => {
delete data[preference];
chrome.storage.local.set(data, (result) => {});
});
}
if (CLOUD >= 0) {
chrome.storage.sync.get(null, (data) => {
delete data[preference];
chrome.storage.sync.set(data, (result) => {});
});
}
} }
} else { } else {
// Clear the data storage. // Remove that particular data.
if (CLOUD >= 0) {
chrome.storage.sync.clear();
}
if (CLOUD <= 0) { if (CLOUD <= 0) {
chrome.storage.local.clear(); chrome.storage.local.get(null, (data) => {
delete data[preference];
chrome.storage.local.set(data, (result) => {});
});
}
if (CLOUD >= 0) {
chrome.storage.sync.get(null, (data) => {
delete data[preference];
chrome.storage.sync.set(data, (result) => {});
});
} }
} }
} else {
// Clear the data storage.
if (CLOUD >= 0) {
chrome.storage.sync.clear();
}
if (CLOUD <= 0) {
chrome.storage.local.clear();
}
} }
})(); }
return forget_action; return forget_action;
} }