Skip to content

Commit

Permalink
fix: storage write not working
Browse files Browse the repository at this point in the history
  • Loading branch information
betterRunner committed Oct 29, 2021
1 parent 74857b1 commit bf5827b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
11 changes: 2 additions & 9 deletions src/content-scripts/renderer/popup/footer/ex-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export const importOper: Oper = {
title: "Import notes from json",
onClick: async () => {
const input = document.createElement("input");
input.setAttribute("id", "import-note-input");
const deleteInput = () => document.removeChild(input);
input.type = "file";
input.onchange = (e) => {
const errMsg =
Expand All @@ -42,7 +40,6 @@ export const importOper: Oper = {
ElMessage.error(
"The file format is wrong, please make sure to import the json file exported by Context-Note."
);
deleteInput();
return;
}
const reader = new FileReader();
Expand All @@ -51,7 +48,6 @@ export const importOper: Oper = {
const content = readerEvent?.target?.result?.toString(); // this is the content!
if (!content) {
ElMessage.error(errMsg);
deleteInput();
return;
}
try {
Expand All @@ -64,7 +60,6 @@ export const importOper: Oper = {
);
if (!isValid) {
ElMessage.error(errMsg);
deleteInput();
return;
}
const items = (await Promise.all(keys.map((k) => get(k)))).map(
Expand All @@ -83,12 +78,12 @@ export const importOper: Oper = {
)
.then(res => res)
.catch(() => {
deleteInput();
return;
});
}
for (const key of keys) {
set(key, json[key] ?? []);
console.log(key, json[key])
await set(key, json[key] ?? []);
}
mitt.emit("update-storage", null);
ElMessage.success(
Expand All @@ -97,11 +92,9 @@ export const importOper: Oper = {
} catch (err) {
ElMessage.error(err as string);
}
deleteInput();
};
reader.onerror = () => {
ElMessage.error(errMsg);
deleteInput();
};
};
input.click();
Expand Down
25 changes: 20 additions & 5 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
const _set = (key: string, value: any) =>
chrome.storage.local.set({ [key]: value });
// write the storage in order
let queue: [string, any][] = [];
const _set = (key: string, value: any) => {
return new Promise((resolve) => {
queue.push([key, value]);
const setter = () => chrome.storage.local.set({ [key]: value }, () => {
queue.shift();
if (queue.length) {
setter();
}
});
if (queue.length === 1) {
setter();
}
resolve(null);
})
}

const _get = (key: string) =>
new Promise((resolve) => {
Expand All @@ -20,7 +35,7 @@ export function get(key: string) {
export async function addItemToArr(arrKey: string, val: any) {
const arr = ((await _get(arrKey)) as any[]) ?? [];
arr.push(val);
set(arrKey, arr);
await set(arrKey, arr);
return arr;
}

Expand All @@ -35,7 +50,7 @@ export async function delItemFromArr(
);
if (index !== -1) {
arr.splice(index, 1);
set(arrKey, arr);
await set(arrKey, arr);
}
return arr;
}
Expand All @@ -52,7 +67,7 @@ const _operArrItem = async (
// run the `oper` to get new item
const newItem = oper(arr[index]);
arr.splice(index, 1, newItem);
set(arrKey, arr);
await set(arrKey, arr);
}
return arr;
};
Expand Down

0 comments on commit bf5827b

Please sign in to comment.