Skip to content

Commit

Permalink
batch
Browse files Browse the repository at this point in the history
  • Loading branch information
008 committed Nov 16, 2023
1 parent 4a1378b commit ddc080d
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Progressive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,18 +540,32 @@ class HeadingGroup {
}

async function getDocWordCount(docID: string): Promise<WordCountType[]> {
await siyuan.pushMsg("获取所有子块……");
const allBlocks: any[] = await siyuan.getChildBlocks(docID);

const size = 300
const groups = [];
while (allBlocks.length > 0) {
groups.push(allBlocks.splice(0, size));
}

await siyuan.pushMsg("开始统计字数……");
let iter = 0;
const content = [];
for (const { id, type } of await siyuan.getChildBlocks(docID)) {
// TODO: batch get counts
const { wordCount } = await siyuan.getBlocksWordCount([id]);
const count = wordCount;
content.push({ id, count, type });
++iter;
if (iter % 200 == 0) {
await siyuan.pushMsg(`已经统计了${iter}个块……`, 3000);
for (const group of groups) {
const tasks = [];
for (const { id } of group) {
tasks.push(siyuan.getBlocksWordCount([id]));
}
const rets = await Promise.all(tasks);
let i = 0;
for (const { id, type } of group) {
const { wordCount } = rets[i++];
const count = wordCount;
content.push({ id, count, type });
}
iter += size;
await siyuan.pushMsg(`已经统计了${iter}个块……`, 3000);
}
await siyuan.pushMsg("统计字数结束……");
return content;
Expand Down

0 comments on commit ddc080d

Please sign in to comment.