Skip to content

Commit

Permalink
ipv code
Browse files Browse the repository at this point in the history
  • Loading branch information
shenyutao committed Aug 25, 2023
1 parent e9b9e7e commit 309447c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function onLoad() {
}

function noNotifyDeleteItem(ids: (string | number)[]) {
tldrs.modify(data => {
tldrs.modify((data) => {
ids.forEach((id) => {
delete data[id];
});
Expand Down Expand Up @@ -128,7 +128,7 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) {
return false;
}
if (!forceFetch) {
return tldrs.get()[item.id] === undefined
return tldrs.get()[item.id] === undefined;
}
return true;
});
Expand All @@ -139,7 +139,9 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) {
return new ztoolkit.ProgressWindow(config.addonName, {
closeOnClick: closeOnClick,
}).createLine({
text: `${getString("popWindow-waiting")}: ${items.length}; ${getString("popWindow-succeed")}: 0; ${getString("popWindow-failed")}: 0`,
text: `${getString("popWindow-waiting")}: ${items.length}; ${getString(
"popWindow-succeed",
)}: 0; ${getString("popWindow-failed")}: 0`,
type: "default",
progress: 0,
});
Expand All @@ -158,7 +160,11 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) {
ztoolkit.ItemBox.refresh();
popupWin.changeLine({
progress: (index * 100) / count,
text: `${getString("popWindow-waiting")}: ${count - index - 1}; ${getString("popWindow-succeed")}: ${succeedItems.length}; ${getString("popWindow-failed")}: ${failedItems.length}`,
text: `${getString("popWindow-waiting")}: ${
count - index - 1
}; ${getString("popWindow-succeed")}: ${
succeedItems.length
}; ${getString("popWindow-failed")}: ${failedItems.length}`,
});
}
})();
Expand All @@ -167,7 +173,9 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) {
popupWin.changeLine({
type: "success",
progress: 100,
text: `${getString("popWindow-succeed")}: ${succeedItems.length}; ${getString("popWindow-failed")}: ${failedItems.length}`,
text: `${getString("popWindow-succeed")}: ${
succeedItems.length
}; ${getString("popWindow-failed")}: ${failedItems.length}`,
});
popupWin.startCloseTimer(3000);
})();
Expand Down
19 changes: 14 additions & 5 deletions src/modules/dataStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export class Data<K extends string | number | symbol, V> {
return this.data;
}

async modify(action: (data: Record<K, V>) => Record<K, V> | Promise<Record<K, V>>) {
async modify(
action: (data: Record<K, V>) => Record<K, V> | Promise<Record<K, V>>,
) {
await this.initDataIfNeed();
const data = this.data;
const newData = await action(data);
Expand Down Expand Up @@ -54,7 +56,9 @@ export class Data<K extends string | number | symbol, V> {
}

private async initDataIfNeed() {
if (this.inited) { return; }
if (this.inited) {
return;
}
this.inited = true;
try {
this.data = await IOUtils.readJSON(this.filePath, { decompress: false });
Expand All @@ -74,7 +78,9 @@ export class DataStorage {

private static shared = new DataStorage();

static instance<K extends string | number | symbol, V>(dataType: string): Data<K, V> {
static instance<K extends string | number | symbol, V>(
dataType: string,
): Data<K, V> {
const path = PathUtils.join(this.shared.dataDir, dataType);
if (this.shared.dataMap[dataType] === undefined) {
const data = new Data<K, V>(path);
Expand All @@ -86,10 +92,13 @@ export class DataStorage {
}

private constructor() {
IOUtils.makeDirectory(this.dataDir, { createAncestors: true, ignoreExisting: true });
IOUtils.makeDirectory(this.dataDir, {
createAncestors: true,
ignoreExisting: true,
});
}
}

export const TLDRUnrelated = "tldr-unrelated"; // semantic scholar 找到了该item,但是该item没有tldr
export const TLDRItemNotFound = "tldr-itemnotfound"; // semantic scholar 找不到该item
export const tldrs = DataStorage.instance<string | number, string>('TLDR.json');
export const tldrs = DataStorage.instance<string | number, string>("TLDR.json");

0 comments on commit 309447c

Please sign in to comment.